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

ID: Q145591


The information in this article applies to:


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 that is on the form to the report.

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:

Q145777 ACC95: Microsoft Access Sample Reports Available on MSL

Q175072 ACC97: Microsoft Access 97 Sample Reports Available on MSL


MORE INFORMATION

This example uses the sample database Northwind.mdb. The technique involves creating a new form and a new report. 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 that is 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 so that it can hold 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 
Version           : 7.0 97
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 15, 1999