I've been working in this code and I don't know whats wrong with the code.
Everything works fine until it get to execute the query.
Here's the code:
Dim i As Integer
ConnectDB
lastrow = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "H").End(xlUp).Offset(0, 0).Row
MsgBox (lastrow)
For i = 2 To lastrow
get_id = Sheet1.Cells(i, 8).Value
col1 = Sheet1.Cells(i, 9).Value
col2 = Sheet1.Cells(i, 10).Value
col3 = Sheet1.Cells(i, 11).Value
mySQL = "UPDATE pegawai SET nama = '" & col1 & "', alamat = '" & col2 & "', tlp = " & col3 & " where id = " & get_id & ""
MsgBox (mySQL)
oConn.Execute (mySQL) <---- ERROR HERE
Set oConn = Nothing
Next i
oConn.Close
When I run this code I get an error object variable or with block variable not set. And when I add
Set oConn = New ADODB.Connection
it produces an error
Operation is not allowed when the object is closed
and this the code for my connection
Dim oConn As ADODB.Connection
Dim rs As ADODB.Recordset
Public Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=belajar;" & _
"USER=root;" & _
"PASSWORD=;"
End Sub
Why does it get wrong? I appreciate when you all could help.