I've got two methods on this one. The first solution uses a query designed
in Access, and the second uses SQL in VBA code.
First, we'll stick with what you seem to be using - an query built through
Access. The easiest way to jury rig this is to create a table with a single
row to hold database settings. Make the columns whatever you need to match
the query you are trying to build.
So for instance, let's say the WHERE section of your SQL statement has two
criteria - a string last name and an integer age. In our settings table we
will create three columns - an autonumber 'index' for easy tracking, a string
'last', and an integer 'age'.
Now, go back to your query and add this new table to the mix. If you want
the WHERE criteria to include people older than the number in the age field,
set your query to read:
WHERE age = DLookup("[age]","settings","[index] = 1")
You can see where this is going. Just make sure to add controls to the form
to save the data to the settings table before running the query
The second solution is to just use VBA. Probably the easier solution,
though some people argue that it is not the fastest to execute. I argue
otherwise.
Open the query in SQL view and copy the text you see. Then, take it to the
form's code and assign it to a string. Add the WHERE components you want,
and then run it! Easy as pie. ... though I should probably show you an
example.
dim strSQL as string
strSQL = "SELECT leadnum FROM clients WHERE age > " & me!age & " AND last =
" & me!last & ";"
docmd.runsql strSQL
Of course, you could always just let the user type in their own SQL to a
text box and add that: strSQL = "SELECT leadnum FROM clients WHERE " &
me!where
I don't think they'd like that though.
Hope that helps!
Nick
Warden said:
I have a field on a form that I would like to use in the "where clause" of a
query.
How can I automate this so that I do not have to open the query in design
view fill in the criteria field then run the query. I would like to just
click a command button and run the query with the "on current" criteria or
generate a report.