Will:
Parameters in a query don't have to be the simple system generated prompts
which you get if you enter something like [Enter Name:] as a parameter for a
column in the query; they can equally well be a reference to a control on an
open form. If you use controls on a form as the parameters the difference
between this and simple system generated prompts is that all the parameters
are entered together, rather than having a separate prompt for each pop up
one after the other. This is less confusing to the user and the overall
effect is more professional.
Lets say the query underlying your subform has three parameters. You would
have three controls on an unbound dialogue form, one for each. They can be
text boxes, combo boxes, list boxes, whatever is most appropriate for the
field in question. With fields such as a name where there will be a fixed
set of names in the database at any one time to choose from a combo box from
which the name can be selected from a sorted list would usually be the best
solution. With dates a text box is likely to be better as the dates entered
as the parameter values are less likely to be a fixed set of values in the
database; you might want to enter a start date and an end date of a range for
instance to return rows with dates within the range.
As well as the controls in which the parameter values are entered or
selected you need a button on the dialogue form to open the main form. It
doesn't matter whether the query which references the controls on the form
underlies the main form or a subform within in it, you just enter a reference
to each control as the parameter in the criteria row of the relevant column
in query design view. Say the dialogue form is called frmSearchDlg and a
combo box on it in which you select a name is called cboNames you'd simply
enter Forms!frmSearchDlg!cboNames in the criteria row of the relevant column
in the query in design view. References to other controls on the form would
be entered in the same way.
The button on the dialogue form would open the main form with something like
this in its Click event procedure:
DoCmd.OpenForm!YourMainForm
or the button could run a macro to open the form. When the form opens its,
or its subform's, underlying query would reference the controls on the
dialogue form, so only the row(s) which match the values entered into the
form would be returned.
When you want to open your main form you don't open it directly but open the
frmSearchDlg form. Values are then entered into the controls on it, which
you'll have labelled appropriately, and the button clicked to open the main
form. As far the user is concerned they are still being prompted for
parameter values, but in a single form rather than one by one.
To see an example of a query which references a control on a form as a
parameter in this way take a look at the Invoices Filter query in the sample
Northwind database. This query is not used as the RecordSource of a form or
report, but instead as the filter setting for the OpenReport method in the
code behind the Print Invoice button on the Orders form. Nevertheless the
query serves to illustrate how a parameter can be a reference to a control on
a form. The form is not a dialogue form in this case but a form bound to the
Invoices table, so the reference to the InvoiceID control means the query
returns the current invoice record, but the principle is the same.
Ken Sheridan
Stafford, England