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

Posted on 2007-10-17
Last Modified: 2013-12-26
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
Question by:samiam41
    LVL 35

    Accepted Solution

    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)

    LVL 9

    Author Comment

    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.

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
    LVL 31

    Expert Comment

    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.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now