Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query with Optional Criteria

Status
Not open for further replies.

risoam

MIS
Apr 17, 2002
33
US
I am trying to write a query that reads data from one tables based off of criteria on a form. All of the criteria fields are optional. If no criteria was specified then all records should be returned. Any and all criteria is optional and should return all unless specified. Does anyone know how to do this? Thanks.
 
You need to build an SQL statement in VBA to do this. Basically you'd set up the query and then add criteria if the control isn't blank...something like this:

SQLstring= "Select * FROM TheTable"
if txtPlace <>&quot;&quot; then
SQLstring=SQLstring & &quot; WHERE Place = &quot; & txtplace
end if

That's a very basic set up of what you're asking...hope that helps.

Kevin
 
I see, but I have a few fields that this applies to. Can I do this in an Access Query? Or do I have to build it outside of it? Thanks.
 
I don't know of a way to do it in a query. You can still use code for a few fields, it's just more complicated since you can only add &quot;WHERE&quot; once and other little things like that. The code isn't that hard, but it's definitely more difficult than just creating a query. Maybe someone else has a better idea for you...sorry.

Kevin
 
I found one way to do it.

iif(isnull([form].[fieldname]),[tablename].[fieldname],[form].[fieldname])

This works but it seems a bit slow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top