Input multiple line text file into one access record each

A

AccessVBBef

Hello,

I currently have this code to input into one record per file

Public Function InputValues()

Dim intInputFile As Integer
Dim strLineIn As String
Dim strFileToImport As String
Dim intCounter As Integer
Dim strText As String
Dim intFindDelim As Integer

strFileToImport = "C:\InputFile.txt"
intInputFile = FreeFile
Open strFileToImport For Input As intInputFile

'--open the file for reading, skipping the first four lines
For intCounter = 1 To 4
Line Input #intInputFile, strLineIn
Next intCounter

Set rst = CurrentDb.OpenRecordset("tblRecords")
rst.AddNew
intCounter = 0

'--read the rest of the file. Basic idea is that intFieldNo
(zero-based) = the sort of offset of the value
'--IOW, if you loop through a counter, the counter= 0 at 5 (line
1). then these will match the field offsets
'--in the table.

Do Until EOF(intInputFile)
Line Input #intInputFile, strLineIn
'strLineIn needs to be parsed a little - strip off the junk
intFindDelim = InStr(1, strLineIn, "=", vbTextCompare)
If intFindDelim = 0 Then Exit Do
'--trim off the junk
strText = Trim$(Right$(strLineIn, Len(strLineIn) -
intFindDelim))
'--write the field to the record
rst.Fields(intCounter) = strText
intCounter = intCounter + 1
Loop

rst.Update

rst.Close
Close intInputFile
Set rst = Nothing

MsgBox "All Done!"
'DoCmd.OpenTable "Application"

End Function

I need to be able to loop this every 25 lines per record - skipping the
input of the 1st 5 lines and the last line.
 
J

John Nurick

How about reading 25 lines into a 25-element array. Air code:


Dim arLines(25) As String
Dim j As Long

...

'--open the file for reading, skipping the first four lines
For j = 1 To 4
Line Input #intInputFile, strLineIn
Next j

...

'Main loop
Do Until EOF(intInputFile)
'read 25 lines
For j = 0 to 24
LineInput intInputFile#, arLines(j)
If EOF(intInputFile) Then
MsgBox "Unexpected End of File!"
Err.Raise something or other
End If
Next j

rst.AddNew
'Inner Loop
For j = 5 To 23 'Skip first 5 and last 1 lines
strText = value parsed out of arLines(j)
rst.Fields(j - 5).Value = strText
Next j

rst.Update
Loop
...

ERR_HANDLER:
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top