Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Complicated search function

I have a form that has 5 comboboxes on it.  They are named cboCriteria1, cboCriteria2, cboCriteria3, cboCriteria4, and cboCriteria5,

And I have a table that looks like this:
 User generated image
If the user selects "Football" in any one of the 5 comboboxes I want the records that have "Football" entered in any of the 5 criteria fields in the table to be displayed in a form that opens in datasheet view mode.  Or in this case, records 1 and 6.  And f the user selects "Football" in all 5 comboboxes then I want the same result...  records 1 and 6 to be displayed.

But if the user for example selects "Football" in the 3rd combobox and "Baseball" in the 2nd combobox then I want all of the field data in records 1, 5, and 6 to display.

The resulting form should display all data in each of the records that have been found.  The form will open as a result of a click of a command button.
Avatar of Daniel Pineault
Daniel Pineault

You're going to have to use VBA I suspect to first count the number of combo boxes used for the search and then build the appropriate WHERE clause.

As a side note, your table structure should not be horizontal in this manner, but instead vertical, such as

RecordId
Criteria
CriteriaType
...
Your going to have a tough time with this because the table design is not good.

 At the moment, your going to be stuck with using VBA and walking the records to figure out which should be included, or building a temp table, executing multiple append queries to check each column.   Then basing the form on a query with a SELECT DISTINCT so you get one row per.

 You really should change the design before you go further.

Jim.
I would agree with Jim that your table design leaves something to be desired.

However, you could create a where clause that looks something like:

WHERE instr([Criteria1] & "," & [Criteria2] & "," & [Criteria3] & "," & [Criteria4] & "," & [Criteria5], NZ(Forms!yourForm!cboCriteria1,"")) >0
OR instr([Criteria1] & "," & [Criteria2] & "," & [Criteria3] & "," & [Criteria4] & "," & [Criteria5], NZ(Forms!yourForm!cboCriteria2,"")) >0
...

Another method would be to replace the combo boxes with a multi select list and build a string from the items selected in the list (would require VBA) and in that case, the SQL would look something like:

WHERE [Critieria1] IN ("Football", "Baseball")
OR [Criteria2] IN ("Football", "Baseball")
...
Avatar of SteveL13

ASKER

Too all..  Thank you so far.  I have attached a mock-up database with data entered that may help someone figure out an answer,  Note that I did re-work the initial table.

SearchMockup.accdb
ASKER CERTIFIED SOLUTION
Avatar of Bitsqueezer
Bitsqueezer
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.