Using multiple Combo-boxes in Access as query criteria

Safari

Member
Local time
Today, 07:53
Joined
Jun 14, 2021
Messages
95
HI ALL

How To Use Multiple Combo-boxes in Access as query criteria Like My Attaced DB​

i want if choose date from 1 combobox the query is run
and if i choose data from 2 combobox the query is run too ( with 2 conditions )

At All i want to run query if i use 1 combobox data or 2 combobox together
 

Attachments

For each criteria, try using something like this:

Forms!FormName.ComboboxName OR Forms!FormName.ComboboxName Is Null
 
For each criteria, try using something like this:

Forms!FormName.ComboboxName OR Forms!FormName.ComboboxName Is Null
not working
 
I opened your database and could not find anything in it related to anything you said. I am going to answer the question differently.

When I had multiple combo boxes to contribute to the WHERE clause for a query, OR when they would contribute to the implied WHERE clause of a form's filter, I built a criteria string FIRST using VBA to concatenate the parts I wanted to use, then applied it as a separate step.

Since you are interested in TWO combo boxes but sometimes one of them isn't used, what I would do is use VBA to build the selection string before I tried to apply the combo selections. In a combo box, if nothing is selected, its .ListIndex is -1, so it is an easy test.

Code:
strCriteria = ""
IF Me.cboA.ListIndex <> -1 THEN
    strCriteria = "[TransactionDate]= #" & Me.cboA & "#"
ELSE
    IF me.cboB.ListIndex <> -1 THEN
        strCriteria = "[TransactionDate] = #" & Me.cboB & "#"
    END IF
END IF
IF strCriteria <> "" THEN
    Me.Filter = strCriteria
    Me.FilterOn = TRUE
END IF

This snippet picks the A combo box in preference to the B combo box if both were used. You didn't tell us what to do if BOTH were used but that is just a matter of adjusting this logic to fit. Since I sometimes built dynamic SQL strings for use with a db.Execute operation, I frequently built the string separately and then activated it. But this method would work either for filtering or for building a new SQL string.
 
are you filtering your Query?
 

Attachments

Forms!FormName.ComboboxName OR Forms!FormName.ComboboxName Is Null
This is what I do but you MUST enclose the expression in parentheses if you are going to add additional criterial

(Forms!FormName.ComboboxName OR Forms!FormName.ComboboxName Is Null) And (somthingelse) OR (someothercondition)
 

Users who are viewing this thread

Back
Top Bottom