Query using multiple parameters?

Posted on 2014-08-27
Last Modified: 2016-06-15
I want to run a query based on options selected in a drop down menu on a form.

I want the user to be able to pick what parameters he/she will use.

However, the query will only run based on 1 of the parameters I have set up and won't run on the others.

My form has the following combo boxes: cmbTeam, cmbTO, and cmbSTO.

I have the query set up so that, in the query builder under the field, it says "[forms]![frm_ParameterRpt]![cmbTeam] or like [forms]![frm_ParameterRpt]![cmbTeam] is null"

I have that for each field I want the user to be able to filter by.

However, something weird is happening.

When I set this up the first time, I put the "[forms]![......" in the cmbSTO field first. I set up the parameters for the other fields, but the query will only run for the cmbSTO parameter.

I reset everything up, but put the parameter in for cmbTeam first. Even though all the other parameters are listed, it will only run the query for cmbTeam.

I am really confused. Does someone know what is going on?

And, I apologize if my wording is super poor. I am happy to explain further.
Question by:Megin
    LVL 47

    Accepted Solution

    Assuming you have fields corresponding to your combo boxes,

    Your query would look something like:

    SELECT * FROM yourTable
    WHERE (IsNull([forms]![frm_ParameterRpt]![cmbTeam]) OR
                   [Team] = [forms]![frm_ParameterRpt]![cmbTeam])
    AND  (IsNull([forms]![frm_ParameterRpt]![cmbTo]) OR
                   [To] = [forms]![frm_ParameterRpt]![cmbTo])
    AND  (IsNull([forms]![frm_ParameterRpt]![cmbSTO]) OR
                   [STO] = [forms]![frm_ParameterRpt]![cmbSTO])

    You would then need to requery your form at some point.  One way would be to requery it in the AFterUpdate event of each of the combo boxes, but that could take some time if you have a large recordset to filter.  Another would be to use a command button to perform the requery operation.

    A third method would be to use VBA to construct a filter and simply apply the filter to the recordset.  If you did that, you might call the following subroutine (located in the forms code module) from the AfterUpdate events or the command button mentioned  above.
    Private Sub FilterForm()
        Dim varFilter as Variant
        varFilter = NULL
        if me.cmbTeam & "" <> "" Then
            varFilter = "[Team] = " & me.cmbTeam
        end if
        if me.cmbTo & "" <> "" Then
            varFilter = (varFilter + " AND ") & "[To] = " & me.cmbTo
        end if
        if me.cmbSTO & "" <> "" Then
            varFilter = (varFilter + " AND ") & "[STO] = " & me.cmbSTO
        end if
        me.Filter = varFilter & ""
        me.FilterOn = (me.Filter <> "")
    End Sub

    Open in new window

    LVL 31

    Assisted Solution

    See my Fancy Filters sample database, which lets you select one or more filters of various data types from combo boxes:

    and here is a screen shot of the form:
    Fancy Filters form

    Author Comment


    I really like the way your form works, but I am not sure I understand the code behind it. Where is "pstr" coming from, and the word "filter" is listed at the end of several of these items.

    Are these standard parts of VBA?

    Am I missing something in this database?
    LVL 31

    Expert Comment

    The variables starting with "pstr" are public variables, declared in the basUtilities standard module.  I include the work "filter" in them because they are all String variables.  The combo boxes have values of different data types (Numeric, String and Date), but the AfterUpdate code for each combo box creates a String variable, using the appropriate delimiters.  These variables are used to create the concatenated filter (see the code on the Create and Apply Combined Filter button).  The code on this button concatenates all the separate filter strings into a single concatenated filter, and then applies it to the subform to display the filtered records.

    Author Comment

    Okay, so I tried to work with the code behind the Fancy Filters Sample Database and just got really, really confused. My VBA coding just isn't there yet.

    I am now trying to work with that SQL code, but right now it isn't doing anything when I use it. But I am not sure if I have it in the editor correctly.

    What I have now is:
    Private Sub btn_Ok_Click()
    Dim strsql As String
    strsql = "select (all that sql code)
    DoCmd.OpenQuery "Qry_Parameters_Rpt"

    Open in new window

    LVL 26

    Expert Comment

    No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

    I have recommended this question be closed as follows:

    -- Dale Fye (Access MVP) (https:#a40289444)
    -- Helen_Feddema (https:#a40290620)

    If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

    Experts-Exchange Cleanup Volunteer
    LVL 31

    Expert Comment

    If you need more help, you could try the new Live feature, or post a gig.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now