Print form code: avoid error if print is cancelled

Laura Mustto

New member
Local time
Today, 03:05
Joined
Jul 17, 2012
Messages
8
Hello again guys, this must be a very newbie question but I managed to make this code to print a record in a new form, at the current record. I'm proud because it worked! But there's a little problem, if you finally don't print (click 'cancel') it returns an error. How to avoid it?

Code:
Dim strDocName As String
 Dim strWhere As String
     strDocName = "frmPrint"
     strWhere = "[P]=" & Me!P
     DoCmd.OpenForm strDocName, acNormal, , strWhere
     DoCmd.PrintOut acSelection
     DoCmd.Close acForm, "frmprint"
 
Last edited:
hello again,

Really you should only print reports. Printing forms can be messy. So create a report (you can make it look like your from if you need) and you can then replace the DoCmd.OpenForm strDocName, acNormal, , strWhere with DoCmd.OpenReport strDocName, acPreview, , strWhere to view the report. If the user then closes that report you do not get any errors.
If you want to keep with the form printing, add command buttons to the print form for the user to click to Print (add the print code there) or cancel (add the close form code there) rather than controlling the printing and closing from your current code (set the command buttons Display When property to Screen Only so they do not get printed)
 
Yep, that solved the problem. Thanks.
 
Me again :p

I didn't want to make a new thread for the following question, it's a existential doubt:

What's the difference between the following codes, if they do the same? Thanks!

Dim strDocName As String strDocName = "frmPrint" DoCmd.OpenForm strDocName, acNormal
DoCmd.OpenForm "frmPrint", acNormal
 

Users who are viewing this thread

Back
Top Bottom