Question : Access VBA Import Text File to access

I am trying to import data from a text file into access table, the text file format is the issue (I do not have the option of changing the output format). The information below is the text file data (also attached), the "2nd" row starting with Emp is also part of the record, but always is onthe row below. I tried also to import into Excel, but trying al of the delimeter options either seperated the date into seperate fields or seperated the data in the second row.
Bottom line - I am needing to import from the text file into the fields listed [Time, Acct, Type, Amt, Tip] and also the next line (which does not have field names, but would be Emp, Check, Tender) which is part of the record
 
Any help is appreciated

Time                           ACCT    TYPE      AMT     TIP  PRINT PURGE
----------------------------  -----  --------  ------  ------ ----- -----
02:34:45 AM Thu Dec 10, 2009  21415   FORWARD    0.00    0.00 FALSE FALSE
  Emp   1 Check         0 Tender  0
12:06:08 AM Thu Dec 10, 2009  21415    CHARGE  333.54   30.39 FALSE FALSE
  Emp 901 Check   4194335 Tender 25
12:10:39 AM Thu Dec 10, 2009  21415    CHARGE  207.37   20.00 FALSE FALSE
  Emp 901 Check   4194336 Tender 25

Answer : Access VBA Import Text File to access

Here is code.  Open a new module, and paste two functions there.  Edit ffile to reflect location of house.txt

Function GetHouse()
Dim a As Variant
Dim b As Variant
Dim vstring As String
Dim vstr2 As String
Dim vstr As String
Dim vdtg As Date
Dim vacct As Long
Dim vtype As String
Dim vamt As Currency
Dim vtip As Currency
Dim vprint As String
Dim vpurge As String
Dim vemp As Long
Dim vcheck As Long
Dim vtender As Long
ffile = "Yourdrive:\yourpath\house.txt"
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("house")
Open ffile For Input As #1
Do While Not (EOF(1))
Line Input #1, vstring
If vstring Like "*TIME*" Or vstring Like "*----*" Or vstring = "" Then
GoTo loopit
End If
vstring = Replace(vstring, ",", "")
vstring = Onespace(vstring)
If Not (vstring Like "*EMP*") Then
a = Split(vstring, " ")
vdtg = CDate(a(4) & " " & a(3) & " " & a(5) & " " & a(0) & " " & a(1))
Debug.Print vdtg
vacct = a(6)
vtype = a(7)
vamt = a(8)
vtip = a(9)
vprint = a(10)
vpurge = a(11)
Line Input #1, vstring
vstring = Onespace(vstring)
b = Split(Trim(vstring), " ")
vemp = b(1)
vcheck = b(3)
vtender = b(5)
rs.AddNew
rs!dtg = vdtg
rs!account = vacct
rs!Type = vtype
rs!amt = vamt
rs!tip = vtip
rs!Print = vprint
rs!purge = vpurge
rs!EMP = vemp
rs!CHECK = vcheck
rs!TENDER = vtender
rs.Update
End If
loopit:
Loop
Close #1
Set rs = Nothing
End Function

Function Onespace(vstr As String) As String
Do Until InStr(vstr, "  ") = 0
vstr = Replace(vstr, "  ", " ")
Loop
Onespace = vstr
End Function
Random Solutions  
 
programming4us programming4us