hi,
i'm trying to make some code i found in a tutorial work.
i have a mysql database, which i use with another application. i've made a new excel document with the same structure as the table in the database, and i'm trying to send the data there.
here is the code:
Dim oConn As ADODB.Connection
Private Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=temperature_monitor3;" & _
"USER=root;" & _
"PASSWORD=;" & _
"Option=3"
End Sub
Function esc(txt As String)
esc = Trim(Replace(txt, "'", "\'"))
End Function
Dim rs As ADODB.Recordset
Private Sub InsertData()
Set rs = New ADODB.Recordset
ConnectDB
With Sheet1
For rowCursor = 2 To 4
strSQL = "INSERT INTO tutorial (pid, channel_name, value, description) " & "VALUES ('" & esc(.Cells(rowCursor, 1)) & "', " & "'" & esc(.Cells(rowCursor, 2)) & "', " & esc(.Cells(rowCursor, 3)) & "', " & esc(.Cells(rowCursor, 4)) & ")"
rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
Next
End With
End Sub
the first part seems to work. doesn't return any errors at least.
The insertData sub however, returns "Run-time error 424: object required". In this case i renamed the sheet i was working with, after writing the code, and this probably caused the error.
I tried doing the whole thing all over again, in a new document, without renaming the sheet, and now the error i get is: "Run-time error '-2147217887 (80040e21)': Automation Error"
can anyone spot a problem? on the site where i got the tutorial, the comments seem to say the code works for most people, however, a few complained about the same error, and got no answer that helped.
thank you for your time
maybe i should add a link to the tutorial: http://www.heritage-tech.net/908/ins...cel-using-vba/
Bookmarks