Report On NoData Event

Eljefegeneo

Still trying to learn
Local time
Today, 09:59
Joined
Jan 10, 2011
Messages
902
I have the following code to print two reports :
Code:
  Dim rpt, rpt1, cond As String
  rpt = "rptEmailReportCurrentMonth"
  rpt1 = "rptEmailReportCurrentMonthReceived"
  cond = "[agentID] = Forms!frmAgentEmailsByAgent.AgentID"
   
  DoCmd.OpenReport rpt, acViewNormal, , cond
      DoCmd.OpenReport rpt1, acViewNormal, , cond
And for the NoData Event of the report I have
Code:
  Cancel = True
Error message 2501 pops up saying that the open report event was cancelled. If I add line

Code:
  On error resume next
All is OK, but that really does not tell me why I was getting the error message.
 
In essence you're getting the error because you told Access to open a report and it couldn't, so it's telling you it couldn't. It is normally handled with an error trap:

http://www.baldyweb.com/ErrorTrap.htm

In this case you could eliminate the error message for that code and tell it to resume next instead of going to the exit handler.
 
Thank you. I understand what you are saying but then I don't. If the accepted way of cancelling a report is to put
Code:
  Cancel = True
In the On NoData event of the report, then why does an error message appear? Does this mean that I should also have some code in the On NoData event to close the report?
And the link you provided is great, but I also have a sequence where I print about a dozen similar reports differing only in the "cond" listed, looping through a record set which is a query.
If I used the code in the link, setting the rs and db to nothing would stop all additional reports from printing.
So, should I just use the
Code:
  On Error Resume Next
  If Err = 2501 Then Err.Clear
When printing the series of reports?
 
The way to handle that with a series of reports, and maintain traditional error handling would be:

Code:
    Case 2501       'Action OpenReport was cancelled.
      DoCmd.Hourglass False
      Resume Next

That should make it go to the next one, but still have active error handling if something else causes an error other than 2501.
 
No problem. I think I skipped over "why does an error message appear". Yes, using Cancel = True in the report's no data event is the normal way of handling reports with no data. The error comes from the code that tried to open the report, not the report itself. The best I can describe it (because I don't know the inner workings myself) is to say the code trying to open the report gives you an error message because it couldn't open a report you told it to open. With properly structured error handling, you can handle that error appropriately. Normally an error message so the user knows why their report didn't open, in this case silently moving on to the next one.
 

Users who are viewing this thread

Back
Top Bottom