Closing report with no data.

L

Les

OK.
I got this code from www.mvps.org, and it works great,
however, I don't want to see an error message if there is
no data, and I can't seem to get it to work without the
message. Can anyone tell me what I'm doing wrong?

'************* Code Start *************
Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data found! Closing report."
Cancel = True
End Sub
'************* Code End *************

Thanks!! You guys are the best!
 
A

Allen Browne

In the code that launches the report, trap error 2501 and ignore it.

Example:

Private Sub cmdPrint_Click()
On Error Goto Err_Handler

DoCmd.OpenReport "MyReport", acViewPreview

Exit_Handler:
Exit sub

Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & " - " & Err.Description
End If
Resume Exit_Handler
End Sub
 
N

Newbie

you need to add an error handling routine that catches error 2501 in the
same place that called the opening of the report

eg
sub whatever()
on error goto errorhandler
docmd.openreport "repname"
sExit:
exit sub
errorhandler:
select case err.number
case 2501
resume sExit
case else
msgbox err.number & " " & err.description
end select
end sub


HTH
 
S

Sugih

I have the similar case here, however I have code on the close event and I
don't want this to be executed when there is no data.
Please help, thanks.

Gix
 
A

Allen Browne

You need a way to remember that the report has no data, and read that
information in the report's Close event. A module-level boolean variable
could to that.

1. Open the report in design view. Open its module.

2. In the General Declarations section (top of the module, with the Option
statements), enter:
Private mbHasNoData As Boolean

3. In the NoData event procedure, enter:
Cancel = True
mbHasNoData = True

4. In the report's Close event, only execute the code it the boolean is
false, i.e.:
Private Sub Report_Close()
If Not mbHasNoData Then
'put your existing code in here.
End If
End Sub
 
Top