JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1616
|
Re: Disconnected recordset to Access database
Since Access doesn't have a BULK INSERT command (at least that I've seen in the help file), the following should do it (with a few modificatoins):
Private Sub DBImport()
On Error GoTo Err_DBImport
Dim adc As New ADODB.Connection
Dim ars As New ADODB.Recordset
Dim iFree As Integer
Dim cd() As String
Dim tmp As String
Dim i As Integer
' Open data file
iFree = FreeFile
Open "filename.txt" For Input As iFree ' the filename to input
' Open Database
adc.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1;Persist Security Info=False"
' Get first line for column names
Line Input #iFree, tmp
cd() = Split(tmp, ",") ' assuming , is the dividing char
' Set SQL start
tmp = "CREATE TABLE [ImportData]("
' Set col names based on first row
For i = LBound(cd()) To UBound(cd()) - 1
tmp = tmp & "[" & cd(i) & "] TEXT(50) , "
Next
' Set last row
tmp = tmp & "[" & cd(UBound(cd())) & "] TEXT(50))"
' Create the table
adc.Execute tmp
' Open the table
ars.Open "[ImportData]", adc, adOpenDynamic, adLockOptimistic
' Insert the remaining data
Do
Line Input #iFree, tmp
cd() = Split(tmp, ",")
ars.AddNew
For i = LBound(cd()) To UBound(cd())
ars.Fields(i) = cd(i)
Next
ars.Update
Loop Until EOF(iFree)
' CLose the file
Close
' Close the db
ars.Close
adc.Close
' Clear the vars
Set ars = Nothing
Set adc = Nothing
Err_DBImport:
MsgBox Err.Description
Set adc = Nothing
Set ars = Nothing
Close
End Sub
|