Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Computer Science АС (ГСФ) / CS Work at practical classes / 9 Work with package MS Office.doc
Скачиваний:
12
Добавлен:
18.02.2016
Размер:
328.19 Кб
Скачать

Creating a Query with the Query Wizard

Although you can build a query from scratch, the Query Wizard can do the dirty work for you in most cases.

Access includes these four query wizards:

  • Simple Query Wizard— Extracts fields from one or more tables and from other queries.

  • Crosstab Query Wizard— Creates a worksheet-like query that summarizes field values and cross-tabulates matching values.

  • Find Duplicates Query Wizard— Creates a data subset from two or more tables or queries that contain matching values in one or more fields that you select.

  • Find Unmatched Query Wizard— Creates a data subset from two or more tables or queries that contain no duplicate records.

You use the Simple Query Wizard often because of its general-purpose design. When you create a new query by selecting Queries from the Database window's object list and then clicking New on the toolbar, Access displays the New Query dialog box (shown in Figure 19.2).

Figure 19.2 Begin creating your query in the New Query dialog box.

Note

When you create a new query, a list of the four query-based wizards appears and you can select from that list. In addition, a fifth entry appears labeled Design View. The Design View entry is misleading because it is not a wizard but a query-building screen from which you must build the query from scratch without the help from any wizard.

To Do: Build a Query with the Simple Query Wizard

Follow these steps to build your first query using the Simple Query Wizard:

  1. After opening your database, click Queries in the Database window.

  2. Click the Database window's New button to open the New Query window.

  3. Select the Simple Query Wizard from the New Query dialog box and click OK. Access starts the wizard and displays the Simple Query Wizard screen (see Figure 19.3).

Figure 19.3 Use the Simple Query Wizard screen to generate your first query.

  1. Select a table or an existing query that holds the data you want the query to extract. (You can build queries from tables or from other queries.) Access displays fields from the selected table (or query) in the Available Fields list.

  2. Select one or more fields and click the button labeled >. Access includes these fields in the resulting query data subset. If you want the query to extract all customer names and balances from a customer table, for example, select those two fields and click > to send them to the Selected Fields list. The Selected Fields list is the query's resulting structure and holds a description of the data subset that the query eventually produces.

  3. Optionally, select another table or query from the Tables/Queries drop-down list and add more fields to the Selected Fields list. If Access prompts you to create a relationship between the tables, click Yes to create the relationship. Access relates the tables automatically if you created the relationship link elsewhere. You are now building a query that extracts data subsets from multiple sources. If you send the wrong field to the Selected Fields list, select the incorrect field and click < to remove it. To remove all your selected fields and begin again, click the button labeled <<.

  4. Click Next to select either a detail or summary query. A detail query includes every field of every record; a summary query does not show duplicate selected records and includes summary statistics if you select them by clicking the Summary Options button.

  5. Click Next and select a title for the query. Access bases the default name for the new query on the first selected table or existing query.

  6. Click Finish to complete the query. Access builds the query and displays the selected records from the query in the Datasheet view. When you close the Datasheet view, you see the new query listed on the Queries page of the Database window. Again, you can often create a filter that produces the same extracted record subset as a query, but you can later re-extract queries by name instead of rebuilding them from scratch as you must do with filters.

The resulting query is just a smaller table, a subset of the original table, that your query generated. For the first time, you have the power to extract records from multiple tables to create a dynaset, or subset, of your database records.

You can also create a new form (using one of the form wizards) to display the results of a query. When you created a new form in Hour 18, "Entering and Displaying Access 2003 Data," you knew only how to create forms from single tables. If multiple tables contain data you want to display in a form, however, create a query to extract from the two tables and base the form on that query.

Note

To synchronize a multiple-table query, all tables must have a common field (such as a customer number), or you must use advanced Access commands to relate the two tables in some way. Without a relationship, such as a common field, the query cannot combine the fields from the two tables.

Access does not save your query results, just the query structure. Therefore, if you want to see a data subset twice, you must open the Database window's Queries page, select the query, and click Open to generate the query extraction once again. Although the extraction requires a little time to generate (usually the speed is negligible unless the tables contain many records), the query is always fresh. If you change one value in any table and open the query again, your most recent table edit appears in the query.

Another advantage of generating the query every time you need to use the data subset is that Access does not have to store the data twice (once in the source tables and again in the query).

Note

If you edit data from the resulting query's Datasheet view (or from the query's Form view), Access updates the data in the original tables. Suppose that you want to edit the pay rate for every employee who works in your company's Northeast division; just create a query to extract only the Northeast division employees, make the edits, and close the query. Find and edit the Northeast employees' pay rates without the other employee records getting in the way.

Соседние файлы в папке CS Work at practical classes