Search
Windows 7

Microsoft Access 2010 : Enhancing the Queries That You Build - Everything You Need to Know About Query Basics

Published 6 min readBy Programming4us Editorial Team

Creating a basic query is easy because Microsoft has provided a user-friendly, drag-and-drop interface. To start a new query, select Query Design from the Other group of the Create tab of the Ribbon; the Show Table dialog appears (see Figure 1). If you prefer, you can select Query Wizard from the Other group of the Create tab of the Ribbon. In that case the New Query dialog appears, allowing you to select from four predefined query wizards (see Figure 2). The Simple Query Wizard walks you through the steps of creating a basic query. The other wizards help you create three specific types of queries: Crosstab, Find Duplicates, and Find Unmatched queries.

Figure 1. The Show Table dialog box.


Figure 2. The New Query dialog box.


Adding Tables to Queries

If you choose to use Design view rather than one of the wizards, the Show Table dialog box appears (refer to Figure 1). In this dialog box, you can select the tables or queries that supply data to a query. Access doesn’t care whether you select tables or queries as the foundation for queries. You can select a table or query by double-clicking the name of the table or query you want to add or by single-clicking the table and then selecting the Add command button. You can select multiple tables or queries by holding down the Shift key while you select a contiguous range of tables or the Ctrl key while you select noncontiguous tables. After you have selected the tables or queries you want, you click Add and then click Close. This brings you to the Query Design window, shown in Figure 3.

Figure 3. The Query Design window.


Adding Fields to Queries

After you add tables to a query, you can select the fields you want to include in the query. The query shown in Figure 3 is based on the Customers table from the Northwind database that ships with Microsoft Access. Notice that the query window is divided into two sections: The top half of the window shows the tables or queries that underlie the query you’re designing, and the bottom half shows any fields that you will include in the query output. You can add a field to the query design grid on the bottom half of the query window in several ways:

  • You can double-click the name of the field you want to add.

  • You can click and drag a single field from the table in the top half of the query window to the query design grid below.

  • You can select multiple fields at the same time by using the Shift key (for a contiguous range of fields) or the Ctrl key (for a noncontiguous range). You can double-click the title bar of the field list to select all fields and then click and drag any one of the selected fields to the query design grid.





Removing a Field from the Query Design Grid

To remove a field from the query design grid, follow these steps:

1.

Find the field you want to remove.

2.

Click the column selector (that is, the small horizontal gray button) immediately above the name of the field. The entire column of the query design grid should become black (see Figure 4).

Figure 4. Removing a field from the query design grid.


3.

Press the Delete key or select Delete from the Edit menu. Access removes the field from the query.

Inserting a Field After a Query Is Built

The process for inserting a field after you have built a query depends on where you want to insert the new field. If you want to insert it after the existing fields, it’s easiest to double-click the name of the field you want to add. If you prefer to insert the new field between two existing fields, it’s best to click and drag the field you want to add and drop it onto the field you want to appear to the right of the inserted field.

Moving a Field to a Different Location on the Query Design Grid

Although the user can move a column while in a query’s Datasheet view, sometimes you want to permanently alter the position of a field in the query output. You can do this as a convenience to the user or, more importantly, to use the query as a foundation for forms and reports. The order of the fields in the query becomes the default order of the fields on any forms and reports you build by using any of the wizards. You can save yourself quite a bit of time by ordering queries effectively.

To move a single column, follow these steps:

1.

Select a column while in the query’s Design view by clicking its column selector.

2.

Click the selected column a second time, and then drag it to a new location on the query design grid.

Move More Than One Column

Follow these steps to move more than one column at a time:

1.

Drag across the column selectors of the columns you want to move.

2.

Click any of the selected columns a second time, and then drag them to a new location on the query design grid.



Saving and Naming Queries

To save a query at any time, you can click the Save button on the toolbar. If the query is a new one, Access prompts you to name the query.


It’s important to understand that when you save a query, you’re saving only the query’s definition, not the actual query result.