Using multiple Combo-boxes in Access as query criteria (1 Viewer)

Safari

Member
Local time
Today, 16:38
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

  • Query.accdb
    476 KB · Views: 138

theDBguy

I’m here to help
Staff member
Local time
Today, 07:38
Joined
Oct 29, 2018
Messages
21,449
For each criteria, try using something like this:

Forms!FormName.ComboboxName OR Forms!FormName.ComboboxName Is Null
 

Safari

Member
Local time
Today, 16:38
Joined
Jun 14, 2021
Messages
95
For each criteria, try using something like this:

Forms!FormName.ComboboxName OR Forms!FormName.ComboboxName Is Null
not working
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:38
Joined
Feb 28, 2001
Messages
27,133
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.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:38
Joined
May 7, 2009
Messages
19,231
are you filtering your Query?
 

Attachments

  • Query.accdb
    1,020 KB · Views: 202

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:38
Joined
Feb 19, 2002
Messages
43,213
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

Top Bottom