Provided by Allen Browne, September 2004. Updated April 2010.
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.
- Click the command button in the toolbox (Access 1 - 2003) or on the Controls group of the Design ribbon (Access 2007 and 2010), 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
Me.Dirty = False
End If
If Me.NewRecord Then
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] & """"
For an explanation, see Quotation marks within quotes.
- The report will not filter correctly if it is already open.
- If you want the report to print without preview, replace acViewPreview with acViewNormal.
- For a more comprehensive example that handles many optional criteria, see Search Criteria database.