Hi all,

I'm make a program to read text file.
I can read the text file but don't know how to write it to listview.
This my code to read into arrays:

Function ReadFileText(ByVal filename As String) As String
        Dim handle As Integer

           ' ensure that the file exists
        If Len(Dir$(filename)) = 0 Then
            Err.Raise 53  ' File not found
        End If

           ' open in binary mode
        handle = FreeFile
        Open filename$ For Binary As #handle
           ' read the string and close the file
        ReadFileText = Space$(LOF(handle))
        Get #handle, , ReadFileText
        Close #handle
End Function

Private Sub Command1_Click()
Dim temp() As String
Dim temp2() As String
Dim FNArr() As String
Dim LNArr() As String
Dim AgeArr() As String
Dim BloddArr() As String

    ' split text file by new line (enter)
    temp = Split(ReadFileText("D:\Mid Task\Data.txt"), vbNewLine)

    ' get how many rows in array
    x = UBound(temp) 

    ' redim each array with new rows. 
    ReDim FNArr(x)
    ReDim LNArr(x)
    ReDim AgeArr(x)
    ReDim BloddArr(x)

    For i = 0 To x
        ' split each line by semicolon sign
        temp2 = Split(temp(i), ";")

        ' write each data to proper arrays
        FNArr(i) = temp2(0)
        LNArr(i) = temp2(1)
        AgeArr(i) = temp2(2)
        BloddArr(i) = temp2(3)
    Next i

End Sub

Any help will be appreciated.
Thank you.

Dani AI

Generated

Nice start, . Rather than loading the entire file into one big string and splitting it yourself, read the file line-by-line and push each row straight into the ListView. That avoids off-by-one bugs from trailing blank lines, reduces memory use, and lets you skip malformed rows safely. Also, set up the columns once (for example in Form_Load) so you do not re-add headers on every click. and already showed the core pattern; the version below just makes it a bit more robust and self-contained.

Private Sub Form_Load()
    ' One-time ListView setup
    With ListView1
        .View = lvwReport
        .FullRowSelect = True
        .GridLines = True
        If .ColumnHeaders.Count = 0 Then
            .ColumnHeaders.Add , , "First Name", 1500
            .ColumnHeaders.Add , , "Last Name", 2000
            .ColumnHeaders.Add , , "Age", 900
            .ColumnHeaders.Add , , "Blood Type", 1200
        End If
    End With
End Sub

Public Sub LoadPeopleFromFile(ByVal path As String)
    Dim f As Integer, sLine As String, parts() As String
    On Error GoTo Fail
    ListView1.ListItems.Clear

    f = FreeFile
    Open path For Input As #f
    Do While Not EOF(f)
        Line Input #f, sLine
        sLine = Trim$(sLine)
        If Len(sLine) > 0 Then
            parts = Split(sLine, ";")
            If UBound(parts) >= 3 Then
                Dim it As MSComctlLib.ListItem
                Set it = ListView1.ListItems.Add(, , Trim$(parts(0)))
                it.SubItems(1) = Trim$(parts(1))
                it.SubItems(2) = Trim$(parts(2))
                it.SubItems(3) = Trim$(parts(3))
            End If
        End If
    Loop
Done: If f <> 0 Then Close #f: Exit Sub
Fail:  MsgBox "Could not load file: " & Err.Description, vbExclamation: Resume Done
End Sub

Quick tips:

  • If you see an empty row, your file likely ends with an extra newline; the Trim$/Line Input combo above avoids that.
  • SubItems are 1-based in VB6. You need exactly 3 subitems for 4 columns.
  • If you get a missing type error for ListItem, add the component: Project -> Components -> Microsoft Windows Common Controls 6.0 (SP6).

Recommended Answers

All 5 Replies

Give more information here.
How many columns you want in listview? and which data you want it to written in listview?

This is how you accessing listview and write data in it. Modifying it as you need.
Assume you have 4 columns :

    With ListView1.ListItems
        .Add , , Text1.Text
        .Item(.Count).SubItems(1) = Text2.Text
        .Item(.Count).SubItems(2) = Text3.Text
        .Item(.Count).SubItems(3) = Text4.Text
    End With

Just using loop through array and write it with sample code above

commented: Thanks for the code +1

Thank you for replyin estella.

I want it in 4 columns (First name, last name, age and blood type).
There are 4 arrays that i want it to written in list view but the point is how i can read it form text file and use listview to show it.

Thank you

Something like this should work:

Private Sub Command1_Click()
    Dim temp() As String
    Dim temp2() As String
    Dim FNArr() As String
    Dim LNArr() As String
    Dim AgeArr() As String
    Dim BloddArr() As String
    Dim Itm As MSComctlLib.ListItem        
    ' split text file by new line (enter)
    temp = Split(ReadFileText("D:\Mid Task\Data.txt"), vbNewLine)
    ' get how many rows in array
    x = UBound(temp)
    ' redim each array with new rows.
    ReDim FNArr(x)
    ReDim LNArr(x)
    ReDim AgeArr(x)
    ReDim BloddArr(x)
    For i = 0 To x            
        ' split each line by semicolon sign
        temp2 = Split(temp(i), ";")
        ' write each data to proper arrays
        FNArr(i) = temp2(0)
        LNArr(i) = temp2(1)
        AgeArr(i) = temp2(2)
        BloddArr(i) = temp2(3)
        'Write the same data to the listview
        Set Itm = ListView1.ListItems.Add(, , temp2(0))
        Itm.SubItems(1) = temp2(1)
        Itm.SubItems(2) = temp2(2)
        Itm.SubItems(3) = temp2(3)          
    Next i
End Sub
commented: Thanks for the code +1

I want it in 4 columns (First name, last name, age and blood type).
There are 4 arrays that i want it to written in list view but the point is how i can read it form text file and use listview to show it.

This should working :

Private Sub Command1_Click()
Dim temp() As String

temp = Split(ReadFileText("D:\Mid Task\Data.txt"), vbNewLine)

With ListView1
    .View = lvwReport
    .FullRowSelect = True
    .ColumnHeaders.Add , , "First Name", 1100
    .ColumnHeaders.Add , , "Last Name", 1400
    .ColumnHeaders.Add , , "Age", 700
    .ColumnHeaders.Add , , "Blood Type", 1000


    With .ListItems
        .Clear
        For i = 0 To UBound(temp)
            temp2 = Split(temp(i), ";")

            Set lsvItem = .Add(, , temp2(0))
            lsvItem.SubItems(1) = temp2(1)
            lsvItem.SubItems(2) = temp2(2)
            lsvItem.SubItems(3) = temp2(3)

        Next i
    End With

End With

I think you don't have to use another arrays. Read data to temp array and write it to listview.

Hope it helps.

commented: Thanks for the code +1

tinstaafl and jxman,
Thank you for both of you. Your code working great

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.