Run Time error when printing is cancelled.

Jason1

Registered User.
Local time
Yesterday, 21:13
Joined
Mar 12, 2010
Messages
63
I am really green when it comes to VB. I have written some code to print a report in Access 2007. When I press my command button, the print properties window comes up just like it should. If I select print in the print properties window, the code runs fine. But, if I select cancel, then I get a run time error. I am sure I am just missing some code. Could someone please fill in the blanks for me. Below is the code I am using.

Code:

CurrentDb.Execute mySQL, dbFailOnError
DoCmd.OpenReport "rptWorkOrder", acViewReport, "", "[WorkOrderNum]=[Forms]![frmIncompleteWO]![combo19]", acNormal
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "rptWorkOrder"
DoCmd.Close acForm, "frmIncompleteWO"
End Sub
 
Trap error 2501 in you code and clear the error.

CurrentDb.Execute mySQL, dbFailOnError
DoCmd.OpenReport "rptWorkOrder", acViewReport, "", "[WorkOrderNum]=[Forms]![frmIncompleteWO]![combo19]", acNormal
If Err.Number = 2501 Then Err.Clear
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "rptWorkOrder"
DoCmd.Close acForm, "frmIncompleteWO"
End Sub

JR
 
JR,

Thanks for the help, but it doesn't seem to be working. I am still getting the 2501 runtime error.
 
Code:
On Error Goto Errorhandel
 
CurrentDb.Execute mySQL, dbFailOnError
DoCmd.OpenReport "rptWorkOrder", acViewReport, "", "[WorkOrderNum]=[Forms]![frmIncompleteWO]![combo19]", acNormal
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "rptWorkOrder"
DoCmd.Close acForm, "frmIncompleteWO"
 
Errorhandel:
If Err.Number = 2501 Then
    Err.Clear
    Resume Next
Else
    Msgbox "Error " & Err.Number & " Description: " & Err.Description
End If
End Sub

JR
 

Users who are viewing this thread

Back
Top Bottom