Use Forms to Control Queries

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm just a beginner…

How can I use a Form to filter my query results?

For example: I want to see product and order information in form view for
account "AAM". In design view of a query I'll change the criteria to "AAM"
and then run the query. Can I do the same thing using a form?

Can I change the criteria for the underlying query with a Form or it's
controls?

Type in or select the criteria from a drop down would be nice.
 
Yes, you can use a form to control the query. You start with a "parameter
query". This is a query where the criteria has been replaced with a "name".
The query will pop-up an input box using that "name" to prompt you for
information, so usually you make the "name" something that would make sense
to the user, such as [Please give the starting date]. If this was placed in
the query as the criteria for a field, when the query was opened or run an
input box would pop-up with the phrase "Please give the starting date", a
textbox for the user to make their input, and Ok and Cancel buttons.

To make this would using a form, instead of a general statement as was used
above, you would give the "path" to a control on a form (ex.
[Forms]![Form1]![MyTextbox]). The query would then look at the textbox named
MyTextbox on the form named Form1 for the value of the criteria. On the
form, you would have a caption for the textbox that describes to the user
what you want them to enter. You can then use the coding capabilities of the
form to verify that they've entered a valid value. You can also use the
Input Mask of the textbox to limit what the user can enter or the format in
which they enter it.

Unlike the input box, the query will not automatically open the form for the
user's input. You'll have to open the form yourself at the appropriate time,
have the user make the entry, then open or run the query. The form must be
open for the query to find it, if the form isn't open, the query will assume
that the parameter in the criteria is just text as it was in the first
example and will pop-up an input box asking for the value and using the text
as the prompt, which will probably be meaningless to the user.

If the parameter value is a Date/Time value, I recommend that you specify
the data type of the parameter or it may not work correctly. To specify
this, with the query in design view go to Query|Parameters... on the menu
bar. Under "Parameter" enter the text exactly as you have it in the criteria
of the query (using the example above, this would be
[Forms]![Form1]![MyTextbox]) (copy <Ctrl+C> and paste <Ctrl+V> will work for
this, if desired). Next, select the "Data Type" Date/Time from the drop down
in the right hand column and click Ok to save. You can have more than one
parameter in a query, so if you want the user to make criteria selections
for multiple fields in the query, you can.
 
You can set the form's Filter property (and set FilterOn to True), or you
can change the SQL used for the form's RecordSource and do a requery.

If you're selecting from a combo box, put code in the AfterUpdate event to
do either of those two suggestions above.
 
Back
Top