Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thank you. It's already helped me greatly, and I enjoy just reading the inputs from the other members..."

Geography

Where in the world do Tek-Tips members come from?

Issue with error trap getting Runt-time Error "91"Helpful Member! 

Prospec (TechnicalUser)
11 May 12 15:28
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:
If cnn.Errors.Count > 0 Then

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
Andrzejek (Programmer)
11 May 12 16:03

If your user says "No" to your question: "Desea crearla?" and
Me.cboVehiculoMarca.SetFocus creates an error (let's say cboVehiculoMarca is not visible or enabled), you go to ErrorHandler: where you check for cnn.Errors.Count but cnn was never Set sad

That's my guess....

Have fun.

---- Andy

Andrzejek (Programmer)
11 May 12 16:09

You may want to add to your code:

CODE

If Not(cnn Is Nothing) Then If cnn.Errors.Count > 0 Then lngError = cnn.Errors(0).NativeError strError = cnn.Errors(0).Description End If End If

Have fun.

---- Andy

Prospec (TechnicalUser)
11 May 12 16:24
Would the follwoing code be the correct to set it?

CODE

Set cnn = New ADODB.Connection

Prospec (TechnicalUser)
11 May 12 16:42
Placed the code as suggested but sill get the run-time error.
Andrzejek (Programmer)
11 May 12 20:30

When you step thru your code, which line of code couses to go to ErrorHandler:?

Have fun.

---- Andy

Prospec (TechnicalUser)
14 May 12 14:56

Hi Andrzejek,

CODE

ExitHere: Set cmd = Nothing Set prm = Nothing cnn.Close <--------- E R R O R THIS LINE IS THE ON THAT CAUSES TO GO TO ERRORHANDLER. 88888888888 Set cnn = Nothing Exit Sub ErrorHandler: ' Grab Access/VBA Error lngError = Err.Number strError = Err.Description <--------- GOES TO THIS LINE. 88888888888 ' 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
Helpful Member!  Andrzejek (Programmer)
14 May 12 15:20

Looks like you are trying to close your connection cnn, but you never set it up or opened it smile

You may try something like:

CODE

Set cmd = Nothing Set prm = Nothing If Not(cnn Is Nothing) Then If cnn.State = 1 Then cnn.Close End If End If Set cnn = Nothing

I think State = 1 is Open, State = 0 is Closed

Have fun.

---- Andy

Prospec (TechnicalUser)
14 May 12 16:05
Great that fixed it thank you very mucho!!!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close