By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,863 Members | 879 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,863 IT Pros & Developers. It's quick & easy.

How do you prevent access from showing error dialog box (Runtime error 2501).

P: 16
I keep on getting the same error message and I don't know how to either solve the problem or prevent it from showing the error. I have set up a switchboard item that opens a report. When the report opens it will call a form so that the user can change parameters in the report and filter the information before the report is generated see below.


Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_Open(Cancel As Integer)
  2. DoCmd.OpenForm "CostCodeFilter", , , , acFormAdd, 3
  3.  
  4. 'Triggered object in the form will let the program know if the user has cancelled the report.
  5. If Reports![Cost Code Stats].Controls("Label14").Visible = False Then   
  6.     Cancel = True
  7. End If
  8. End Sub
  9.  
If the user closes the filter box it will cancel the report generation but for some reason it always spits out the same error message.

I have tried using error handling in the code but the problem is that the error is generated outside of the open event and the error handling is not triggered.

Thanks for help.
May 30 '12 #1

✓ answered by TheSmileyCoder

The error is being raised by line 2:
Expand|Select|Wrap|Line Numbers
  1. Function CCS_Run()
  2.   DoCmd.OpenReport "Cost Code Stats", acViewPreview
  3. End Function
  4.  
when the report is cancelled.

Check for the error, and ignore it.

Expand|Select|Wrap|Line Numbers
  1. Function CCS_Run()
  2.   On Error Goto ErrHandler
  3.   DoCmd.OpenReport "Cost Code Stats", acViewPreview
  4.  
  5. ExitFunction:
  6.   Exit Function
  7.  
  8. ErrHandler:
  9.   If Err.Number=2501 then
  10.     'User cancelled report, ignore and exit
  11.     Resume ExitFunction
  12.   Else
  13.     Msgbox Err.Number & " - " & err.Description
  14.     Resume ExitFunction
  15.   End If
  16. End Function
  17.  

Share this Question
Share on Google+
3 Replies


P: 16
Just to clarify..

The switch board runs prompts the function CCS_Run

Expand|Select|Wrap|Line Numbers
  1. Function CCS_Run()
  2. DoCmd.OpenReport "Cost Code Stats", acViewPreview
  3. End Function
That function loads the the report which has an open event

Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_Open(Cancel As Integer)
  2.  
  3. DoCmd.OpenForm "CostCodeFilter", , , , acFormAdd, 3
  4. 'Cost code filter will load a form that allows the user to filter the report.
  5.  
  6. If Reports![Cost Code Stats].Controls("Label14").Visible = False Then: Cancel = True
  7.  
  8. End Sub
I found that if I open the report from database instead of through the switch board I do not get the error message otherwise the error will come up.

Does it create problems if I have nested DoCmd.Open commands?
May 30 '12 #2

P: 16
The answer is yes it does create problems. There has to be an error handle in the function CCS_Run to catch the event that the report has been cancelled.
May 30 '12 #3

TheSmileyCoder
Expert Mod 100+
P: 2,321
The error is being raised by line 2:
Expand|Select|Wrap|Line Numbers
  1. Function CCS_Run()
  2.   DoCmd.OpenReport "Cost Code Stats", acViewPreview
  3. End Function
  4.  
when the report is cancelled.

Check for the error, and ignore it.

Expand|Select|Wrap|Line Numbers
  1. Function CCS_Run()
  2.   On Error Goto ErrHandler
  3.   DoCmd.OpenReport "Cost Code Stats", acViewPreview
  4.  
  5. ExitFunction:
  6.   Exit Function
  7.  
  8. ErrHandler:
  9.   If Err.Number=2501 then
  10.     'User cancelled report, ignore and exit
  11.     Resume ExitFunction
  12.   Else
  13.     Msgbox Err.Number & " - " & err.Description
  14.     Resume ExitFunction
  15.   End If
  16. End Function
  17.  
May 30 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.