A family of Microsoft relational database management systems designed for ease of use.
Firstly create a report based on the relevant table or query; do not print a form. You then have two options to restrict the report to the current record by opening it from a form bound to the table or query:
1. Add two command buttons to the form and open the report in print preview or to send it to a printer. If we assume for this example that the form is based on a Contacts table with a numeric primary key ContactID, and you've named the report rptContacts, to open the report in print preview the code would be:
Const MESSAGETEXT = "No current record."
Dim strCriteria As String
strCriteria = "ContactID = " & Me.ContactID
If Not IsNull(Me.ContactID) Then
' ensure current record is saved
Me.Dirty = False
' open report in print preview
DoCmd.OpenReport "rptContacts", _
View:=acViewPreview, _
WhereCondition:=strCriteria
Else
MsgBox MESSAGETEXT, vbExclamation, "Invalid Operation"
End If
For the button to print the report the code would be exactly the same, but the View argument of the OpenReport method would be omitted:
DoCmd.OpenReport "rptContacts", _
WhereCondition:=strCriteria
2. the alternative approach is to base the report on a query which includes a parameter on the ContactID column referencing the ContactID control on the form. In query design view, assuming the form is named frmContacts, you'd out the following in the criteria row of the ContactID column:
Forms!frmContacts!ContactID
The code to open the report from the form would be as above, but omitting the WhereCondition argument of the OpenReport method, so to open the report in print preview would be;
DoCmd.OpenReport "rptContacts", _
View:=acViewPreview, _
and to print the report:
DoCmd.OpenReport "rptContacts"
Tip: if you put the following in the Activate event procedure of the report;
DoCmd.Maximize
and the following in its Deactivate event procedure:
DoCmd.Restore
the report will be maximized when you open it in print preview, and the normal window will be restored when you close it.