Cancel report if no data

  • Thread starter Thread starter Leslie Isaacs
  • Start date Start date
L

Leslie Isaacs

I want to stop a report from printing if there is no underlying data.
I have set the report's OnNoData event to Cancel=True, and this seems to
work fine, but I see on the AccessWeb (at
http://www.mvps.org/access/reports/rpt0006.htm) the following:



However, if you're opening the report from code behind a form, you need to
handle the error that's generated as a result.
'*********** Code Start ************
Private Sub TestNoData_Click()
On Error Resume Next
DoCmd.OpenReport "SomeReport", acViewPreview
If Err = 2501 Then Err.Clear
End Sub
'*********** Code End ************

I don't understand this - what is the error that is generated, and why? With
my report I'm not aware that any error is being generated - or am I missing
something.
Any help would be appreciated.
Leslie Isaacs.
 
When you code something like
Private Sub Open_Report()
DoCmd.OpenReport "rptSomething", acViewPreview, strFilter,
strSQL
End Sub

If rptSomething has no data, the On No Data Event is
triggered and the OpenReport action is cancelled.
Then on Open_Report Error 2501 happens because of this
cancellation so you should code the error handling.

hth
 
Leslie Isaacs said:
I want to stop a report from printing if there is no underlying data.
I have set the report's OnNoData event to Cancel=True, and this seems to
work fine, but I see on the AccessWeb (at
http://www.mvps.org/access/reports/rpt0006.htm) the following:



However, if you're opening the report from code behind a form, you need to
handle the error that's generated as a result.
'*********** Code Start ************
Private Sub TestNoData_Click()
On Error Resume Next
DoCmd.OpenReport "SomeReport", acViewPreview
If Err = 2501 Then Err.Clear
End Sub
'*********** Code End ************

I don't understand this - what is the error that is generated, and why? With
my report I'm not aware that any error is being generated - or am I missing
something.
Any help would be appreciated.
Leslie Isaacs.

If you have code behind a button that says "Open this report" and also have
code in the report that cancels the report, the first bit of code will
consider this an error. Specifically it is error 2501 which is raised any
time a DoCmd method is cancelled.

If you don't trap for that error number and ignore it, the user will get an
error message every time the report has no data.
 
Back
Top