How to populate report based on criteria specified in form?

Posted on 2004-11-17
Last Modified: 2008-03-06
Hello Experts,

I have noticed several references to this subject but I’m having trouble creating a solution that meets my needs.

My goal is to have a form that allows the user to select the criteria used to filter an existing query and populate the report based on the results of that query.

In Access 2000 I have a query called “qryReport”. This query contains several expressions that generate a master record set of all the data I need for my reports.

The report is called “rptReport”.  It contains selected fields found in qryReport. These fields will not change on the report (at least for now).

Finally, I have a form that contains about 30 text, combo and checkbox objects that will be used to filter data found in qryReport and populate the report. I also have a combobox that allows users to sort data based on the fields represented in the report.

I will also be looking into exporting this report as and Excel spreadsheet. I’m not looking for a solution for this particular feature right now, but I thought I’d mention my future plans in case it impacts the solution.

I have tried sending parameters to qryReport, modifying the record source in rptReport on the fly, and applying filters to rptReport but I keep running into roadblocks.  

I’m looking for a solution that can easily be managed if the criteria on the form must change.

Any suggestions or samples would be greatly appreciated.

Question by:JakeB52
    LVL 4

    Expert Comment

    I put this

       Me.Filter = Forms.frmProjectSummary.Filter
       Me.FilterOn = Forms.frmProjectSummary.FilterOn
       Me.OrderBy = Forms.frmProjectSummary.OrderBy
       Me.OrderByOn = Forms.frmProjectSummary.OrderByOn

    In my Report OnOpen and it pulls the sort order and filter properties from my Project Summary Form.

    Author Comment

    Thanks CrossWired.

    I should add that the information collected on my form may not exactly reflect the text that I'm filtering. For example, a combobox may have a list of countries, and even though "Germany" may appear as the selected item to the user, I'm actually filtering based on the country's id ( cboCountry.Column(0) ). The data contained in my combobox may look something like this:


    whereas the item that appears to the user will depend on the language mode he/she is using.

    So overall, I would prefer not to depend on the data that is displayed on the form to filter my report.
    LVL 4

    Expert Comment

    On the form you would have to setup the filter correctly, I use 2 ComboBoxes to set the different filter criteria and once my project summary shows the right info, I hit a button and it pulls up the correct report.  If you need an example of my form filtering code let me know.


    Author Comment

    A code sample would be helpful. Thanks.
    LVL 4

    Accepted Solution

    I have 1 button - Command33
    2 Checkboxes - Check75 - which includes or excludes info from Comb73
                        - Check77 - which includes or excludes info from Combo31
    2 ComboBoxes - Combo73 - which list different filtering criteria (aka "Current Projects", "Current Quotes")
                          - Combo31 - which list all the engineers names

    Private Sub Command33_Click()
    If Me.Check75 = True Then
        Select Case Me.Combo73
        Case 1
            tempStage = "(Stage = 9)"
            Me.Label81.Caption = "Current Quotes"
            Me.Label87.Caption = ""
        Case 2
            tempStage = "((Stage > 0) AND (Stage < 9)) AND IsNull(Priority)"
            Me.Label81.Caption = "Current Projects"
            Me.Label87.Caption = ""
        Case 3
            FilterDays = InputBox("How many days back should I show?")
            FilterDate = Format(DateAdd("d", -(FilterDays), Now()), "mm/dd/yyyy")
            tempStage = "(Stage = 0) AND ((DelivDate > #" & FilterDate & "#) AND (DelivDate <> Null))"
            Me.Label81.Caption = "Recently Completed Projects"
            Me.Label87.Caption = "Last " & FilterDays & " Days"
        Case 4
            tempStage = "(Priority = 'H')"
            Me.Label81.Caption = "Projects on Hold"
            Me.Label87.Caption = ""
        Case 5
            StartDate = InputBox("What start date?", , Format(Now(), "mm/dd/yyyy"))
            EndDate = InputBox("What end date?", , Format(Now(), "mm/dd/yyyy"))
            tempStage = "(Stage = 0) AND ((DelivDate > #" & StartDate & "#) AND (DelivDate < #" & EndDate & "#) AND (DelivDate <> Null))"
            Me.Label81.Caption = "Specific Dates Completed Projects"
            Me.Label87.Caption = StartDate & " - " & EndDate
        Case 6
            DoCmd.OpenForm "frmCompanyPopUp", , , , , acDialog
            Comp = Forms("frmCompanyPopUp").Combo0.Value
            tempStage = "CompID = " & Comp
            Me.Label81.Caption = "All Projects for " & DLookup("CompName", "tblCompany", "ID = " & Comp)

        End Select
    End If

    Me.FilterOn = False
    If Me.Check77 = True Then
        Me.Filter = "(PL = " & "'" & Me.Combo31 & "')"
        If Me.Check75 = True Then
            Me.Filter = "(" & Me.Filter & " AND " & tempStage & ")"
        End If
    ElseIf Me.Check75 = True Then
        Me.Filter = tempStage
    End If

    If Me.Check77 = False And Me.Check75 = False Then
    Me.Filter = ""
    End If

    Me.FilterOn = True

    End Sub

    Author Comment


    My apologies for not getting back to your sooner... I came across a few issues while generating my reports that were not directly related to this posting. Your suggestions worked great. Thank you very much!
    LVL 4

    Expert Comment

    no problem, it got you going so everyoen is happy

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now