Hi!
Another approach, one that has worked for me in the past, would be to
create a temporary import table and populate that with the file, parsing the
file on the fly. Since I cannot be sure that the file presents fixed length
records although it kind of looks like it might be.... I assume, based on
your statements that the format of this report is locked, that the format
and content will not change.
The example requires the table be in place with the appropriate data types :
tblTempImport, has columns
[Supplier Name] text 50
[Invoice No] long
[Amount] Currency (assuming English locale)
This code assumes that Microsoft DAO 3.51 or similar library is loaded
(Alt-F11, Tools, References)
The first question would be, is the file name always the same? If this is
true, then you can use a constant. Otherwise you need to acquire the path
and file name one way or another. I will assume a constant in the example.
The parsing logic won't work if you have leading spaces in front of the
Supplier Name, we are assuming that either there is a supplier name and it
is the first thing in the row of information or else we have a bunch of
spaces then the Invoice No. appears.
Hope this alternate view of how to get at the information gives you some
ideas.
The code: (aacchhhhoooooo)
========================================================
Sub ImportMyFile()
'declare constant value file path and name
Const myFile = "C:\import_folder\import_file_name.txt"
'set reference to target table
Dim rs As DAO.Recordset
Set rs = DAO.OpenRecordset("tblTempImport")
'declare working variables
Dim g As String
Dim i As Integer
Dim count As Long
Dim vData(3) As Variant
Dim gSupplier As String
Open myFile For Input As #1
'get past the header row
Line Input #1, g
Do While Not EOF(1)
'get the next row of data
Line Input #1, g
'add to the count of rows
count = count + 1
'parse out the data
vData(1) = Trim(Left(g, InStr(g, " ")))
vData(2) = Trim(Mid(g, InStr(g, " "), InStr(g, "£") - 1))
vData(3) = Trim(Mid(g, InStr(g, "£") - 1))
'if we have a new supplier change the name stored in gSupplier
If Len(vData(1)) > 0 Then gSupplier = vData(1)
'insert the current supplier name
vData(1) = gSupplier
rs.AddNew
For i = 1 To 3
rs.Fields(i - 1) = vData(i)
Next
rs.Update
Loop
Close #1
Set rs = Nothing
MsgBox "We have imported " & count & " records.", vbOKOnly, "Import
Completed"
End Sub