multi field search criteria problem (1 Viewer)

itchy

Utterly confused since...
Local time
Today, 20:53
Joined
Jan 25, 2001
Messages
31
Hi All,
This is a little long winded but this problem has had me stumped for weeks!
I have a problem concerning multiple criteria in a query. I have a search form that has 3 unbound text boxes. The form also has a subform that is tied to a query. Each text box on the form is tied to a specific field in the query. (e.g textbox1 searches in the strName field, textbox2 searches in the strCompany field, etc.) When a user enters some text in the text box, they then click a button that requeries the subform displaying the records where the text is found in the corresponding field. If only one text box has search criteria entered then the search is performed only on that field, if two text boxes have criteria entered then an “And” search is performed on both fields. I am using the following in the criteria field in my query:

Like “*|Forms![frmDataSearch]![strCriteria1]|*” & “*”

I place a statement similar to this in all of the three fields that I wish to search on. Here is the problem, all 3 fields in the underlying table must have data in them in order for all records to appear. If just one of the fields is null (or “”), the entire record is filtered out of the query. Here is an example:

Record in table:
strName: Bob
strCompany: “”
strRegion: North America

Text box craters
textbox1(search in Name): bob
textbox2(search in Company):
textbox3(search in Region):

This actually returns no records because the strCompany field in the underlying table is empty. What I basically want is that if no criteria is specified in any textbox then show all records, even those where the search fields may be empty strings. If only one of the textboxes has criteria specified, then show all records where that field is similar to the criteria, including those where either of the other two fields may contain empty strings, an so on…
I have tried adding to the search criteria in the query an “Or” condition, but this doesn’t work:
Criteria in query: Like “*|Forms![frmDataSearch]![strCriteria1]|*” & “*” Or “*”
I have tried creating a function to assess the value of the textbox and return one of two possible criteria strings but this also doesn’t work:
Criteria in query: Criteria(Forms![frmDataSearch]![strCriteria1])
It seems that no matter what I put in the criteria of the query, as soon as there is a criteria expression specified, it automatically filters out any records where this field is null or an empty string.

Any ideas?

Itchy
 

vandewi

Senior Member
Local time
Today, 15:53
Joined
Aug 3, 2001
Messages
26
Create 3 new fields in your query. The first one checks the value of textbox1 and ignores it if it is null. In the FIELD box of the query, type:
IIF(isnull([Forms]![frmDataSearch]![textbox1]),-1,[strname] Like *&[Forms]![frmDataSearch]![textbox1]&*

When you hit ENTER, Access will place a expr1 in front of your expression. Now you must place the value "-1" (no quotes) in the criteria section.

Repeat this process for the other fields.
 

vandewi

Senior Member
Local time
Today, 15:53
Joined
Aug 3, 2001
Messages
26
By the way, I am missing a close parethesis on the end of the previous expression.
 

TheDeal56

Registered User.
Local time
Today, 14:53
Joined
Jul 24, 2007
Messages
23
Is there any way you can post an example of this db? I think this would help me a lot. Thanks
 

Users who are viewing this thread

Top Bottom