Search form using two fields in combobox

Hi Experts,

How do I create a combobox on a form to search for records using two fields in the recordsource?  I've used the wizard but it will only bring me to the first field in the query.  Thank you for your help!
Skip SleeperIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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 FyeOwner, Developing Solutions LLCCommented:
Well, normally, you would use two separate combo boxes to filter your recordset on the values in two separate fields.  I usually add two buttons (Filter and ClearFilter) next to the combo boxes.  Then in the cmd_Filter_Click event:

Private Sub cmd_Filter_Click

    Dim strFilter as string

    if me.cboFilter1 & "" <> "" then strFilter = "([Field1] = " & me.cboFilter1 & ")"

    if me.cboFilter2 & "" <> "" then
        if strFilter <> "" then strFilter = strFilter & " AND "
        strFilter = strFilter & "([Field2] = " & me.cboFilter2 & ")"

    me.Filter = strFilter
    me.FilterOn = iif(strFilter = "", False, True)
End Sub

Open in new window

This assumes that the values displayed in cboFilter1 and cboFilter2 are both numeric.  If either of those is a text string, then use something like:

strFilter = strFilter & "([Field2] = " & chr$(34) & me.cboFilter2 & chr$(34) & ")"

Then in the cmd_ClearFilter button:

Private Sub cmd_ClearFilter_Click

    me.cboFilter1 = NULL
    me.cboFilter2 = NULL
    me.filterOn = false

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
Skip SleeperIT ManagerAuthor Commented:
Hi Dale,

Would an end-user be able to find a single record if both of the comboboxes were used?

I'm guessing that the single combobox option won't work?

Dale FyeOwner, Developing Solutions LLCCommented:
Well, you could create a single combo box which would have a RowSource something like:

SELECT DISTINCT Field1, Field2 FROM yourTable ORDER BY Field1, Field2

You would then have to set the combo box to display 2 columns (when it expands), but a combo will only ever display a single value when when the combo is colllapsed.  This is why it would be preferable to use two combo boxes.
Skip SleeperIT ManagerAuthor Commented:
Got it, thanks!
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.