Professional Documents
Culture Documents
The last few tutorials have been primarily concerned result is that parameters can be used to create
with interface issues. In the remaining tutorials, the extremely flexible queries.
focus shifts to transaction processing. When the concepts from this tutorial are combined
with action queries (Tutorial 11) and triggers
10.1 Introduction: Dynamic queries (Tutorial 13), you will have a the skills required to
using parameters create a simple transaction processing system with-
A parameter query is a query in which the criteria out writing a line of programming code.
for selecting records are determined when the query
is executed rather than when the query is designed. 10.2 Learning objectives
For example, recall the select query shown in What is a parameter query? How do I create
Figure 4.6. In this query, the results set is limited to one?
records that satisfy the criterion DeptCode = How do I prompt the user to enter parameter
“COMM”. If you wanted a different set of results, you values?
would have to edit the query (e.g., change the crite-
How do I create a query whose results
rion to “CPSC”) and rerun the query.
depend on a value on a form?
However, if a variable (parameter) is used for the cri-
terion, Access will prompt the user for the value of
the variable before executing the query. The net
(here
Run the query and supply a parameter value
Access is asking for the value of X).
(“COMM”)
Replace the literal criterion
with a parameter (X)
Note that the spelling mistakes discussed in may be useful to be able to generate a list of courses
Section 4.3.4 are processed by Access as offered by the department currently being viewed.
parameters. Although you could use a creatively-named parame-
ter to invoke the “Enter Parameter Value” dialog, this
10.3.2 Using parameters to generate requires the user to type in the value of DeptCode.
prompts A more elegant approach is to have Access pull the
Since the name of the parameter can be anything value of a parameter directly from the open form.
(as long as it is enclosed in square brackets), you This exploits the second step in the operation of a
can exploit this feature to create quick and easy dia- parameter query (Access will attempt to resolve a
log boxes. parameter with the value of an object within the cur-
• Change the name of your DeptCode parameter rent environment). The basic idea is shown in
from [X] to [Courses for which depart- Figure 10.3.
ment?]. The key to making this work is to provide a parame-
• Run the query, as shown in Figure 10.2. ter name that correctly references the form object in
which you are interested. In order to avoid having to
10.3.3 Values on forms as parameters remember the complex naming syntax for objects on
A common requirement is to use the value on a form forms, you can invoke the expression builder to
to influence the outcome of a query. For instance, if select the correct name from the hierarchy of data-
the user is viewing information about departments, it base objects.
the
When Access asks for
value of the
parameter, it uses the
parameter’s name.
for
Name the parameter [Courses
which department?].
• Create a very simple form based on the Although the naming syntax of objects in
Departments table and save it as frmDepart- Access is tricky, it is not impossible to com-
ments. prehend. For example, the name
• Leave the form open (in form view or design Forms![frmDepartments]![DeptCode]
mode, it does not matter). consists of the following elements: Forms
• Open pqryCourses in design mode, place the refers to a collection of Form objects; [frm-
cursor in the criteria row of the DeptCode field, Departments] is a specific instance of a
and invoke the expression builder as shown in Form object in the Forms collection; [Dept-
Figure 10.4. Code] is a Control belonging to the form. See
• Perform the steps shown in Figure 10.5 to create Tutorial 14 for more information on the hierar-
a parameter that references the DeptCode field chy of objects used by Access.
on the frmDepartments form.
• Run the query. The results set should correspond 10.4 Application to the assignment
to the department showing in the frmDepart- You will use parameter queries as the basis for sev-
ments form. eral action queries (see Tutorial 11) that process
• Move to a new record on the form. Notice that transactions against master tables. For now, simply
you have to requery the form (Shift-F9) in order create the parameter queries that take their criteria
for the new parameter value to be used (see values from forms you have already created.
Figure 10.6).
Criteria
Place the cursor in the
row of the
DeptCode field and
right-click to bring up
the pop-up menu.
Departments
Create a simple form based on the
table and leave it open
in the background.
Select Build to
invoke the builder.
FIGURE 10.5: Use the builder to select the name of the object you want to use as a parameter.
the
Select Forms to get a list of all
forms in your database.
form
Since the frmDepartments
is open, click on Loaded
Forms and select the form.
Press OK
when done.
Move to the middle pane and
select Field List to get a list of the
The text will
be copied
fields on the form in the pane on into the
the far right. criteria row.
FIGURE 10.6: Requery the results set to reflect changes on the form.
form.
Move to a new record on the
Notice that the query is not
automatically updated.
parameter
Press Shift-F9 to requery. The new
value (MATH in this case)
is used to select records.
• Create a parameter query to show all the order • It shows the change (positive or negative but not
details for a particular order. zero) in backorders for each item in a particular
• Create a second parameter query to show all the order.
shipment details for a particular shipment. • The query consist of three fields: OrderID, Pro-
Each order may result in a number of changes being ductID and a calculated field Qty (i.e., the
made to the BackOrders table. For some items in change in the back order for a particular product).
the order, more product is ordered than is actually • The name of the parameter is in this query is sim-
shipped (i.e., a backorder is created). For other ply[pOrderID]. Since the value of this parame-
items, more product is shipped than is ordered (i.e., ter will be set by the Visual Basic shortcut before
a backorder is filled). the query is run, there is no need to set it to a
value on a form.
In Tutorial 15, you are supplied with a “shortcut”
Visual Basic procedure that makes the changes to Since the query is accessed by a program,
the BackOrders table for you. However, the short- the name of the query and all the fields must
cut procedure requires a query that lists the changes be exactly as described above. In other
that must be made to the BackOrders table for a words, you are given a precise specification
particular order. The requirements for this query are for a database object that fills a role in a pro-
the following: cess designed and implemented by someone
• The name of the query is else. You will not understand how the query
pqryItemsToBackOrder fits in until Tutorial 15.