[ Team LiB ] |
Recipe 1.1 Specify Query Criteria at Runtime1.1.1 ProblemWhen you design a query, you don't always know which subset of records you would like to see when you run the query. Instead of creating several queries with the same basic design but slightly different criteria, you'd like to be able to create one query that can be used to return the same fields, but a different set of records, each time it's run. 1.1.2 SolutionUse a parameter query with one or more replaceable parameters that it will request at runtime (when you run the query). This solution demonstrates how you can create and run parameter queries using the default parameter prompt. Here are the steps to create a parameter query using default prompts:
Figure 1-1. The qryAlbumsPrm1 parameter in design view
Figure 1-2. The Enter Parameter Value dialog for qryAlbumsPrm1To see how this works using the sample database, open 01-01.MDB and run the qryAlbumsPrm1 query. You will be prompted for the type of music. Enter a music type, such as rock, alternative rock, or jazz. The query will then execute, returning only the records of the specified music type. For example, if you enter "Alternative Rock" at the prompt, you'll see the datasheet shown in Figure 1-3. Figure 1-3. The datasheet for qryAlbumsPrm11.1.3 DiscussionFor queries with simple text parameters, you can get away without declaring the parameter using the Query Parameters command. If you create parameters for crosstab or action queries, however, you must declare the parameter. We recommend that you get in the habit of always declaring all parameters to eliminate any chance of ambiguity. The entries you make in the Parameters dialog end up in the Parameters clause that is added to the beginning of the query's SQL, which you can see by selecting View SQL View. The result of a parameter query needn't be a query's datasheet. You can base reports, forms, and even other queries on a parameter query. When you run the object that is based on the parameter query—for example, a report—Access knows enough to resolve the parameters prior to running the report. You can use parameters in any type of query, including select, totals, crosstab, action, and union queries. |
[ Team LiB ] |