|
-
December 14th, 2009, 03:34 PM
#1
CSV Reading using ADO wont delimit.
Hey,
I've got the following code:
Code:
Sub processImport()
Set fileConn = connectCsv()
Set rs = fileConn.Execute("SELECT * FROM import.csv")
If rs.EOF And rs.BOF Then
outLine ("No Records Found")
Call enableLauncher
Exit Sub
End If
Do Until rs.EOF
conn.Execute (buildImportQuery(rs))
rs.MoveNext
Loop
fileConn.Close
End Sub
Function connectCsv() As Object
Dim csvConn As Object
Set csvConn = CreateObject("ADODB.Connection")
csvConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & path & "\data\;" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
'csvConn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
' "Dbq=" & path & "\data\" & _
' "Extensions=asc,csv,tab,txt"
Set connectCsv = csvConn
End Function
Function buildImportQuery(line)
Dim queryParts(2) As String, _
maxFields As Byte, _
key As Variant, _
fields As Object
Set fields = CreateObject("Scripting.Dictionary")
If line.fields.Count >= 0 Then
Call fields.Add("videoid", line.fields(0))
End If
If line.fields.Count >= 1 Then
Call fields.Add("title", line.fields(1))
End If
If line.fields.Count >= 2 Then
Call fields.Add("done", line.fields(2))
End If
If line.fields.Count >= 3 Then
Call fields.Add("disabled", line.fields(3))
End If
If line.fields.Count >= 4 Then
Call fields.Add("moderated", line.fields(4))
End If
If line.fields.Count >= 5 Then
Call fields.Add("failed", line.fields.fields(5))
End If
For Each key In fields.Keys
If fields(key) <> False Then
queryParts(0) = queryParts(0) & key & ","
queryParts(1) = queryParts(1) & "'" & Replace(Replace(fields(key), "'", "''"), "#COMMA#", ",") & "',"
End If
Next
buildImportQuery = "INSERT INTO videos (" & Left(queryParts(0), Len(queryParts(0)) - 1) & ") VALUES (" & Left(queryParts(1), Len(queryParts(1)) - 1) & ");"
End Function
And the following text file (import.csv)
Code:
test1,test2
test3,test4
But somehow it doesn't split and I get an error saying line.fields(1) is inaccessible, and when showing the output of line 0 it shows "test1,test2". so I've got no idea what I'm doing wrong, probably something stupid I hope you can help me out.
Thank you for your time, Xeross
Last edited by xeross; December 14th, 2009 at 03:55 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|