Function GetDatafromTextFile(ByVal strFile As String)
Dim xlsApp As Excel.Application, xlsWorkBook As Excel.Workbook, xlsSheet As Excel.Worksheet
Dim DB As Database, rs As DAO.Recordset, strUNITID As String
Dim gcount As Long
On Error GoTo ErrHandler:
10 Set DB = CurrentDb
20 Set rs = DB.OpenRecordset("Select * from [tblENASummary]", dbOpenDynaset)
30 Set xlsApp = New Excel.Application
40 Set xlsWorkBook = xlsApp.Workbooks.Open(strFile)
50 Set xlsSheet = xlsApp.Worksheets(1)
60 For i = 2 To xlsSheet.Cells(xlsSheet.Rows.count, "A").End(xlUp).ROW Step 1 ' steps through line by line evaluating the files contents
70 gcount = gcount + 1
80 SysCmd acSysCmdSetStatus, gcount
90 If i = 2 Then
100 strUNITID = ExtractedUNITID(xlsSheet.Cells(i, 1)) ' Uses a function to extract out the unit id
110 End If
' Locate the start of the data
120 If xlsSheet.Cells(i, 1) > 0 And IsNumeric(xlsSheet.Cells(i, 1)) = True Then
130 If Format(CDate(xlsSheet.Cells(i, 2)), "Short Date") = Format([Forms]![frmMain]![FileDate], "Short Date") Then
140 With rs ' Add new entry
150 .AddNew
160 ![UnitID] = strUNITID
170 ![TransactionID] = xlsSheet.Cells(i, 1)
180 ![TransactionDate] = CDate(xlsSheet.Cells(i, 2))
190 ![CCode] = xlsSheet.Cells(i, 3)
200 ![RejectOrAccept] = xlsSheet.Cells(i, 4)
210 ![EntryPoint] = xlsSheet.Cells(i, 5)
220 ![Currency] = xlsSheet.Cells(i, 6)
230 ![BillType] = xlsSheet.Cells(i, 7)
240 ![BillQuantity] = xlsSheet.Cells(i, 8)
250 .Update
260 End With
270 End If
280 End If
290 Next i
300 rs.Close
310 Set rs = Nothing
320 Set DB = Nothing
330 xlsApp.Quit
340 Set xlsSheet = Nothing
350 Set xlsWorkBook = Nothing
360 Set xlsApp = Nothing
370 Exit Function
ErrHandler:
380 If Err.Number = 1004 Then
' The file was not found
390 Err.Clear
400 Else
410 MsgBox Err.Number & " - " & Err.Description
420 End If
430 rs.Close
440 Set rs = Nothing
450 Set DB = Nothing
460 xlsApp.Quit
470 Set xlsSheet = Nothing
480 Set xlsWorkBook = Nothing
490 Set xlsApp = Nothing
End Function
Function ExtractedUNITID(strString As String) As String
Dim strTemp As String, lLocation As Long
lLocation = InStrRev(strString, "\")
strTemp = Left(strString, lLocation - 1)
lLocation = InStrRev(strTemp, "\")
strTemp = Right(strTemp, Len(strTemp) - lLocation)
ExtractedUNITID = strTemp
End Function