• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 58
  • Last Modified:

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.
2 Solutions
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

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?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now