Option Explicit
Private Sub Form_Load()
Dim intX As Integer
Dim strCurrFileName As String
Dim intCurrFileHandle As Integer
Dim astrFileFields() As String
Dim intFieldNbr As Integer
Dim objCurrLI As ListItem
SetupListView
Do
intX = intX + 1
' The Do loop will exit (and files will stop loading) as soon
' as it is discovered that a particular file is not there.
' This assumes the files are sequentially numbered with no gaps.
' If this assumption is not correct, you will need to adjust the
' code to handle the situation (i.e., skip the missing file and
' continue on to the next) ...
strCurrFileName = "p:\fleets\estimates\" & CStr(intX) & ".txt"
If Dir(strCurrFileName) = "" Then
Exit Do
End If
' get a Windows file handle
intCurrFileHandle = FreeFile
' Open the current file
Open strCurrFileName For Input As #intCurrFileHandle
'clear the array which will store the current file's fields
Erase astrFileFields
intFieldNbr = -1
Do Until EOF(intCurrFileHandle)
' Based on your post, each "field" of the text file is on a
' separate line. I am assuming "<CONTAINS>" and "<END CONTAINS>"
' are also there. Anyway, this code will store each line (field)
' of the file in an element of the astrFileFields array ...
intFieldNbr = intFieldNbr + 1
ReDim Preserve astrFileFields(0 To intFieldNbr)
Line Input #intCurrFileHandle, astrFileFields(intFieldNbr)
Loop
Close intCurrFileHandle
' Now add a listitem in the listview for this file's data ...
' It is assumed that we want to NOT load the "CONTAINS" and "END CONTAINS" lines.
' Basically, as long you know what order the fields came in on in the file, you
' can assign them to the appropriate column of the listview.
Set objCurrLI = ListView1.ListItems.Add(, , astrFileFields(1))
objCurrLI.SubItems(1) = astrFileFields(2)
objCurrLI.SubItems(2) = astrFileFields(3)
Loop
End Sub
Private Sub SetupListView()
With ListView1
.View = lvwReport
.GridLines = True
.ColumnHeaders.Clear
.ListItems.Clear
' set up 3 columns, each having width 1/3 the size of
' the ListView itself ...
.ColumnHeaders.Add , , "Estimate #", .Width * 0.33
.ColumnHeaders.Add , , "Reg #", .Width * 0.33
.ColumnHeaders.Add , , "Date", .Width * 0.33
End With
End Sub