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 :)
Re: Parse/Load a Text Document
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:
Private Sub Form_Load()
Dim itmX As ListItem ' Create a variable to add ListItem objects.
Dim clmX As ColumnHeader ' Create an object variable for the ColumnHeader object.
' Add ColumnHeaders.
Set clmX = ListView1.ColumnHeaders.Add(, , "Column 1", ListView1.Width / 3)
Set clmX = ListView1.ColumnHeaders.Add(, , "Column 2", ListView1.Width / 3)
Set clmX = ListView1.ColumnHeaders.Add(, , "Column 3", ListView1.Width / 3)
ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
ListView1.View = lvwReport ' Set View property to Report.
' Add a main item
Set itmX = ListView1.ListItems.Add(, , "First value")
' Add two subitems for that item
itmX.SubItems(1) = "First value subitem 1"
itmX.SubItems(2) = "First value subitem 2"
' Add another main item
Set itmX = ListView1.ListItems.Add(, , "Second value")
' Add two subitems for that item
itmX.SubItems(1) = "Second value subitem 1"
itmX.SubItems(2) = "Second value subitem 2"
' Add another main item
Set itmX = ListView1.ListItems.Add(, , "Third value")
' Add two subitems for that item
itmX.SubItems(1) = "Third value subitem 1"
itmX.SubItems(2) = "Third value subitem 2"
End Sub
EDIT: Coded by MartinLiss
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:
Dim itmX As ListItem ' Create a variable to add ListItem objects.
Dim clmX As ColumnHeader ' Create an object variable for the ColumnHeader object.
Dim ff As Integer
Dim strParts() As String
Dim strLine As String
ff = FreeFile
' Add ColumnHeaders.
Set clmX = ListView1.ColumnHeaders.Add(, , "Column 1", ListView1.Width / 2)
Set clmX = ListView1.ColumnHeaders.Add(, , "Column 2", ListView1.Width / 2)
ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
ListView1.View = lvwReport ' Set View property to Report.
Open "C:\temp\something.txt" For Input As ff
Do Until EOF(ff)
Line Input #ff, strLine
strParts = Split(strLine, ":")
' Add a main item
Set itmX = ListView1.ListItems.Add(, , RTrim(strParts(0)))
' Add a subitem for that item
itmX.SubItems(1) = strParts(1)
Loop
Close ff
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:
With Loader1
.DialogTitle = "List Loader"
.Filter = "*.txt"
.ShowOpen
FName = .FileName
MsgBox FName
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.
Re: Parse/Load a Text Document
Credit to MartinLiss. :)
VB Code:
.Filter = "Text Files (*.txt) |*.txt"
Re: Parse/Load a Text Document
And here is how to put it all together.
VB Code:
Dim itmX As ListItem ' Create a variable to add ListItem objects.
Dim clmX As ColumnHeader ' Create an object variable for the ColumnHeader object.
Dim ff As Integer
Dim strParts() As String
Dim strLine As String
ff = FreeFile
' Add ColumnHeaders.
Set clmX = ListView1.ColumnHeaders.Add(, , "Column 1", ListView1.Width / 2)
Set clmX = ListView1.ColumnHeaders.Add(, , "Column 2", ListView1.Width / 2)
ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
ListView1.View = lvwReport ' Set View property to Report.
With Loader1
.DialogTitle = "List Loader"
.Filter = "Text Files (*.txt) |*.txt"
.ShowOpen
If .FileName = "" Then
MsgBox "No file selected", vbCritical
Exit Sub
End If
Open .FileName For Input As ff
End With
Do Until EOF(ff)
Line Input #ff, strLine
strParts = Split(strLine, ":")
' Add a main item
Set itmX = ListView1.ListItems.Add(, , RTrim(strParts(0)))
' Add a subitem for that item
itmX.SubItems(1) = strParts(1)
Loop
Close ff
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:
Public Function STZ_ErrHandleFILE(sFunctionName As String)
On Error GoTo STZ_ErrHandleFILE_Err
Open App.Path & "\ErrorLog.log" For Append As #1
Print #1, Now() & " - " & Err.Number & " - " & Err.Description & " - " & " In " & sFunctionName
Close #1
Err.Clear
On Error GoTo 0
Exit Function
STZ_ErrHandleFILE_Err:
STZ_ErrHandleFILE "STZ_ErrHandleFILE"
End Function
The information is then written like so:
Quote:
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:
Private Sub Form_Load()
SizeInternalForms frmErrorLogViewer
Dim ErrorLog As ListItem
Dim intFF As Integer
Dim strParts() As String
Dim strLine() As String
Open App.Path & "\ErrorLog.log" For Input As #intFF
Do Until EOF(intFF)
Line Input #intFF, strLine
strParts = spilt(strLine, "-")
Set ErrorLog = lvwErrorLog.ListItems.Add(, , RTrim(strParts(0))) 'date/time
ErrorLog.SubItems(1) = strParts(1) 'error number
ErrorLog.SubItems(2) = strParts(2) 'error description
ErrorLog.SubItems(3) = strParts(3) 'function name
Loop
Close intFF
End Sub
The error I get is:
Quote:
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?
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.
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.