473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Search/Filter box that filters across multiple fields based on multiple criteria

Hi! I'm basically emulating a split form by having a subform in datasheet view that displays all the records of a table. Clicking on a record in the subform populates the mainform with the record's data, just like in a split form.

Anywho, I have a search/filter box in the header of my main form that filters the subform on the fly. It works great so far, but we want to take it a step further by filtering for records that contain criteria across multiple fields. For instance, assuming the table below:

Expand|Select|Wrap|Line Numbers
  1.   ID  | Field A | Field B | Field C 
  2.   1     Cat       Dog       Bird
  3.   2     Earth     Rock      Fire
  4.   3     Rain      Wind      Water
  5.   4     AA Cat    BB Fire   CC Rock
  6.  
Typing in "r" will result in all of the records being there:

Expand|Select|Wrap|Line Numbers
  1.   ID  | Field A | Field B | Field C 
  2.   1     Cat       Dog       Bird
  3.   2     Earth     Rock      Fire
  4.   3     Rain      Wind      Water
  5.   4     AA Cat    BB Fire   CC Rock
  6.  
Typing in "ro" will result in:

Expand|Select|Wrap|Line Numbers
  1.   ID  | Field A | Field B | Field C 
  2.   2     Earth     Rock      Fire
  3.   4     AA Cat    BB Fire   CC Rock
  4.  
Typing in "rock, fire" will result in:

Expand|Select|Wrap|Line Numbers
  1.   ID  | Field A | Field B | Field C 
  2.   2     Earth     Rock      Fire
  3.   4     AA Cat    BB Fire   CC Rock
  4.  
Typing in "rock, fire, a cat" will result in:

Expand|Select|Wrap|Line Numbers
  1.   ID  | Field A | Field B | Field C 
  2.   4     AA Cat    BB Fire   CC Rock
  3.  

Typing in "rock, fire, b cat" will result in empty results since no field contains "b cat".

Expand|Select|Wrap|Line Numbers
  1.   ID  | Field A | Field B | Field C 
So, commas would separate criteria and only records that contain all criteria would be displayed. This is what we have for the On Change event for our text box:

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtFilter_Change()
  2.  
  3.     Dim strFilterText As String
  4.  
  5.     strFilterText = txtFilter.Text
  6.     txtHiddenFilter.Value = strFilterText
  7.  
  8.   Me.sbfm_Tasks.Form.Filter = "TaskName Like '*" & strFilterText & "*' Or TaskDescription Like '*" & strFilterText & "*'"
  9.   Me.sbfm_Tasks.Form.FilterOn = True
  10.  
  11.  
  12. 'Tests for a trailing space and exits the sub routine at this point
  13. 'so as to preserve the trailing space, which would be lost if focus was shifted from Text Box txtFilter
  14.     If Len(Me.txtHiddenFilter) <> 0 And InStr(Len(txtHiddenFilter), txtHiddenFilter, " ", vbTextCompare) Then
  15.         'Returns the cursor to the the end of the text in Text Box txtFilter,
  16.         'and restores trailing space lost when focus is shifted to the list box
  17.             Me.txtFilter = strFilterText
  18.             Me.txtFilter.SetFocus
  19.             Me.txtFilter.SelStart = Me.txtFilter.SelLength
  20.             Exit Sub
  21.     End If
  22.  
  23. 'Returns the cursor to the the end of the text in Text Box txtFilter
  24.     Me.txtFilter.SetFocus
  25.     If Not IsNull(Len(strFilterText)) Then
  26.         Me.txtFilter.SelStart = Len(strFilterText)
  27.     End If
  28.  
  29. End Sub
How can this code be modified to satisfy our request? Thanks!
Jun 10 '14 #1
4 1382
Here's a zipped copy of my database in accdb and mdb format.
Attached Files
File Type: zip notesreportfixed.zip (128.4 KB, 80 views)
Jun 10 '14 #2
Seth Schrock
2,965 Expert 2GB
It isn't the easiest of things to do, but it is possible and like most things, it can be done multiple ways. Here is how I would do it. First, it seems like you are wanting multiple criteria that would be separated by a comma. So, it would probably be simplest to use the Split function with the comma as the delimiter. Next, you need to compare your all your criteria to all the fields. Probably the easiest way would be to loop through your criteria inside a loop of the fields. Here is what you will end up with.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM MyTable
  2. WHERE [Field A] Like '*rock*' 
  3.         OR [Field A] Like '*fire*' 
  4.         OR [Field A] Like '*a cat*'
  5.         OR [Field B] Like '*rock*' 
  6.         OR [Field B] Like '*fire*' 
  7.         OR [Field B] Like '*a cat*'
  8.         OR [Field C] Like '*rock*' 
  9.         OR [Field C] Like '*fire*' 
  10.         OR [Field C] Like '*a cat*'
I can't help but think that there will be a performance problem though depending on how many criteria you put in and how many fields you need the criteria applied to.
Jun 10 '14 #3
GregHB
2
Wow! I feel like a light-bulb just got turned on. I had done a similar query for a knowledgebase that I developed last summer, but I had used the [Field A] or [Field B] or [Field C] approach, similar to what Seth wrote. However, I had not even thought of trying what he wrote.

Using that type of approach may help add a lot of additional functionality for my team / customers (~50 IT Trainers).

I wish I could contribute more to your cause, CNStarz, but here's hoping that this thread can help us both out. Thanks for the question, I hadn't even thought how to phrase it.
Jun 10 '14 #4
twinnyfo
3,653 Expert Mod 2GB
cnstarz,

An alternate method would be for the Change Event to first evaluate mow many criteria have been entered (i.e. to perform a character-by-character assessment, looking for a comma to delineate).

Then, the code could Dim an array, with that many cells. Then, using language similar to Seth's cycle through the search criteria and build a very complex WHERE clause. Depending on whether you wanted inclusive or exclusive (whether it is any records that contain any of the criteria versus any records that contain at least some of the criteria) this could get very complicated.

It could be done, and as Seth mentions, there are probably a multitude of ways to do it. Either way, you have to build in a method to assess all the search criteria first, then build your WHERE clause around that.

Hope this hepps.
Jun 10 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: fstenoughsnoopy | last post by:
I have a customer order database and I need to pull a customers information, ie first name, last name, address, city, state, zip, phone, etc, into the oder table. i don't know how to go about...
1
by: davidevan | last post by:
What I'm trying to do is set a players division according to their age. So if age is 8, update division to junior, if age is 9, update division to medium, if age is 10, update division to pee wee,...
3
by: Tonij (with a J) | last post by:
Hi all, I have an Access 2003 database that has been a work in progress for a while, it's basically a system inventory and issue tracking system. Recently I have added a seperate area for...
4
4Him
by: 4Him | last post by:
First off, let me say this is a great site! I've just started working with Access and much of my success is from what I've read here! Background: I have a form, driven off a single table. Goal:...
1
by: OKCDOCB | last post by:
Access 2000. I want to be able to enter a zip code in field one and have the related city, state and supervisor filled into field 2, 3 and 4. Currently I add the zip code to a table, field one...
3
by: Redbeard | last post by:
Hi All this is my first time post, be gentle. I am looking at creating a keyword search that searches multiple fields in a Form and then filters records that match the keyword. The Form...
3
by: marsh44 | last post by:
I am using a multiple filters to search for specific criteria. In one of the filters criteria, I need to use the OR instead of the AND as illustrated below. When insterting the OR it will not...
2
by: abirch | last post by:
Hey. So the problem that I'm running into involves auto-numbering invoice numbers. My boss has 3 tables, 2 of which are only to temporarily store information, but when she goes to create a new...
32
LeighW
by: LeighW | last post by:
Hi, I'm still having a couple problems with searches. I have a search form, frm_Search. The form I am trying to filter, frm_Form1 An unbound combobox on frm_Search, Cbo_Permit 6 different...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.