Form criteria to include and exclude results

cpclifford

New Member
Joined
Mar 9, 2011
Messages
2
I have a form that allows users to enter search criteria in two item description boxes. One box is criteria to search for and include results while the other asks for criteria to exclude. Both of these boxes make use of wild cards "*" for user ease. For instance, I want a user to be able search for and include "battery" but exclude "aaa". Currently, the query works if the exclude box is populated. However, if it is not populated, access excludes all results because of the wildcard. Is there a way to tell access to search based on the include/ exclude criteria if populated but if the exclude criteria is blank (null) to search only on the include criteria? I have attempted to write an iif statement but cant seem to get it to work.

I am currently using the statement below, but it is returning zero results if the exclude box is left blank due to the wildcard. Wildcards are required.

Like ("*" & [Forms]![FRM_SEARCH_DB]![SEARCH_ITEM] & "*") And Not Like ("*" & [FORMS]![FRM_SEARCH_DB]![SEARCH_EXCLUDE] & "*")

Any suggestions? Thanks in advance, Chris
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Behind your search button set the form's filter like this

Code:
If Not IsNull(Me!Search_Exclude) then
  me.filter="Like ('*' & Me![SEARCH_ITEM] & '*') And Not Like ('*' & Me![SEARCH_EXCLUDE] & '*')"
else
  me.filter="Like ('*' & Me![SEARCH_ITEM] & '*')"
end if
me.allowfilters=true

Or something to that effect.

hth,

Rich
 
Upvote 0
Rich- Thanks for the response. I am fairly new to Access and forms and not familiar with code at all. Is there a way I can set the filter in Design View? Possibly on the Data Property Sheet? I don't fully understand what you mean by "behind your search button"

Thanks, Chris
 
Upvote 0
ah, you can't really dynamically set a property without code. I assume you have a button that the user clicks to find the records? Set this button's On Click property to [Event Procedure] then click the elipsis (...) to open the code pane. Access should create an On Click event stub for you. Paste the code in there so it looks like this (where Command1 is the name of your button).

Code:
Private Sub Command1_Click()
If Not IsNull(Me!Search_Exclude) then
  me.filter="Like ('*' & Me![SEARCH_ITEM] & '*') And Not Like ('*' & Me![SEARCH_EXCLUDE] & '*')"
else
  me.filter="Like ('*' & Me![SEARCH_ITEM] & '*')"
end if
me.allowfilters=true
End Sub

If you have some other method of enacting the find function other than a button let me know. We'll find a place to put this code.

hth,

Rich
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,777
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top