Microsoft Knowledge Base Email Alertz

This article describes how to create a command button on a filtered form that, when clicked, opens a report and applies the filter that is on the form to the report. NOTE : This article explains a technique demonstrated in the sample

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 208548 - Last Review: December 12, 2002 - Revision: 1.0

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

This article was previously published under Q208548
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

SUMMARY

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

NOTE: This article explains a technique demonstrated in the sample file, RptSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:
231851  (http://kbalertz.com/Feedback.aspx?kbNumber=231851/EN-US/ ) Microsoft Access 2000 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.
  1. Open the sample database Northwind.mdb.
  2. In the Database window, click Reports under Objects, and then click New.
  3. In the New Report dialog box, click AutoReport: Tabular, select Customers from the Choose the table or query where the object's data comes from list, and click OK.
  4. Close and save the report as rptCustomers.
  5. In the Database window, click Forms under Objects, and then click New.
  6. In the New Report dialog box, click AutoForm: Tabular, select Customers from the Choose the table or query where the object's data comes from list, and click OK.
  7. Close and save the form as frmFilterForm.
  8. Open frmFilterForm in Design view.
  9. Increase the size of the form footer section so that it can hold three command buttons.
  10. Create a command button in the form footer and set its properties as follows:
       Name: cmdOpenReport
       Caption: Open Report
       OnClick: [Event Procedure]
    					
  11. Set the OnClick property of the command button to the following event procedure:
    Private Sub cmdOpenReport_Click()
        If Me.Filter = "" Then
            MsgBox "Apply a filter to the form first."
        Else
            DoCmd.OpenReport "rptCustomers", acViewPreview, , Me.Filter
        End If
    End Sub
    					
  12. Create a second command button in the form footer and set its properties as follows:
       Name: cmdClearFilter
       Caption: Clear Filter
       OnClick: [Event Procedure]
    					
  13. Set the OnClick property of the second command button to the following event procedure:
    Private Sub cmdClearFilter_Click()
        Me.Filter = ""
    End Sub
    					
  14. Create a third command button in the form footer and set its properties as follows:
       Name: cmdClose
       Caption: Close
       OnClick: [Event Procedure]
    					
  15. Set the OnClick property of the third command button to the following event procedure:
    Private Sub cmdClose_Click()
        DoCmd.Close acForm, Me.Form.Name
    End Sub
    					
  16. Set the following properties for the frmFilterForm form:
       OnOpen: [Event Procedure]
       OnClose: [Event Procedure]
    					
  17. Set the OnOpen property of the form to the following event procedure:
    Private Sub Form_Open(Cancel as Integer)
        Me.Filter = ""
    End Sub
    					
  18. Set the OnClose property of the form to the following event procedure:
    Private Sub Form_Close()
        DoCmd.Close acReport, "rptCustomers"
    End Sub
    					
  19. Switch the form to Form view.
  20. On the toolbar, click the Filter By Form button to set a filter, and then click the Apply Filter button to apply the filter.
  21. 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, click Microsoft Access Help on the Help menu, type Filter Property in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about Filter By Form, click Microsoft Access Help on the Help menu, type Modify a filter in the Filter By Form window in a table, query, or form in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about Filter By Selection, click Microsoft Access Help on the Help menu, type Filter records by selecting values in a form or datasheet in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

APPLIES TO
  • Microsoft Access 2000 Standard Edition
Keywords: 
kbhowto kbdta KB208548
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate