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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not to print reports when there are no records in source query 2

Status
Not open for further replies.

DBritter

Technical User
Jun 28, 2000
37
US
I have a series of mailmerge reports that run off of queries. I have a form set up to run all of the letters and their underlying queries when a button is clicked.

The problem occurs when the query comes up with no results and the letter prints anyways with "#error" in the place of the null fields.

I want to be able to print or not print based on wether there are any records in the query.
 
look at the Access documentation for the "On NoData" event. I've been having the same problem. Now I know too :)

Also see... ::)

"Cancel the printing of a report when it doesn't contain any records" also in the Access documentation.
 
If I can expand on the previous response...

For each report you want not to print if there are no records in the underlying query, place the following in the report's NoData event.
Code:
Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data available!" 
Cancel = True 
End Sub
This will work fine if you're attempting to open the report from the database window. However, if you're using a command button to trigger the print/preview event, you'll generate an error which you need to deal with. Here's an idea of how to handle it:
Code:
Private Sub CmbPrintRpt_Click()
On Error Resume Next 
DoCmd.OpenReport "Your Report", acViewPreview 
If Err = 2501 Then Err.Clear 
'more code goes here
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top