how to add the contents of a test file in a vb6 listbox one lne at a time

I am trying to read in a log text file from a network drive based on inputbox value. The file contains names of files that had been scanned in for that day. I need to write the contents of the text file to a vb6 listbox that will allow me to open one or multiple files the user chooses. The files are in .pdf format. This is what I have but it will not work.

gdate = Format(frmSelectionReader.txtDate, "yyyy-mm-dd")

path = "network drive\" & gdate
    LstFile2.Pattern = "*.txt"
'   LstFile2.path = path
    If Right(path, 1) <> "\" Then
        path = path & "\"
    End If

All I get is the file name not the contents inside the file
Who is Participating?
Hi Sam,

You can use a function like this to get a file's contents:

Function OpenTextFileIntoLineArray(ByVal txtFilePath As String) As String()
 Dim vFF As Long, tempArr() As String, Cnt As Long
 Cnt = 0
 ReDim tempArr(Cnt)
 If Len(Dir(txtFilePath)) = 0 Then
  'if file doesn't exist, returns one-item array specifying invalid source file
  tempArr(0) = "Invalid Source File"
  'return array containing the file's contents
  vFF = FreeFile
  Open txtFilePath For Input As #vFF
  Do Until EOF(vFF)
   ReDim Preserve tempArr(Cnt)
   Line Input #vFF, tempArr(Cnt)
   Cnt = Cnt + 1
  Close #vFF
 End If
 OpenTextFileIntoLineArray = tempArr
End Function

Just use it like this:

 LstFile2.List = OpenTextFileIntoLineArray(yourLogTextFilePath)

samiam41Author Commented:
Thanks mvidas
When I run the code I am getting an error message "Can't assign to read only property. The property for the list box has read only property = false. Am I missing a reference or something.

Mike TomlinsonMiddle School Assistant TeacherCommented:
To load lines from a text file into a ListBox:

    Dim ff As Integer
    Dim line As String
    Dim fileName As String
    Dim fileContents As String
    fileName = "c:\someFile.txt"
    ff = FreeFile
    Open fileName For Input As #ff
    While Not EOF(ff)
        Line Input #ff, line
        List1.AddItem line
    Close #ff
Alternatively you can use FileSystemObject to do you reading. I find it has less nuances than VB, it doesn't try to be "smart" for you and parse the input. On the other hand it is an activex object though. So you use what you're comfortable with.

You can do something like:

1) First, add "Microsoft Scripting Runtime" to your Project References.

2) Use code like this:

        Dim fso As New FileSystemObject
        Dim ts As TextStream
        'Open file.
        Set ts = fso.OpenTextFile("C:\somefolder\mylogfile.txt")
        'Loop while not at the end of the file.
        Do While Not ts.AtEndOfStream
          Debug.Print ts.ReadLine
        'Close the file.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.