Query using multiple parameters?

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Helen FeddemaCommented:
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
MeginAuthor Commented:

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?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Helen FeddemaCommented:
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.
MeginAuthor Commented:
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

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
Helen FeddemaCommented:
If you need more help, you could try the new Live feature, or post a gig.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.