cancel print dialog on NoData

supmktg

Registered User.
Local time
Today, 17:38
Joined
Mar 25, 2002
Messages
360
I have a print button that opens a report and then opens the print dialog box for printer selection and printing.

It works great using the following code:

DoCmd.OpenReport "MyReport", acViewPreview, acEdit
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "MyReport"

On NoData, I have a message box saying no records, but then the Print Dialog Box still pops up!

This is the code on my reports NoData event:

Private Sub Report_NoData(Cancel As Integer)

MsgBox "There are no records for your selection.", vbOKOnly, " No Records Returned"
Cancel = True

End Sub

Is there code I can add to the Print Button or the NoData event that would bypass the Print Dialog Box?

Thanks

Supmktg
 
Could you declare a public variable, eg EmptyReport and set this to true in the nodata event of the report.

You could then test this with an if statement in your print event and only call the print command if emptyrreport is false.

You would need to set emptyreport to false before callling the report, or it might not call the print command when the report has data.

Hope this helps

Sue Powell
 
Pat,

I'm going to eliminate the 2 extra lines of code, thanks! I want to cancel the print event if there is no data, and inform the user of no records to print. How do I test to see if the recordset is empty?

Thanks,

Supmktg
 
Pat,

I misunderstood your suggestion. I now realize that you meant to change the acPreview to acPrintAll.

I want to see the Dialog box so I can select a printer etc.

I still think I can solve my problem with a record count check by adding:

If record count = 0 then
DoCmd.Close acReport, "MyReport"
Else
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "MyReport"

My question is, how do I get the record count, and will this work?

Thanks,

Supmktg
 
On Error Resume Next
DoCmd.OpenReport "MyReport", acViewPreview

on the command button


In the NoData event of the Report
Cancel=True
MsgBox" There is no data for the period selected"

In the OnActivate event of the report
DoCmd.RunCommand acCmdPrint


The No data event will prevent the Dialog box from opening if the recordset's empty
 
Cancel Print

Rich said:
On Error Resume Next
DoCmd.OpenReport "MyReport", acViewPreview

on the command button


In the NoData event of the Report
Cancel=True
MsgBox" There is no data for the period selected"

In the OnActivate event of the report
DoCmd.RunCommand acCmdPrint


The No data event will prevent the Dialog box from opening if the recordset's empty

This code is really useful but what can I add to it to prevent the code stopping if the user decides against printing after all? I get the print dialogue box and if I click cancel the code stops at the OnActivate event.

I know the answer is prob really easy but my brain is not working today!!

Thanks
 

Users who are viewing this thread

Back
Top Bottom