Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Prevent Printing Report With No Data 6

Status
Not open for further replies.

MachineBoy

Programmer
Feb 18, 2002
10
0
0
US
I am getting a Run Time Error 2501 when I try to cancel a report from being written when there is no data generated by a query. Is there a way to prevent this? I am using Microsoft Access 95. I have tried trapping the error to no avail.
 
I have not used Access 95 in a while but check to see if the report has an Event called "On No Data" if so write your code here as to what should happen when no data is presented.

Good Luck
ssecca
 
You need to error trap for error 2501 in the event where you open the report (Where the docmd.openreport resides)
 
Thank both of you for the response. Could y'all give me an example of the code for each method? I have tried both and still am not successful. My report uses a parameter query and if the query produces no data then I get that run-time error 2501. I have coded using the OnNoData property in the report module and tried to trap the error where I DoCmd.OpenReport.
 
Insert this code behind the button you use to open the report (substituting in the appropriate names):

Private Sub cmdButtonName_Click()
On Error GoTo Err_cmdButtonName

DoCmd.OpenReport "rptName", acViewPreview

Exit_cmdButtonName:
Exit Sub

Err_cmdButtonName:

' This traps the error message
If Err = 2501 Then
Resume Exit_cmdButtonName

Else
MsgBox Err.Description
Resume Exit_cmdButtonName

End If

End Sub

Insert this code in the On NoData event procedure of the report:

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ErrorHandler

MsgBox "There are currently no records", vbOKOnly

Cancel = True

ExitReport:
Exit Sub

ErrorHandler:
MsgBox Err.Description
Resume ExitReport

End Sub
Linda Adams
Visit my web site for writing and Microsoft Word tips: Official web site for actor David Hedison:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top