Can't suppress error msg when canceling form open

J

John S. Ford, MD

I have a dialog box that allows users to enter criteria that are used to
select records on another form. I use the following code to prevent the
form from opening if there are no records meeting the chosen criteria:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Error
If Me.Recordset.RecordCount = 0 Then
Cancel = True
MsgBox "No admissions meet these criteria."
End If
CompletedRoutine:
Exit Sub
Err_Error:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox Err.Number & Err.Description
Resume CompletedRoutine
End If
End Sub

The message box comes up as planned but I still get the 2501 error (The Open
Form action was canceled). Why is my code not suppressing this error
message?

Thanks in advance!

John
 
P

Paul Shapiro

If I remember correctly, the 2501 error should be raised in the routine that
tried to open the form, not in the form itself.

An easy way to know where the error occurs is to add the form and routine
names to the error message:
MsgBox "Module" & Me.Name & ".Form_Open: " & "Error #" & Err.Number & ":
" & Err.Description

I use a module-wide constant with the module name in every module, instead
of the Me.Name which works for a form or report, but may not work in a code
module.

Or use the debugger to step through your code.
 
A

Albert D. Kallal

It is the code that "calls" the form that is giving you that error msg.

You could use:

Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.RecordCount = 0 Then
Cancel = True
MsgBox "No admissions meet these criteria."
end if
End Sub

The code to "open" the above form would be:

on error resume next
docmd.OpenForm "name of above form"

The code you have with error handling is still a good thing, I just wanted
to show that you don't need a lot of error handling in this case and it is
your "calling" code that opens the form that is erroring out here...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top