Using Access 2010 have the following code on a Control Box's NotInList Event.
When the user selects NO from the popup asking to add new item the following error apprears: "Run-Time error '91' Object variable or With Block varialble not set"
It points to this line in the code:
[bold]If cnn.Errors.Count > 0 Then [/bold]
THanks,
When the user selects NO from the popup asking to add new item the following error apprears: "Run-Time error '91' Object variable or With Block varialble not set"
It points to this line in the code:
[bold]If cnn.Errors.Count > 0 Then [/bold]
THanks,
Code:
Private Sub cboVehiculoMarca_NotInList(NewData As String, Response As Integer)
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim lngRecs As Long
Dim strError As String
Dim lngError As Long
Dim strMsg As String
On Error GoTo ErrorHandler
strMsg = "'" & NewData & "' no esta en la lista de Marcas. "
strMsg = strMsg & "Desea crearla?"
If vbNo = MsgBox(strMsg, vbYesNo + vbQuestion, _
"Nueva Marca") Then
'Response = acDataErrDisplay
Me.cboVehiculoMarca.SetFocus
Else
Set cnn = New ADODB.Connection
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn
cnn.Open adhcADH2002SQLCnn
cmd.CommandText = "proInsertarMarca"
cmd.CommandType = adCmdStoredProc
' Check that CustomerId, LastName, and
' FirstName are completed.
If IsNull(NewData) Or _
NewData = vbNullString Then
MsgBox "Descripción de Marca es campo obligatorio", _
vbCritical + vbOKOnly, Me.Caption
Me.cboVehiculoMarca.SetFocus
GoTo ExitHere
End If
' Append parameters to the Command
' object's Parameters collection.
Set prm = cmd.CreateParameter("Marca", _
adVarChar, adParamInput, 50, NewData)
cmd.Parameters.Append prm
cmd.Execute RecordsAffected:=lngRecs, _
Options:=adExecuteNoRecords
Response = acDataErrAdded
' Check if stored proc worked.
If lngRecs <> 0 Then
MsgBox "Registro Creado.", _
vbInformation + vbOKOnly, Me.Caption
Else
MsgBox "No fue posible crear Registro.", _
vbCritical + vbOKOnly, Me.Caption
End If
End If
ExitHere:
Set cmd = Nothing
Set prm = Nothing
cnn.Close
Set cnn = Nothing
Exit Sub
ErrorHandler:
' Grab Access/VBA Error
lngError = Err.Number
strError = Err.Description
' If ADO Error available grab
' the first one in the collection.
If cnn.Errors.Count > 0 Then
lngError = cnn.Errors(0).NativeError
strError = cnn.Errors(0).Description
End If
MsgBox "Error " & lngError & ": " & _
strError, vbCritical + vbOKOnly, _
"frmCustomer Error"
Resume ExitHere
End Sub