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

Print dialog box error...on Cancel?

Status
Not open for further replies.

btj

Technical User
Nov 17, 2001
94
US
I have a button on a form that allows users to print it. As we have multiple printers and each user may have different printing needs, I coded the button to bring up the Print dialog box.

It works fine except when someone clicks Cancel in the dialog box. Access says that there is a Run-Time Error (2501).

Here is the code I am using:

Private Sub cmdPrint_Click()
MsgBox "This will print all records unless you select otherwise.", vbInformation
DoCmd.RunCommand acCmdPrint
'remove this line of code if you do not want print options
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.PrintOut acSelection
End Sub

Does anyone know how to fix this? Additionally, can they also bring up a print preview screen?

I would appreciate any advice you can give.

Thanks,
Ben
 
You need to add an error handler to the code:

Private Sub cmdPrint_Click()
On error goto printerr

MsgBox "This will print all records unless you select otherwise.", vbInformation
DoCmd.RunCommand acCmdPrint
'remove this line of code if you do not want print options
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.PrintOut acSelection

exit sub

printerr:

select case err
case 2501
resume next
case else

end select

exit sub

End Sub
 
John,
Thank you for the rapid response! That makes sense, but now when you click Cancel, it prints the form anyway.

Is that because of "Resume Next"? I just want the Cancel button to do its job - cancel with no other complications.

What am I missing?
 
Yeah, the resume next just continues the code to the next statement. Just change Resume next to exit sub.
 
Great...thanks so much for the tip.

That was exactly what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top