Microsoft Access Tips for Casual Users

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

  1. Open your form in design view.
  2. 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.
  3. If the wizard starts, cancel it. It will not give you the flexibility you need.
  4. Right-click the new command button, and choose Properties. Access opens the Properties box.
  5. On the Other tab, set the Name to something like: cmdPrint
  6. 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.
  7. On the Event tab, set the On Click property to: [Event Procedure]
  8. Click the Build button (...) beside this. Access opens the code window.
  9. 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

  1. 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.
  2. The report will not filter correctly if it is already open.
  3. If you want the report to print without preview, replace acViewPreview with acViewNormal.
  4. For a more comprehensive example that handles many optional criteria, see Search Criteria database.

Home Index of tips Top