ACC: How to Filter a Report Using a Form's Filter

Last reviewed: February 23, 1998
Article ID: Q145591
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article describes how to create a button on a filtered form that opens a report and applies the same filter to the report that is on the form.

NOTE: This article explains a technique demonstrated in the sample files, RptSampl.exe (for Microsoft Access for Windows 95 version 7.0) and RptSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q145777
   TITLE     : ACC95: Microsoft Access Sample Reports Available on MSL

   ARTICLE-ID: Q175072
   TITLE     : ACC97: Microsoft Access 97 Sample Reports Available on MSL

MORE INFORMATION

This technique involves creating a new form and report in the sample database Northwind.mdb. The form uses event procedures to apply a filter and to open the new report. The report uses the Filter property to apply the same filter used in the form.

  1. Open the sample database Northwind.mdb.

  2. Use the AutoReport: Tabular Wizard to create a new report based on the Customers table. Close and save the report as rptCustomers.

  3. Use the AutoForm: Tabular Wizard to create a new form based on the Customers table. Close and save the form as frmFilterForm.

  4. Open frmFilterForm in Design view. Increase the size of the form footer section to accommodate three command buttons.

  5. Create a command button in the form footer and set its properties as follows:

           Name:     cmdOpenReport
           Caption:  Open Report
           OnClick:  [Event Procedure]
    
        Set the OnClick [Event Procedure] as follows:
    
           Private Sub cmdOpenReport_Click()
             If Me.Filter = "" Then
               MsgBox "Apply a filter to the form first"
             Else
               DoCmd.OpenReport "rptCustomers", A_PREVIEW, , Me.Filter
             End If
           End Sub
    
    

  6. Create a second button in the form footer and set its properties as follows:

           Name:     cmdClearFilter
           Caption:  Clear Filter
           OnClick:  [Event Procedure]
    
        Set the OnClick [Event Procedure] as follows:
    
           Private Sub cmdClearFilter_Click()
             Me.Filter = ""
           End Sub
    
    

  7. Create a third button in the form footer and set its properties as follows:

           Name:     cmdClose
           Caption:  Close
           OnClick:  [Event Procedure]
    
        Set the OnClick [Event Procedure] as follows:
    
           Private Sub cmdClose_Click()
             DoCmd.Close acForm, Me.Form.Name
           End Sub
    
    

  8. Set the following properties for the frmFilterForm form:

    OnOpen: [Event Procedure] OnClose: [Event Procedure]

    Set the form's OnOpen [Event Procedure] as follows:

    Private Sub Form_Open(Cancel as Integer)

             Me.Filter = ""
           End Sub
    
        Set the form's OnClose [Event Procedure] as follows:
    
           Private Sub Form_Close()
             DoCmd.Close acReport, "rptCustomers"
           End Sub
    
    

  9. Switch the form to Form view.

  10. On the toolbar, click the Filter By Form button to set a filter, and then click the Apply Filter button to apply the filter.

  11. Click the Open Report button on the form. A report should appear with the same filter that was applied to the form.

REFERENCES

For more information about the Filter property, search the Help Index for "Filter Property," or ask the Microsoft Access 97 Office Assistant.

For more information about Filter By Form or Filter By Selection, search the Help Index for "Filter By Form" or "Filter By Selection," or ask the Microsoft Access 97 Office Assistant.

Keywords          : FmrCdbeh kbusage kbfaq
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 23, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.