You probably know that, if you create a button on a
form and call a report, when you click the button, all records on the
report would be printed. On a data entry form, you may want to print only
the report related to the record you are viewing. This is equivalent to
printing an invoice. Although this involves writing code, only a very
slight modification is necessary to implement this behavior. It consists
of setting the criteria of the DoCmd.OpenReport method.
|
Practical Learning: Printing an Invoice |
|
- To follow with this exercise, open the College Park Auto Shop
database
- From the Forms section of the Database window, double-click
the WorkOrders form to open it
- After viewing it, switch it to Design View
- On the Toolbox, make sure the Control button is down
and click the Command Button
- On the form click in the Form Footer section
- When the Command Button Wizard starts, in the first page, in the
Categories list, click Report Operations
- In the Actions section, click Preview Report:
- Click Next
- In the list of reports, click WorkOrders and click Next
- In the third page of the wizard, change the content of the top text
box to Preview Invoice
- Click Next
- Change the name of the button to cmdPreviewInvoice and click
Finish
- To customize the button you just added, right-click it and click
Build Event
- Change its code as follows:
Private Sub cmdPreviewInvoice_Click()
On Error GoTo Err_cmdPreviewInvoice_Click
Dim stDocName As String
stDocName = "WorkOrders"
DoCmd.OpenReport stDocName, acPreview, , "WorkOrderID = " & WorkOrderID
Exit_cmdPreviewInvoice_Click:
Exit Sub
Err_cmdPreviewInvoice_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewInvoice_Click
End Sub
|
- Close the code window or Microsoft Visual Basic that was opened
- To test the form, switch it to Form View and navigate to a record
other than the first. Then click the Preview Invoice button
|
|
|