Home

Using a Report

 

Selecting a Report

To perform an operation on a report, you may need to select it first. To do this, in the Reports section of the Database window, you can simply click it, once. 

To programmatically select a report, you can use the DoCmd object that is equipped with the SelectObject() method. The syntax to use would be:

DoCmd.SelectObject acReport, [objectname][, indatabasewindow]

The first argument must be acReport to indicate that the object you are selecting is a report. The second argument is the name of the report you want to select. To select but only highlight the report in the Database window, you can pass the third argument as True.

If the report is already opened and it is displaying, and if you omit the third argument or pass it as False, the report would be displayed in the foreground. If the report is not opened and you omit the third argument or pass it as False, you would receive an error.

Opening a Report

When you start Microsoft Access and open a database, if it has some reports, obviously they would be closed. To use a report, you can open it first. A  report can be opened in Design View or in Print Preview. If you (or the user) double-click(s) a report in the Reports section of the Database window, it opens in Print Preview. This views allows the user to review the document before printing. By default, the view is blurred to show as much of its area as possible. To be able to read it, you  (or the user) can click the body of the report to zoom. To print it, you can click the Print button on the toolbar to send the document to the printer.

A report can also display in Design View. To show it, if the report is currently closed, you can right-click it and click Design View. You can also select it first, then click the Design button under the title bar of the Database window. If the report is already opened, to display it in Design View, as done for the form, you can click the View button on the toolbar. You can also click View -> Design on the main menu.

To programmatically open a report, you can call the OpenReport() method of the DoCmd object. Its syntax is:

DoCmd.OpenReport(ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs)

The first argument of this method is the name of the report that you want to open.

The second argument is a constant value that can be one of the following:

View Name Result
acViewDesign The report will display in Design View
acViewNormal The report will be sent directly to the printer
acViewPreview The report will display in Print Preview

This third argument, optional, is the name of a query in the database.

The fourth argument, also optional, allows you to specify what record would be printed. If you omit this argument, all records of the Record Source value of the report would be printed. If you want to print only one or a few selected records, you can create a WHERE statement and pass it as this argument.

The fifth argument specifies how the report should be displayed. It is a constant value that can be acDialog, acHidden, acIcon, or acWindowNormal. This argument is almost never used as it has little to no effect.

In most cases, instead of writing the code manually, you can use the Command Button Wizard to select the report to print and how you want the process to be done.

Practical LearningPractical Learning: Designing a Report

  1. Open the Solas Property Management database for which you created a report earlier
  2. In the Database window, click Forms and double-click Invoices
  3. Switch it to Design View
  4. In the Toolbox make sure the Control Wizard button is down.
    Click Command Button and click the left side of the Form Footer section
  5. In the first page of the wizard, click Report Operations
  6. In the right list, click Preview Report and click Next
     
  7. In the second page, make sure Invoices (it should be the only one) is selected and click Next
  8. In the third page, change the string in the text box with Preview This Invoice
     
  9. Click Next
  10. In the fourth page, change the Name of the control to cmdPreviewThisInvoice and click Finish
  11. Once again, in the Toolbox click Command Button and click on the right side of the previously added button
  12. In the first page of the wizard, click Report Operations and click Next
  13. In the second page click Next
  14. In the third page, change the string in the text box with Preview All Invoices and click Next
  15. In the fourth page, change the Name of the control to cmdPreviewAllInvoices and click Finish
  16. In the Footer section of the form, right-click the Preview This Invoice button and click Build Event...
  17. Change the event as follows:
     
    Private Sub cmdPreviewThisInvoice_Click()
    On Error GoTo Err_cmdPreviewThisInvoice_Click
    
        Dim stDocName As String
        Dim strWHERECondition As String
    
        stDocName = "Invoices"
        strWHERECondition = "InvoiceID = " & InvoiceID
        DoCmd.OpenReport stDocName, acPreview, , strWHERECondition
    
    Exit_cmdPreviewThisInvoice_Click:
        Exit Sub
    
    Err_cmdPreviewThisInvoice_Click:
        MsgBox Err.Description
        Resume Exit_cmdPreviewThisInvoice_Click
        
    End Sub
  18. Close Microsoft Visual Basic and return to Microsoft Access
  19. Switch the form to Form View
  20. Navigate to the third record and click Preview This Invoice
  21. Notice that only that invoice is available.
    If you have a printer, on the toolbar, click the Print button
  22. Close the report
  23. Click Preview All Invoices and notice that you can navigate to all invoices
  24. Close the report
  25. Close the form
  26. When asked whether you want to save the changes, click Yes

Closing a Report

After using a report, you (or the user) can close it. To close a report, you can click its system Close button Close or Close (Windows XP). You can also double-click its System button on the left side of its title bar. You can also press Ctrl + F4. When you (or the user) closes a report, if its design has been changed since it was opened, you (or the user) would be prompted whether to save it or ignore the changes.

To programmatically close a report, you can call the Close() method of the DoCmd object whose syntax is the same we saw for a form. Here is an example:

Private Sub cmdCloseStafMembers_Click()
    DoCmd.Close acReport, "StaffMembers", acSavePrompt
End Sub

When this code runs, if a report named StaffMembers is opened, it would be closed. If there is no report opened by that name, nothing would happen (Nice!).

 

Previous Copyright © 2005-2012, FunctionX, Inc. Next