1 Attachment(s)
Loading from a flat (txt) file to Listbox
I have an input file similar to the one below:-
- The first 6 characters (1,6) for all rows would be the same . Following this
- in the first row (7,1 st position) there will be some 1 digit
- in the second row there will be EOL(7,3 rd position), followed by 9 Characters (H6E016094)(10,9 th position)
- in the third row there will be 95 followed by 7 digits (4125159) (9,7 th position)
2188571
218857EOLH6E016094
218857954125159
How do I strip these fields from file and diplay that in a form inside a List Box. Below is how I expect to see the value from the file to be displayed.
Column1 Column2 Column3 Column4
1 EOL H6E016094 954125159
Is using Listbox is the correct approach or how do I go about?
Re: Loading from a flat (txt) file to Listbox
You want to use a Listview or some type of grid rather than a listbox.
Re: Loading from a flat (txt) file to Listbox
I am not familar with neither ListBox nor Grid. I leave the choice to you guys for which one would be better and easy to understand
Re: Loading from a flat (txt) file to Listbox
Can you attach a sample file?
Re: Loading from a flat (txt) file to Listbox
First post edited with the attachment
Re: Loading from a flat (txt) file to Listbox
I see your sample but that's not what I meant. I'm looking for an actual data file.
In your explanation you seem to want to put data from the first 3 lines in the file on the same line in the listbox/listview. Is that correct? If so is there more data and is all the data in sets of 3 rows?
Re: Loading from a flat (txt) file to Listbox
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim f As Integer
Dim i As Integer
Dim s As String
f = FreeFile
Open "C:\sample.txt" For Input As f
For i = 1 To 3
Line Input #f, s
Select Case i
Case 1
Debug.Print Mid$(s, 7, 1)
Case 2
Debug.Print Mid$(s, 7, 3)
Debug.Print Mid$(s, 10, 9)
Case 3
Debug.Print Mid$(s, 7, 9)
End Select
Next
Close f
End Sub
Re: Loading from a flat (txt) file to Listbox
All the data to be populated for one row is there in the file. Always data from set of 3 rows in line will form a one row in the listbox. If I go with more data in the sample file, that won't help much I think Martin.
Re: Loading from a flat (txt) file to Listbox
Well it would help to test it better, but try this.
VB Code:
Dim ff As Integer
Dim strLine As String
Dim clmX As ColumnHeader
Dim itmX As ListItem
Dim intPart As Integer
' Add ColumnHeaders.
Set clmX = ListView1.ColumnHeaders.Add(, , "Col 1", 900)
Set clmX = ListView1.ColumnHeaders.Add(, , "Col 2", 900)
Set clmX = ListView1.ColumnHeaders.Add(, , "Col 3", 1200)
Set clmX = ListView1.ColumnHeaders.Add(, , "Col 4", 1200)
ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
ListView1.View = lvwReport ' Set View property to Report.
ff = FreeFile
Open "C:\temp\test.txt" For Input As #ff
Do Until EOF(ff)
For intPart = 1 To 3
Select Case intPart
Case 1
Line Input #ff, strLine
Set itmX = ListView1.ListItems.Add(, , Mid$(strLine, 7, 1))
Line Input #ff, strLine
itmX.SubItems(1) = Mid$(strLine, 7, 3)
Case 2
itmX.SubItems(2) = Mid$(strLine, 10)
Case 3
Line Input #ff, strLine
itmX.SubItems(3) = Mid$(strLine, 7)
End Select
Next
Loop
Close ff
Re: Loading from a flat (txt) file to Listbox
Will try out that. ThankYou for all the responses.