Hi All
I'm trying to insert records using VBA in Access into a mysql database and I'm getting the above error. The code I'm using I have found on the web, and in all frankness don't understand it as I'm only a beginner. However the code does work if I'm using a select statement to retrieve records, but it does not work when I'm trying to do an insert statement.
Any pointers/adivce would be really appreciated! The error is thrown on statement. rs.Close
Thanks Paul
Code is
I'm trying to insert records using VBA in Access into a mysql database and I'm getting the above error. The code I'm using I have found on the web, and in all frankness don't understand it as I'm only a beginner. However the code does work if I'm using a select statement to retrieve records, but it does not work when I'm trying to do an insert statement.
Any pointers/adivce would be really appreciated! The error is thrown on statement. rs.Close
Thanks Paul
Code is
Code:
Private Sub InsertTrainers_Click()
On Error GoTo Err_InsertTrainers_Click
Dim strDatabaseName As String
Dim strDBCursorType As String
Dim strDBLockType As String
Dim strDBOptions As String
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim b As Long
Dim strSQL As String
Dim strMsg, Response As String
strMsg = "Do you want to add this event to all the trainers schedule?"
'Get confirmation of the insert into the trainers schedule.
Response = MsgBox(strMsg, vbOKCancel)
If Response = vbOK Then
strDBCursorType = adOpenDynamic 'CursorType
strDBLockType = adLockOptimistic 'LockType
strDBOptions = adCmdText 'Options
Set cn = New ADODB.Connection
cn.Open ConnectString()
With cn
.CommandTimeout = 0
.CursorLocation = adUseClient
End With
Set rs = New ADODB.Recordset 'Creates record set
strSQL = "Insert Into TrainerSchedule (TrainerID,EventID,Roleid,Statusid,TrainerSOWid,TrainerTravelid, TrainerAccomid) " _
& "Select TrainerID, EventID, 71, 21,76,80,84 From FindTrainerforEvent Where Eventid = " & [EventID]
rs.Open strSQL, cn, strDBCursorType, strDBLockType, strDBOptions
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End If
Exit_InsertTrainers_Click:
Exit Sub
Err_InsertTrainers_Click:
MsgBox Err.Description
Resume Exit_InsertTrainers_Click
End Sub
Private Function ConnectString() As String
Dim strServerName As String
Dim strDatabaseName As String
Dim strUserName As String
Dim strPassword As String
'Change to IP Address if not on local machine
'Make sure that you give permission to log into the
'server from this address
'See Adding New User Accounts to MySQL
'Make sure that you d/l and install the MySQL Connector/ODBC 3.51 Driver
strServerName = "localhost"
strDatabaseName = "EventPlanner"
strUserName = "XXXXXX"
strPassword = "XXXXXXX"
ConnectString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=" & strServerName & _
";DATABASE=" & strDatabaseName & ";" & _
"USER=" & strUserName & _
";PASSWORD=" & strPassword & _
";OPTION=3;"
End Function
Comment