Printing Forms - Dialog Box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am a novice Access user and have a question regarding printing forms. I
made the mistake of trying to print from a form rather than a report.
Needless to say I had to go back and fix that using Allen Browne's easy to
follow instructions. However, his instructions send the print job to the
default printer. What I need is to be able to display the Print Dialog box
and allow the user to select the printer before printing the record. How
would I accomplish this in conjunction?

Allen's Instructions follow below:

Print the record in the form
How do you print just the one record you are viewing in the form?

Create a report, to get the layout right for printing. Use the primary key
value that uniquely identifies the record in the form, and open the report
with just that one record.

The steps
Open your form in design view.
If you do not see the toolbox, open it from the View menu.
Click the command button in the toolbox, and click on your form.
If the wizard starts, cancel it. It will not give you the flexibility you
need.
Right-click the new command button, and choose Properties. Access opens the
Properties box.
On the Other tab, set the Name to something like: cmdPrint
On the Format tab, set the Caption to the text you wish to see on the
button, or the Picture if you would prefer a printer or preview icon.
On the Event tab, set the On Click property to: [Event Procedure]
Click the Build button (...) beside this. Access opens the code window.
Paste the code below into the procedure. Replace ID with the name of your
primary key field, and MyReport with the name of your report.
The code
Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub
Notes
If your primary key is a Text type field (not a Number type field), you need
extra quotes:
strWhere = "[ID] = """ & Me.[ID] & """"
The report will not filter correctly if it is already open.
If you want the report to print without preview, replace acViewPreview with
acViewNormal.
 
Try changing the statement:

DoCmd.OpenReport "MyReport", acViewPreview, , strWhere

to:

DoCmd.OpenReport "MyReport", acViewPreview, , strWhere, acIcon
DoEvents
DoCmd.RunCommand acCmdPrint
 
Van:

Thanks for the follow up, but after making the change that you suggested, it
still sent the print job to the default printer and did not allow me to
choose which printer. It should be noted, and for this I apologize, the I
changed the code originally so as to not display the Print Preview, just
print the form. So in essence, my code now looks like this:

DoCmd.OpenReport "MyReport", acViewNormal, , strWhere, acIcon
DoEvents
DoCmd.RunCommand acCmdPrint

Any other ideas?


Van T. Dinh said:
Try changing the statement:

DoCmd.OpenReport "MyReport", acViewPreview, , strWhere

to:

DoCmd.OpenReport "MyReport", acViewPreview, , strWhere, acIcon
DoEvents
DoCmd.RunCommand acCmdPrint

--
HTH
Van T. Dinh
MVP (Access)



Eric said:
I am a novice Access user and have a question regarding printing forms. I
made the mistake of trying to print from a form rather than a report.
Needless to say I had to go back and fix that using Allen Browne's easy to
follow instructions. However, his instructions send the print job to the
default printer. What I need is to be able to display the Print Dialog
box
and allow the user to select the printer before printing the record. How
would I accomplish this in conjunction?

Allen's Instructions follow below:

Print the record in the form
How do you print just the one record you are viewing in the form?

Create a report, to get the layout right for printing. Use the primary key
value that uniquely identifies the record in the form, and open the report
with just that one record.

The steps
Open your form in design view.
If you do not see the toolbox, open it from the View menu.
Click the command button in the toolbox, and click on your form.
If the wizard starts, cancel it. It will not give you the flexibility you
need.
Right-click the new command button, and choose Properties. Access opens
the
Properties box.
On the Other tab, set the Name to something like: cmdPrint
On the Format tab, set the Caption to the text you wish to see on the
button, or the Picture if you would prefer a printer or preview icon.
On the Event tab, set the On Click property to: [Event Procedure]
Click the Build button (...) beside this. Access opens the code window.
Paste the code below into the procedure. Replace ID with the name of your
primary key field, and MyReport with the name of your report.
The code
Private Sub cmdPrint_Click()
Dim strWhere As String

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If

If Me.NewRecord Then 'Check there is a record to print
MsgBox "Select a record to print"
Else
strWhere = "[ID] = " & Me.[ID]
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub
Notes
If your primary key is a Text type field (not a Number type field), you
need
extra quotes:
strWhere = "[ID] = """ & Me.[ID] & """"
The report will not filter correctly if it is already open.
If you want the report to print without preview, replace acViewPreview
with
acViewNormal.
 
I suggested acViewPreview and you had acViewNormal in your code.
acViewNormal will always print to the default printer.
 
I obviously have the wrong coding then. What would be the code to open and
run the report and then bring up the Print Dialog box and allow the user to
select the printer. Once they have selected the printer, then they can hit
the Print Button to print the Report. Thank you again for all of your help.
 
Eric, you seem to be missing pieces each time you change your code. As Van
originally said...

In the click event of your command button on the form...
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere, acIcon
DoEvents
DoCmd.RunCommand acCmdPrint
 
Back
Top