Printing an Invoice

Introduction

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

  1. To follow with this exercise, open the College Park Auto Shop database 
  2. From  the Forms section of the Database window, double-click the WorkOrders form to open it
  3. After viewing it, switch it to Design View
  4. On the Toolbox, make sure the Control button is down and click the Command Button
  5. On the form click in the Form Footer section
  6. When the Command Button Wizard starts, in the first page, in the Categories list, click Report Operations
  7. In the Actions section, click Preview Report:
     
  8. Click Next
  9. In the list of reports, click WorkOrders and click Next
  10. In the third page of the wizard, change the content of the top text box to Preview Invoice
  11. Click Next
  12. Change the name of the button to cmdPreviewInvoice and click Finish
  13. To customize the button you just added, right-click it and click Build Event
  14. 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
  15. Close the code window or Microsoft Visual Basic that was opened
  16. To test the form, switch it to Form View and navigate to a record other than the first. Then click the Preview Invoice button

 


Copyright 2004 Yevol