Hi,
I Have some difficulties importing a comma-separated text-file using
the code below.
It doensn't work when the separator used is the comma (",").
It seems to be a conflict because the comma is also used as decimal
sign (W2k, Dutch)
When i try to import test1.csv (see below code) using this function i
get this error message:
"Item cannot be found in the collection corresponding to the requested
name or ordinal."
When i import test2.csv, using ";" as separator, everything works
fine.
I'm sure the code is OK , got it from MSDN, and ExpertsExchange.
Anyone can tell me how to handle a separator that is also being used
as decimal sign?
Thanks a million!
Jan
Public Function ReadMPBNLCsv(strFile As String)
Dim strConnect As String
Dim strDir As String
Dim strTest As String
Dim adoText As ADODB.Connection
Dim adoAccess As ADODB.Connection
Dim rcdText As ADODB.Recordset
Dim SQLtxt As String
Set adoText = New ADODB.Connection
Set adoAccess = New ADODB.Connection
Set rcdText = New ADODB.Recordset
strDir = DirectoryFromFile(strFile)
'Set up the connection for the text file.
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source="
& strDir & ";" & "Extended
Properties=""text;HDR=YES;FMT=CSVDelimited"""
adoText.Open strConnect
'Set up the connection for the Access DB.
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data " & "Source="
& AppPath() & "mpbm.mdb" & ";Persist Security Info=False"
adoAccess.Open strConnect
'Open text to a recordset.
rcdText.Open "SELECT * FROM mpbnl.csv", adoText, adOpenStatic,
adLockReadOnly, adCmdText
While Not rcdText.EOF
strTest = "INSERT INTO [test] ([a], [b], [c], [d]) VALUES " &
"('" & rcdText![a] & "', " & "'" & rcdText![b] & "', " & "'" &
rcdText![c] & "', " & "'" & rcdText![d] & "');"
txtImportLog.Text = strTest
MsgBox "OK"
rcdText.MoveNext
Wend
rcdText.Close
adoText.Close
adoAccess.Close
Set rcdText = Nothing
Set adoText = Nothing
Set adoAccess = Nothing
End Function
Test1.csv:
---------------------------
"a","b","c","d"
"1","2","3","4"
"5","6","7","8"
"9","10","11","12"
Test2.csv:
---------------------------
"a";"b";"c";"d"
"1";"2";"3";"4"
"5";"6";"7";"8"
"9";"10";"11";"12"
>> Stay informed about: VB6: import CSV using ADO, Regional settings problem?