jadams0173
Technical User
- Feb 18, 2005
- 1,210
I'm tring to read a text file from a point where I left off. I know the line number where I stopped and would like to start reading again after that line. Is this possible. I've been searching but I guess I'm not using the right criteria to find what I'm looking for.
Here is what I have.
(The reason to copy the text file to a table, is not everyone has permissions to the folder. Also this is a production text file and should someone open or delete it bad things could happen. The text file is generated by some test equipment. A software upgrade when from using a TAB as the delimiter to a SPACE, which is why I do the replace. There is also lines that are of no use to the user so I delete them. The table is used to run queries off of other tables.)
Here is what I have.
(The reason to copy the text file to a table, is not everyone has permissions to the folder. Also this is a production text file and should someone open or delete it bad things could happen. The text file is generated by some test equipment. A software upgrade when from using a TAB as the delimiter to a SPACE, which is why I do the replace. There is also lines that are of no use to the user so I delete them. The table is used to run queries off of other tables.)
Code:
Function ReadTextFile()
Dim fso As FileSystemObject
Dim STREAM As TextStream
Dim CurLine As String
Dim arrValues() As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tblTextFileImport", dbOpenDynaset)
Set fso = New FileSystemObject
'OPEN THE TEXT FILE
Set STREAM = fso.OpenTextFile("F:\scanfile.txt")
'LOOP THRU THE TEXT FILE ONE LINE AT A TIME
While Not STREAM.AtEndOfStream
'READ THE LINE
CurLine = STREAM.ReadLine
'REPLACE THE {TAB} WITH A {SPACE}
CurLine = Replace(CurLine, vbTab, Chr$(32), , , vbTextCompare)
'SPLIT THE LINE BY THE {SPACE} INTO THE ARRAY ARRVALUES
arrValues = Split(CurLine, " ")
'ADD THE CONTENTS OF THE TEXT FILE TO THE TABLE
With rs
.AddNew
!id = Val(Trim(arrValues(0)))
!SFCNUMBER = Trim(arrValues(1))
.Update
End With
Wend
'DELETE THE RECORDS THAT DO NOT CONTAIN MTX AS THE START OF THE SFC NUMBER
db.Execute "DELETE tblTextFileImport.SFCNUMBER " _
& "FROM tblTextFileImport " _
& "WHERE (((tblTextFileImport.SFCNUMBER) Not Like 'MTX*'))"
'REFRESHES THE RECORDSET SINCE WE DELETED RECORDS FROM IT
rs.Requery
'STORE THE LAST LINE NUMBER OF THE TEXT FILE IN THE LAS RECORD OF THE TABLE
With rs
.MoveFirst
.MoveLast
.Edit
!LASTLINE = STREAM.Line - 1
.Update
End With
rs.Close
db.Close
Debug.Print STREAM.Line - 1
STREAM.Close
Set rs = Nothing
Set db = Nothing
Set STREAM = Nothing
Set fso = Nothing
End Function