Results 1 to 10 of 10

Thread: Parse/Load a Text Document

  1. #1

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Parse/Load a Text Document

    I recently asked this question, but never was given a complete answer, and im still a little slow on the whole idea of it.

    Anyways, basicly I want to load a text document into a listview. An example entry would be:

    Something1 : Something2

    And then when it loaded, Something1 would be loaded as an Item in the listview, and then Something2 would be added as a SubItem for that Item. I know how to loop through a list view and everything, just parsing and adding it im not sure about it. Thanks in advanced
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  2. #2

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Parse/Load a Text Document

    Any help?
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Parse/Load a Text Document

    A Listview seems like a poor choice to load a file into. I think a flexgrid is easier to work with, and should do what you need it to do. Here is the method for loading a listview, though. It wouldn't be very flexible, but if the data is always in the same format, you could split lines into an array, and then load them in.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim itmX As ListItem ' Create a variable to add ListItem objects.
    4.     Dim clmX As ColumnHeader ' Create an object variable for the ColumnHeader object.
    5.    ' Add ColumnHeaders.
    6.     Set clmX = ListView1.ColumnHeaders.Add(, , "Column 1", ListView1.Width / 3)
    7.     Set clmX = ListView1.ColumnHeaders.Add(, , "Column 2", ListView1.Width / 3)
    8.     Set clmX = ListView1.ColumnHeaders.Add(, , "Column 3", ListView1.Width / 3)
    9.    
    10.     ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
    11.     ListView1.View = lvwReport ' Set View property to Report.
    12.    
    13.     ' Add a main item
    14.     Set itmX = ListView1.ListItems.Add(, , "First value")
    15.     ' Add two subitems for that item
    16.     itmX.SubItems(1) = "First value subitem 1"
    17.     itmX.SubItems(2) = "First value subitem 2"
    18.  
    19.     ' Add another main item
    20.     Set itmX = ListView1.ListItems.Add(, , "Second value")
    21.     ' Add two subitems for that item
    22.     itmX.SubItems(1) = "Second value subitem 1"
    23.     itmX.SubItems(2) = "Second value subitem 2"
    24.    
    25.     ' Add another main item
    26.     Set itmX = ListView1.ListItems.Add(, , "Third value")
    27.     ' Add two subitems for that item
    28.     itmX.SubItems(1) = "Third value subitem 1"
    29.     itmX.SubItems(2) = "Third value subitem 2"
    30.    
    31.  
    32. End Sub

    EDIT: Coded by MartinLiss
    Last edited by dglienna; Oct 1st, 2005 at 05:22 PM.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Parse/Load a Text Document

    Here a working example of reading, parsing, and loading a file like the one you describe based on the code that dglienna borrowed from me.

    VB Code:
    1. Dim itmX As ListItem ' Create a variable to add ListItem objects.
    2.     Dim clmX As ColumnHeader ' Create an object variable for the ColumnHeader object.
    3.     Dim ff As Integer
    4.     Dim strParts() As String
    5.     Dim strLine As String
    6.    
    7.     ff = FreeFile
    8.    
    9.    ' Add ColumnHeaders.
    10.     Set clmX = ListView1.ColumnHeaders.Add(, , "Column 1", ListView1.Width / 2)
    11.     Set clmX = ListView1.ColumnHeaders.Add(, , "Column 2", ListView1.Width / 2)
    12.    
    13.     ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
    14.     ListView1.View = lvwReport ' Set View property to Report.
    15.    
    16.     Open "C:\temp\something.txt" For Input As ff
    17.    
    18.     Do Until EOF(ff)
    19.         Line Input #ff, strLine
    20.         strParts = Split(strLine, ":")
    21.         ' Add a main item
    22.         Set itmX = ListView1.ListItems.Add(, , RTrim(strParts(0)))
    23.         ' Add a subitem for that item
    24.         itmX.SubItems(1) = strParts(1)
    25.     Loop
    26.  
    27.     Close ff

  5. #5

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Parse/Load a Text Document

    I see, thank you very much

    One last thing, I know it's a little off subject, but instead of just giving the path, im using Common Dialog Control, to let the user choose the file. Can someone show the correct way to filter what is shown? I am using this currently:

    VB Code:
    1. With Loader1
    2. .DialogTitle = "List Loader"
    3. .Filter = "*.txt"
    4. .ShowOpen
    5. FName = .FileName
    6. MsgBox FName
    7. End With

    Everything goes right, except it's not filtering only ".txt" files. I see it down in the "File Type" on the control, but I can still see EXE's, DLL's, and all the rest.

    Thanks again.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Parse/Load a Text Document

    Credit to MartinLiss.


    VB Code:
    1. .Filter = "Text Files (*.txt) |*.txt"

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Parse/Load a Text Document

    And here is how to put it all together.

    VB Code:
    1. Dim itmX As ListItem ' Create a variable to add ListItem objects.
    2.     Dim clmX As ColumnHeader ' Create an object variable for the ColumnHeader object.
    3.     Dim ff As Integer
    4.     Dim strParts() As String
    5.     Dim strLine As String
    6.    
    7.     ff = FreeFile
    8.    
    9.    ' Add ColumnHeaders.
    10.     Set clmX = ListView1.ColumnHeaders.Add(, , "Column 1", ListView1.Width / 2)
    11.     Set clmX = ListView1.ColumnHeaders.Add(, , "Column 2", ListView1.Width / 2)
    12.    
    13.     ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
    14.     ListView1.View = lvwReport ' Set View property to Report.
    15.    
    16.     With Loader1
    17.         .DialogTitle = "List Loader"
    18.         .Filter = "Text Files (*.txt) |*.txt"
    19.         .ShowOpen
    20.         If .FileName = "" Then
    21.             MsgBox "No file selected", vbCritical
    22.             Exit Sub
    23.         End If
    24.         Open .FileName For Input As ff
    25.     End With
    26.    
    27.     Do Until EOF(ff)
    28.         Line Input #ff, strLine
    29.         strParts = Split(strLine, ":")
    30.         ' Add a main item
    31.         Set itmX = ListView1.ListItems.Add(, , RTrim(strParts(0)))
    32.         ' Add a subitem for that item
    33.         itmX.SubItems(1) = strParts(1)
    34.     Loop
    35.    
    36.     Close ff

  8. #8
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Parse/Load a Text Document

    I am looking to do something similar. I have an error log that my app will write all errors to. I am looking to load that into a viewer for the end user.

    This is what I have so far:
    From Statics add-in (modified) -- write the info to ErrorLog.log
    VB Code:
    1. Public Function STZ_ErrHandleFILE(sFunctionName As String)
    2.     On Error GoTo STZ_ErrHandleFILE_Err
    3.  
    4.     Open App.Path & "\ErrorLog.log" For Append As #1
    5.     Print #1, Now() & " - " & Err.Number & " - " & Err.Description & " - " & " In " & sFunctionName
    6.     Close #1
    7.     Err.Clear
    8.     On Error GoTo 0
    9.     Exit Function
    10. STZ_ErrHandleFILE_Err:
    11.  
    12.     STZ_ErrHandleFILE "STZ_ErrHandleFILE"
    13. End Function
    The information is then written like so:
    date1 - error number 1 - error desc 1 - in function name 1
    date2 - error number 2 - error desc 2 - in function name 2
    and this is what i have to load the file and where i get the error.
    VB Code:
    1. Private Sub Form_Load()
    2.     SizeInternalForms frmErrorLogViewer
    3.  
    4.     Dim ErrorLog As ListItem
    5.     Dim intFF As Integer
    6.     Dim strParts() As String
    7.     Dim strLine() As String
    8.    
    9.     Open App.Path & "\ErrorLog.log" For Input As #intFF
    10.    
    11.     Do Until EOF(intFF)
    12.         Line Input #intFF, strLine
    13.         strParts = spilt(strLine, "-")
    14.        
    15.         Set ErrorLog = lvwErrorLog.ListItems.Add(, , RTrim(strParts(0))) 'date/time
    16.         ErrorLog.SubItems(1) = strParts(1) 'error number
    17.         ErrorLog.SubItems(2) = strParts(2) 'error description
    18.         ErrorLog.SubItems(3) = strParts(3) 'function name
    19.         Loop
    20.     Close intFF
    21. End Sub
    The error I get is:
    Compile error
    Type mismatch
    on this line:
    Private Sub Form_Load() <-----highlighted in yellow
    Line Input #intFF, strLine <-----strLine highlighted in blue

    please advise. is it because of
    Set ErrorLog = lvwErrorLog.ListItems.Add(, , RTrim(strParts(0))) 'date/time

    being a date/time format?

  9. #9

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Parse/Load a Text Document

    This is a rather old topic, but your problem is don't dim strLine as an array, just a normal string variable.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  10. #10
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Parse/Load a Text Document

    Quote Originally Posted by Inuyasha1782
    This is a rather old topic, but your problem is don't dim strLine as an array, just a normal string variable.
    ok, thanks. got it working.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width