Looking to take your VBA skills further?...

Discover twinBASIC — a powerful new development platform that expands on VBA and VB6 with advanced features, modern tools, and enhanced compatibility. Perfect for those ready to elevate their projects or transition from VBA, twinBASIC lets you build on what you already know and take your applications to the next level!

Try out twinBASIC Community Edition - it's free!

Print the record in the form

        12 votes: *****     13,66 views      No comments
by Allen Browne, 20 April 2005    (for ALL VERSIONS of Access)

Microsoft Access Tips for Casual Users

Provided by allenbrowne.com, September 2004. Updated June 2006


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

Rate this article:  Your rating: PoorYour rating: Not so goodYour rating: AverageYour rating: GoodYour rating: Excellent


This is a cached tutorial, reproduced with permission.

Have your say - comment on this article.

What did you think of 'Print the record in the form'?

No comments yet.

Why not be the first to comment on this article?!

Have your say...

Name
E-mail (e-mail address will be kept private)
Comments


Comments require approval before being displayed on this page (allow 24 hours).