MS Access

Parameter Queries

Getting tired of modifying a query every time you want to use a new criterion? A parameter query is your answer. A parameter query prompts the user for the query's criteria.

For example, you could create a Regional Sales query that would ask for the name of the state that you want to filter by.

Enter a by entering a message or prompt enclosed in [brackets] in the field's criteria row.

The parameter query prompts the user to enter the state.

The results of the parameter query.

Creating a parameter query is easy. All you have to do is click the Criteria row for the field that you want to use as a parameter and type a message, enclosed in [brackets], that you want Access to display when you run the query, as shown in figure.

  1. Click the qryTourSales query and then click the Design button.

    As always, you need to add the field that you want to group data by onto the design grid. This time we want to create a query that summarizes total employee sales. Here are the fields that we will use in the query:

  2. In the tblEmployees field list, double-click the FirstName, LastName, and State fields, and in the tblCustomerTours field list, double-click the Cost and Date fields.

    Next you need to tell the query that you want to group and summarize the query.

  3. If necessary, click the Totals button on the toolbar.

    The Total row appears. You need to specify which field(s) you want to use to group the query, which field(s) you want to be calculated, and which field(s) you want to use to limit the number of records displayed in the query.

    You want to group records using the FirstName, LastName, and State fields and find the total of the Cost field.

  4. Click the Cost column's Total row, click the list arrow, and select Sum from the list.

    This will total the Cost field. We will use the Date field as criteria to limit the records to those that fall between two dates. Instead of entering a criteria expression with two fixed date values, such as "Between 1/1/00 and 3/31/00," we will create two parameters that will prompt the user to enter the two date values each time they run the query.

  5. Click the Date column's Criteria row and type Between [Enter start date] and [Enter end date].

    You've just created two parametersthe [Enter start date] parameter and the [Enter end date] parameter.

    Since the Date field is only being used as a criteria field, you need to select the "Where" option from its Total row.

  6. Click the Date column's Total row, click the list arrow, and select Where from the list.

    The "Show" check box automatically unchecks itself, indicating that the Date field will not appear in the query results.

    Let's add one more parameterone that prompts the user to enter the state where the tour was sold.

  7. Click the State column's Criteria row and type [Enter the state].

    This will prompt the user to enter the name of the state.

    Since the State field is only being used as a criteria field, you need to select the "Where" option from its Total row.

  8. Click the State column's Total row, click the list arrow, and select Where from the list.

    Let's test our query.

  9. Click the Run button on the toolbar.

    Access prompts you to enter the first parameter, as shown in figure. You want to summarize records from Washington.

  10. Type WA and click OK.

    Access prompts you for the next parameterthe start date.

  11. Type 1/1/00 and click OK.

    Access prompts you for the last parameterthe end date.

  12. Type 6/30/00 and click OK.

    Access displays the results of the parameter query, as shown in figure. The order of your query may be different if you put the FirstName field on the design grid before the LastName field.

  13. Save the query as qryParameter and then close the query.

Some advanced Access developers use custom-made forms to provide parameter queries with their information. If developers bind a parameter to the controls on a form (such as [frmCustomers]![Name]), users can fill out one dialog box instead of having to fill out five or six pop-up dialog boxes.

To create a parameter query:

  1. display the query in design view.

  2. click the criteria row for the field you want to use for your parameter criteria and enter the text of the prompt, surrounded by square brackets [ ].

  3. click the view button or run button on the toolbar to run the query.

  4. enter a criteria value in response to the prompt and click ok.

by BrainBellupdated
Advertisement: