Handling Misleading Report Errors

<< Click to Display Table of Contents >>

Navigation:  Smart Access 1996-2006 > Jun-1997 >

Handling Misleading Report Errors

Ken Getz            
 
In my application, I allow users to print a report. I use DoCmd OpenReport to open the report, and things usually work fine. However, if the user cancels the print job, I get an ugly error message (2501: Action OpenReport was cancelled). How can I allow the user to cancel the report without seeing that error message?
 
You can do what most people do: tell them "Don't do that!" Just kidding. The solution is quite simple, although not what you might expect. With most macro actions (or methods of the DoCmd object, in Access 95 and 97), you can use the SetWarnings action (or method) to disable warnings like this. That is, you'd expect code such as the following to handle the problem:
 

' Code from frmPrintReport1

DoCmd SetWarnings False

DoCmd OpenReport "Catalog"

DoCmd SetWarnings True

 
 
This still allows the error to occur. To test this out, open frmPrintReport1, click the command button, and cancel the report as soon as it starts up. The error is hard to miss!
 
The solution, then, is to use Access' error handling. Because canceling a report triggers a runtime error, the only way to trap the error is to engage an error trap. That is, you can either have Access disregard errors altogether, or handle the error gracefully, using your own code.
 
It's simple to convince Access to disregard errors totally: to do so, simply use the On Error Resume Next statement in your code. The sample form, frmPrintReport2, uses this technique with code as follows:
 

' Code from frmPrintReport2

On Error Resume Next

DoCmd OpenReport "Catalog"

 
 
Try this form. If you press the Cancel button while the report is printing, your report will indeed stop printing, and you won't see an error message. Mission accomplished, right? Well, not quite.
 
What if you want to see some message in this case, but not the default message? In that case, you'll need to take one more error handling step. In this final example, frmPrintReport3, I've used a full error handling fragment that allows the procedure to display a specific message if the user cancels the print job:
 

' Code from frmPrintReport3

Const vbInformation = 64

 

On Error GoTo PrintReportErr

DoCmd OpenReport "Catalog"

 

PrintReportExit:

 Exit Sub

 

PrintReportErr:

 Select Case Err

   Case 2501   ' Action OpenReport was cancelled.

     MsgBox _

      "You've cancelled the report. " & _

      "Click the Print Report button " & _

      "to start again.", _

      vbInformation, "Report Cancelled"

   Case Else

     MsgBox "Error: " & Error & " (" & Err & ")"

 End Select

 Resume PrintReportExit

 
 
About the Download Files
The 16-bit demonstration database is QA970616.MDB, and the 32-bit version is QA970632.MDB. The 32-bit database is in Access 95 format, but you can easily convert it for Access 97.
 
Download GETZ44.EXE