I'm trying to transition from DAO to ADO, but am having difficulties understanding the statement differences. The code below works fine for a cbo "Not in List" event using DAO . . . I have left the old code commented out to illustrate what I am doing.
The (new) code returns an "Object variable or with block variable not set" error.
Thanks!
- - -
Private Sub Category_NotInList(NewData As String, response As Integer)
'Dim db As Database, Rs As Recordset 'old line
Dim Cnn As New ADODB.Connection 'new line
Dim Rs As ADODB.Recordset 'new line
On Error GoTo Err_Category_NotInList
Dim strMsg As String
strMsg = "'" & NewData & "' is not a valid Category. "
strMsg = strMsg & "Do you want to add the new Category to the list? "
strMsg = strMsg & "'Yes' to add or 'No' to retry!"
If MsgBox(strMsg, vbQuestion + vbYesNo, "Add New Category?"
= vbNo Then
response = acDataErrContinue
Else
'Set db = CurrentDb 'old line
'Set rs = db.OpenRecordset("tblCategoryList", dbOpenDynaset) 'old line
Set Cnn = CurrentProject.Connection 'new line
Rs.Open "tblCategoryList", Cnn 'new line
Rs.AddNew
Rs!Category = NewData
Rs.Update
End If
Exit_Category_NotInList:
Exit Sub
Err_Category_NotInList:
MsgBox Err.Description
Resume Exit_Category_NotInList
End Sub
- - - -
Bry
The (new) code returns an "Object variable or with block variable not set" error.
Thanks!
- - -
Private Sub Category_NotInList(NewData As String, response As Integer)
'Dim db As Database, Rs As Recordset 'old line
Dim Cnn As New ADODB.Connection 'new line
Dim Rs As ADODB.Recordset 'new line
On Error GoTo Err_Category_NotInList
Dim strMsg As String
strMsg = "'" & NewData & "' is not a valid Category. "
strMsg = strMsg & "Do you want to add the new Category to the list? "
strMsg = strMsg & "'Yes' to add or 'No' to retry!"
If MsgBox(strMsg, vbQuestion + vbYesNo, "Add New Category?"
response = acDataErrContinue
Else
'Set db = CurrentDb 'old line
'Set rs = db.OpenRecordset("tblCategoryList", dbOpenDynaset) 'old line
Set Cnn = CurrentProject.Connection 'new line
Rs.Open "tblCategoryList", Cnn 'new line
Rs.AddNew
Rs!Category = NewData
Rs.Update
End If
Exit_Category_NotInList:
Exit Sub
Err_Category_NotInList:
MsgBox Err.Description
Resume Exit_Category_NotInList
End Sub
- - - -
Bry