B
Braveheart
I have a CSV file which I want to import to a pre-exixting table structure.
I believed that this could be accomplished using the code below, but when I
view the data afterwards, the first record from the CSV file has been
ignored. I am probably doing something really dumb, but any help would be
appreciated.
Here's the code:
Public Function ImportFile(strFilename, strTableName, intNumRecs)
Dim strMsg, strFileOnly, strPathOnly As String
Dim slashPos, I, intOutCount, numRead As Integer
Dim outConn, inpConn As Connection, inpRs, outRs As Recordset
Dim connStr As String
slashPos = InStrRev(strFilename, "\", , vbTextCompare)
strFileOnly = Right(strFilename, Len(strFilename) - slashPos)
strPathOnly = Left(strFilename, slashPos)
MsgBox "[" + strFileOnly + "][" + strPathOnly + "]"
Set inpConn = CreateObject("ADODB.Connection")
Set outConn = CurrentDb()
Set inpRs = CreateObject("ADODB.Recordset")
Set outRs = CreateObject("ADODB.Recordset")
inpConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=" + strPathOnly + ";" & _
"Extensions=asc,csv,tab,txt"
inpRs.Open "SELECT * FROM " + strFileOnly, _
inpConn, adOpenStatic, adLockReadOnly, adCmdUnspecified
outRs.Open strTableName, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
intOutCount = outRs.RecordCount
If intOutCount > 0 Then
With outRs 'empty current contents of the table
outRs.MoveLast
outRs.MoveFirst
Do Until .EOF
.Delete
.Update
.MoveNext
Loop
End With
End If
With inpRs
numRead = 0
Do Until .EOF
outRs.AddNew
numRead = numRead + 1
For I = 0 To .Fields.Count - 1
outRs.Fields(I) = .Fields(I)
Next I
outRs.Update
.MoveNext
Loop
intNumRecs = outRs.RecordCount
End With
Set outRs = Nothing
Set inpRs = Nothing
End Function
I believed that this could be accomplished using the code below, but when I
view the data afterwards, the first record from the CSV file has been
ignored. I am probably doing something really dumb, but any help would be
appreciated.
Here's the code:
Public Function ImportFile(strFilename, strTableName, intNumRecs)
Dim strMsg, strFileOnly, strPathOnly As String
Dim slashPos, I, intOutCount, numRead As Integer
Dim outConn, inpConn As Connection, inpRs, outRs As Recordset
Dim connStr As String
slashPos = InStrRev(strFilename, "\", , vbTextCompare)
strFileOnly = Right(strFilename, Len(strFilename) - slashPos)
strPathOnly = Left(strFilename, slashPos)
MsgBox "[" + strFileOnly + "][" + strPathOnly + "]"
Set inpConn = CreateObject("ADODB.Connection")
Set outConn = CurrentDb()
Set inpRs = CreateObject("ADODB.Recordset")
Set outRs = CreateObject("ADODB.Recordset")
inpConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=" + strPathOnly + ";" & _
"Extensions=asc,csv,tab,txt"
inpRs.Open "SELECT * FROM " + strFileOnly, _
inpConn, adOpenStatic, adLockReadOnly, adCmdUnspecified
outRs.Open strTableName, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
intOutCount = outRs.RecordCount
If intOutCount > 0 Then
With outRs 'empty current contents of the table
outRs.MoveLast
outRs.MoveFirst
Do Until .EOF
.Delete
.Update
.MoveNext
Loop
End With
End If
With inpRs
numRead = 0
Do Until .EOF
outRs.AddNew
numRead = numRead + 1
For I = 0 To .Fields.Count - 1
outRs.Fields(I) = .Fields(I)
Next I
outRs.Update
.MoveNext
Loop
intNumRecs = outRs.RecordCount
End With
Set outRs = Nothing
Set inpRs = Nothing
End Function