By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,940 Members | 611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,940 IT Pros & Developers. It's quick & easy.

nodata event / still printing form data

P: 19
I have a report that I run from a Macro. This macro executes when you click the printer icon button on the form. The macro is simple: 1. open report 2. printout 3. close report . All this works fine - except when I do not want to print the report if no data exist. I have the nodata event with cancel = true. It works but, a print is generated that looks like a snapshot of the form with the printer icon with the 2 test records. Why is that printing? How can I get rid of it? Thanks in advance.

The report has this event procedure:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_NoData(Cancel As Integer)
  2. Cancel = True
  3. '
  4. MsgBox ("report cancelled")
  5. End Sub
Feb 26 '09 #1
Share this Question
Share on Google+
4 Replies


DonRayner
Expert 100+
P: 489
Your problem is being caused because of the macro. The cancel event on the report doesn't stop the macro from running, it just stops the form from opening. The print command still runs and since the form is now active instead of the report it is printing the form.

Instead of using a Macro to print the report you need to open it directly from the command button that you click to print. Assuming your button is named print the vba for the on click event would be as follows. Just replace ReportName with the actual name of your report.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Print_Click()
  2. On Error GoTo ErrPoint
  3. DoCmd.OpenReport "ReportName", acViewNormal
  4.  
  5. ExitPoint:
  6. Exit Sub
  7.  
  8. ErrPoint:
  9. If Err.Number = 2501 Then
  10.     MsgBox "Report Cancelled"
  11.     Resume ExitPoint
  12. Else
  13.     MsgBox Err.Number & " " & Err.Description
  14. End If
  15. End Sub
The cancel event generates error number 2501 that you can trap and use to display your message box.
Feb 26 '09 #2

Expert Mod 2.5K+
P: 2,545
Hi. I guess from what you say about the steps performed by your macro it opens the report in print preview mode, then uses printout as the next macro action. The cancel event will leave you with no report to print, as it closes the open report immediately (there being nothing to print preview).

If I am right about this you can instead send the report directly to print and take out your printout and close macro actions. The print and print preview options are under View in your open report macro action.

-Stewart
Feb 26 '09 #3

P: 19
Don and Stewart - much thanks for your response. I have tried to like Macros but they can be a pain - like this example has shown. Here is the final solution taken from Don's code. There are actually 4 reports run at the same time, but only the 4th will have no data sometimes. A Query is done for reports 1-3 and another done for report 4 (which may have scanned documents or not). It was the ChemicalReport4ScanDoc that has the nodata event. So the final analysis is: the Macro was creating extra print after the report was cancelled with the nodata event.
Expand|Select|Wrap|Line Numbers
  1. Public Sub ChemicalPrintSub()
  2.     On Error GoTo ErrPoint
  3. '
  4. DoCmd.OpenReport "ChemicalReport1", acViewNormal
  5. DoCmd.OpenReport "ChemicalReport2", acViewNormal
  6. DoCmd.OpenReport "ChemicalReport3", acViewNormal
  7. DoCmd.OpenReport "ChemicalReport4ScanDoc", acViewNormal
  8. '
  9. ExitPoint:
  10. Exit Sub
  11.  
  12. ErrPoint:
  13. If Err.Number = 2501 Then
  14.     Resume ExitPoint
  15. Else
  16.     MsgBox Err.Number & " " & Err.Description
  17. End If
  18. '
  19. End Sub
Feb 26 '09 #4

DonRayner
Expert 100+
P: 489
You're quite welcome, good luck with your project.

Don
Feb 27 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.