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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a string into a query criteria

Status
Not open for further replies.

jonohara

Technical User
Mar 26, 2000
53
GB
I need to run a query, the criteria for which, I hope to pass in from a string variable on a form.

i.e. I have a string variable e.g. 'ref1, ref2, ref3' (built up from a multiselect listbox) The query has a corresponding reference field. Can I set the criteria for the query using this variable? (it currently only works when the string contains a single item e.g. 'ref1').

I would prefer not to have to create a query on the fly if it can be avoided.

Thanks
 
This will work if you build your string correctly and use the in clause

because you are not using numbers you need to use the single quote around each so your string looks like
strcrit ="'ref1', 'ref2', 'ref3'"
then you can say "where this in (" & strcrit & ")"

If ref1 and ref2 are field names then you would do more like
strcrit ="ref1, ref2, ref3 "
srtsql = "select " & strcrit & "from tblname"
 
Create a table called "criteria", with columns corresponding to the values you wish to pass. (example: Customer, Date, Item)
In your code when you wish your query to run, clear this table, then write what you want to pass to the query. Have your query use this table .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top