A query specifies criteria that indicates which data fields and data rows are displayed to you. While this is similar in capabilities to the Filter by Form discussed in the datasheet view, a query can be created that combines data from two or more tables.
To create a query object in Microsoft Access you first click on the Query button in the list of object types. As shown in the picture to the right, clicking on the Query object type displays two methods for creating a new query object: In this class, we will learn both methods for creating a query. |
In the database window, double-click on the "Create query by using wizard" entry to start the wizard.
Query Wizard Step 1In the first step of the Query Wizard, you must select the fields you want to display on your query results.
|
|
In this demonstration, the First Name, Last Name, City, and Home Phone Number fields in the Student table were moved to the Selected Fields list. |
|
Query Wizard Step 2 (last step)In the second step of the Query Wizard:
The question "What title do you want for your query?" is misleading. In this wizard, the word "title" means name. |
In this demonstration, the "title" was changed to StudentQuery and the option to display the results (view information) was selected. The datasheet view displayed at this time looks like the picture on the right: Please see the query datasheet view for information on this view. |
In the database window, double-click on the "Create query in Design view" entry to create a query using the design view.
Show TableBefore the query design view is displayed, you must select one or more tables or queries to use as the basis for this new query. At the moment, the database only contains one table - Student - so this is already selected in the list of tables. Clicking the Add button will add the Student table to the query design view, which is currently displayed behind the Show Table dialog box. At this point, the Close button may be clicked to close the Show Table dialog box. |
The query design view displayed at this time looks like the picture on the right: The query design view contains two sections.
An explanation of each row shown in the bottom section follows.
|
Each of the following examples assumes that you've double-clicked on the "Create query in Design view" entry in the database window and that you added the Student table to the query design view.
The First Name field will appear in the bottom section of the query design view in the first column.
The Last Name field will appear in the bottom section of the query design view in the second column.
The City field will appear in the bottom section of the query design view in the third column.
The Home Phone Number field will appear in the bottom section of the query design view in the fourth column.
At this point, the query design view looks like the following:
The data resulting from executing this query is displayed in a datasheet view. This datasheet view would look like:
The data resulting from executing this query is displayed in a datasheet view. This datasheet view would look like:
Comparing these results with example A, we see that only those students that live in Syracuse AND whose first name begins with an M are shown.
The data resulting from executing this query is displayed in a datasheet view. This datasheet view would look like:
Comparing these results with example A, we see that only those students that live in Piscataway OR whose first name begins with an M are shown.
The data resulting from executing this query is displayed in a datasheet view. This datasheet view would look like:
Comparing these results with example A, we see that only those students that live in Syracuse OR Piscataway are shown.
The datasheet view used to display query data has the same features and capabilites as the datasheet view used to view table data.
This view displays the Structured Query Language (SQL) statement that Microsoft Access generated based on your query design.
When you run a query, Microsoft Access actually executes the SQL statement displayed in this view. The results produced by executing the SQL statement are then displayed in a datasheet view.
For those of you that are curious, here are the SQL statements generated by Microsoft Access for the four query examples (A-D) described above.