run-time error 2501

  • Thread starter MES via AccessMonster.com
  • Start date
M

MES via AccessMonster.com

I keep getting 'runtime error 2501', and I am unsure of what code to use to
fix. Here is what I'm doing.

Click open a report, then the report 'On Open' event triggers a form to open.
This form allows user to select the parameter (in this case, the employee) to
use in the query that populates the report. All works fine, and I am able to
run the report for the employee I select. My problem is that I have a cancel
button on my select form of which my intent is to have it cancel what I'm
doing and go back to my main menu, but I am getting the runtime error. The
command I have for my cancel button is 'Close'. This is my code for both the
report and the form:

1. REPORT CODE
Private Sub Report_Close()
DoCmd.Close acForm, "Employees - Select2"
End Sub

Private Sub Report_Open(Cancel As Integer)
' Set public variable to true to indicate that the report
' is in the Open event
bInReportOpenEvent = True

' Open Employees - Select2 form
DoCmd.OpenForm "Employees - Select2", , , , , acDialog

DoCmd.Maximize

' Cancel report if user clicked the cancel button
If IsLoaded("Employees - Select2") = False Then Cancel = True

' Set public variable to false to indicate that the
' Open event is completed
bInReportOpenEvent = False

End Sub

2. FORM CODE

Private Sub Cancel_Click()
DoCmd.Close 'Close Form
End Sub

Private Sub Select_Employee_Click()
Me.Visible = False
End Sub

Can anyone tell me what my code is missing? Also, where would I put the
missing code?

Thanks in advance.
 
A

Allen Browne

The code in the Open event of the report checks to see if the form is
loaded. If not it cancel's the event. The event then notifies whatever macro
or code attempted to open the report that the attempt failed. It does this
by raising error 2051. In effect, the error message means, "Hey, whoever
asked for this report to be open: you can't keep going on the assumption
that the report opened. It's been canceled."

That means you need error handling in the event that opened the report. You
can then trap the error number and substitute your own message, or no
message at all if you wish. It will look like this:

Private Sub cmdPrint_Click()
On Error Goto Err_Handler

DoCmd.OpenReport "Report1", acViewPreview

Exit_Handler:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
MsgBox "Oops: that didn't work."
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Sub


If error handling is new, see:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html
 

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