Option Compare Database
Option Explicit
Dim fieldName As String, fieldLen As Integer
Function ImportFromFile(FileName As String)
Dim db As DAO.Database, rst As DAO.Recordset
Dim strRecord As String, arrayFields() As String, xCol As Integer, i As Integer, aIndex As Integer
Set db = CurrentDb
Set rst = db.OpenRecordset("[COLOR=Red]TheTableToFillWithTheImportData[/COLOR]")
' Initialise the array
ReDim arrayFields(0)
Open FileName For Input As #1 ' Open the import file.
While Not EOF(1)
Line Input #1, strRecord
If Len(strRecord & "") > 0 Then
' Increase the size of the array if needs be and preserve old values
If Int(Len(strRecord) / 5) > UBound(arrayFields) + 1 Then
ReDim Preserve arrayFields(Int(Len(strRecord) / 5) - 1)
End If
aIndex = 0
' Save each part of the record into the array
For i = 1 To Len(strRecord)
aIndex = aIndex + 1
SetFieldVal Mid(strRecord, i, 5)
arrayFields(aIndex) = Mid(strRecord, i, fieldLen)
i = i + (fieldLen - 1)
Next
With rst
.AddNew
For xCol = 0 To aIndex
SetFieldVal Left(arrayFields(xCol), 5)
If fieldName <> "" Then
.Fields(fieldName) = Trim(arrayFields(xCol))
End If
Next
.Update
End With
End If
Wend
Close #1
rst.Close
Set rst = Nothing
MsgBox "Successfully Imported!"
End Function
Function SetFieldVal(fieldID As String)
Select Case fieldID
Case "AC120"
'-- Name Field
fieldName = "[COLOR=Red]NameField[/COLOR]"
fieldLen = [COLOR=Red]25[/COLOR]
Case "AC130"
'-- Address field
fieldName = "[COLOR=Red]AddressField"[/COLOR]
fieldLen = [COLOR=Red]25[/COLOR]
Case "BC120"
'-- Shipping Address
fieldName = "[COLOR=Red]ShippAddField[/COLOR]"
fieldLen = [COLOR=Red]25[/COLOR]
Case "BC130"
'-- Bankruptcy Info
fieldName = "[COLOR=Red]BankField[/COLOR]"
fieldLen = [COLOR=Red]25[/COLOR]
Case "AF120"
'-- Connection Status
fieldName = "[COLOR=Red]ConnStatField[/COLOR]"
fieldLen = [COLOR=Red]25[/COLOR]
Case Else
'-- Unknown field identity
fieldName = ""
fieldLen = 0
End Select
End Function