Microsoft KB Archive/145591

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

Article ID: 145591

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q145591



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



SUMMARY
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:

145777 ACC95: Microsoft Access Sample Reports Available in Download Center

175072 ACC97: Microsoft Access 97 Sample Reports Available in Download Center



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.

 Open the sample database Northwind.mdb. Use the AutoReport: Tabular Wizard to create a new report based on the Customers table. Close and save the report as rptCustomers. Use the AutoForm: Tabular Wizard to create a new form based on the Customers table. Close and save the form as frmFilterForm. Open frmFilterForm in Design view. Increase the size of the form footer section so that it can hold three command buttons.  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   Create a second button in the form footer and set its properties as follows:

<pre class="fixed_text">      Name:     cmdClearFilter Caption: Clear Filter OnClick: [Event Procedure]

Set the OnClick [Event Procedure] as follows:

Private Sub cmdClearFilter_Click Me.Filter = "" End Sub </li>  Create a third button in the form footer and set its properties as follows:

<pre class="fixed_text">      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 </li>  Set the following properties for the frmFilterForm form:

<pre class="fixed_text">      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 </li> Switch the form to Form view.</li> On the toolbar, click the Filter By Form button to set a filter, and then click the Apply Filter button to apply the filter.</li> Click the Open Report button on the form. A report should appear with the same filter that was applied to the form.</li></ol>

<div class="references_section">