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.

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.