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.
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.