• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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
0
samiam41
Asked:
samiam41
1 Solution
 
mvidasCommented:
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"
 Else
  '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
  Loop
  Close #vFF
 End If
 OpenTextFileIntoLineArray = tempArr
End Function

Just use it like this:

 LstFile2.List = OpenTextFileIntoLineArray(yourLogTextFilePath)

Matt
0
 
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.

0
 
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
    Wend
    Close #ff
0
 
Frosty555Commented:
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
        Loop
        'Close the file.
        ts.Close


Source: http://support.microsoft.com/kb/186118
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now