Error 2001 You cancelled previous operation problem

  • Thread starter Thread starter Jerry Foust via AccessMonster.com
  • Start date Start date
J

Jerry Foust via AccessMonster.com

I have a problem with a form that uses a parameter query. I created an If..Then statement to deal with problems of no data found in the parameter query. However, when a user presses cancel on the dialog box of the parameter search box the error "Error 2001: You canceled the previous operation" appears. If I use a do...loop statement to avoid the error I get stuck in an endless loop. The following is a copy of my code (I'm not very knowledgable at writing code).

Private Sub Form_Activate()
On Error GoTo Errorhandler:

If Me.Recordset.RecordCount = 0 Then
Do
MsgBox "No records found. Check spelling and Search again."
Me.Requery

Loop Until Me.Recordset.RecordCount > 0

Errorhandler:
Resume Next

End If

End Sub

Any help would be greatly appreciated.

Jerry
 
if Me.Recordset.EOF then
Msgbox("no records?!?")
recordset.close
exit sub
end if
-----Original Message-----
I have a problem with a form that uses a parameter
query. I created an If..Then statement to deal with
problems of no data found in the parameter query.
However, when a user presses cancel on the dialog box of
the parameter search box the error "Error 2001: You
canceled the previous operation" appears. If I use a
do...loop statement to avoid the error I get stuck in an
endless loop. The following is a copy of my code (I'm not
very knowledgable at writing code).
 
Thanks for the response. But this did not work either. I keep geeting stuck in at the Me.Requery line. The Form receives its data from a query that asks the user to input a parameter. For some reason Access does not like interrupting that activity.

Jerry
 
Jerry Foust via AccessMonster.com said:
I have a problem with a form that uses a parameter query. I created
an If..Then statement to deal with problems of no data found in the
parameter query. However, when a user presses cancel on the dialog
box of the parameter search box the error "Error 2001: You canceled
the previous operation" appears. If I use a do...loop statement to
avoid the error I get stuck in an endless loop. The following is a
copy of my code (I'm not very knowledgable at writing code).

Private Sub Form_Activate()
On Error GoTo Errorhandler:

If Me.Recordset.RecordCount = 0 Then
Do
MsgBox "No records found. Check spelling and Search again."
Me.Requery

Loop Until Me.Recordset.RecordCount > 0

Errorhandler:
Resume Next

End If

End Sub

Any help would be greatly appreciated.

Jerry

I'm not at all sure that using the form's Activate event for this is the
best idea, but I suspect you can get around your immediate problem by
amending the code like this:

'----- start amended code -----
Private Sub Form_Activate()

On Error GoTo Errorhandler

If Me.Recordset.RecordCount = 0 Then
Do
MsgBox "No records found. Check spelling and Search again."
Me.Requery
Loop Until Me.Recordset.RecordCount > 0

End If

Exit_Point:
Exit Sub

Errorhandler:
Select Case Err.Number
Case 2001, 2501 ' "Cancel" error codes
Resume Exit_Point
Case Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End Select

End Sub

'----- end amended code -----
 
Thank you for your help. I took the procedure out of the Form_Activate event and put your suggested code in the the Form_Open. Now it works perfectly.

Jerry
 
Back
Top