Data Management

10+ tips for working efficiently in Access' Query Design window

If you create Access queries, you probably know your way around the Query Design window. But there are some not-so-obvious tricks you can use to work more efficiently, maintain accuracy, and ensure database integrity.

This article is also available as a PDF download.

To create an Access query, you use the Query Design window. You specify a few data sources and fields, add a criteria expression or two, and perhaps specify a sort order. What you might not realize is that there's more to working in this window than just creating a query. Working efficiently in this environment can help you maintain accuracy and database integrity.

#1: Mastering the terminology

Queries come with a few terms you might not associate with their graphic counterparts. With that in mind, here are a few environmental terms you might read in articles and documentation:

  • Design view — This is the window you use to design a query. You might also see this window called the Query Design window or the Query Builder. The Query Builder is actually a different interface from the Query Design window. Access displays this window when you define a query as a control's data source.
  • Datasheet view — This is the table-looking view of a query's resulting recordset. There are other views, but you'll usually work with Datasheet view.
  • QBE grid — This is the lower pane in the Query Design window's graphic interface. (QBE stands for query by example.) Use the bottom pane to specify the fields upon which the query acts.

#2: Adding fields to the QBE grid

The upper pane of the Query Design window (Design view) displays a query's data sources. If you want the query to act upon a field, you must drag that field to the QBE grid (the lower pane). Fortunately, Access supports a number of ways to do this:

  • The most obvious way is to type the field's name into a Field cell. As you type, Access selects the most appropriate field name from the data source. If there's more than one table and the same field name occurs in both, you must also specify the source table in the Table cell. If you don't, Access defaults to the first table (left to right in the upper pane) that contains the field.
  • Perhaps the most efficient and trouble-free method is to select the field from the Field cell's drop-down list. Doing so eliminates errors. If there are multiple tables, the list displays all the fields from all the tables, using the table.field format, which can make this method a little more difficult to use.
  • You can use the drag-and-drop method to drag a field from a field list in the upper pane to a column in the QBE grid. In the field list, click the field you want to add to the query. Then, while holding down the mouse, drag the field to the QBE grid.
  • A quick double-click will also do the trick. Simply double-click a field to add it to the QBE grid.

If you enter a field name that doesn't exist or you create an ambiguous reference, Access won't run the query, so you don't have to worry about this type of mistake fouling up a query's results. However, you will waste time finding and correcting the error. For this reason, it's best to avoid typing field and table names.

#3: Quickly adding all the fields to the QBE grid

Some queries require all the fields from a data source. When this is the case, you could use the asterisk character (*) in the QBE grid to represent all of the fields. You'll find it at the top of each field list. However, you should avoid using the asterisk character because it has limits. For instance, you can't include an aggregate function or run a Totals query against the asterisk character. The limits make perfect sense, but the asterisk character has a way of turning up an error when you least expect it.

Fortunately, there's another easy way to add all fields to the QBE grid. Double-click the field list's title bar to select all of the fields. Then, simply drag the entire block of fields to the grid. Access will fill the grid in the order the fields occur in the list.

#4: Dragging contiguous and noncontiguous blocks

You can drag a contiguous or noncontiguous block of fields to the grid. To select a contiguous block of fields, click the first field name in the block. Then, hold down the [Shift] key and click the last. Access will select the two clicked fields and all the fields in between. Once Access selects the block, drag it to the QBE grid. Selecting a noncontiguous block is similar. Hold down the [Ctrl] key and click each field name. When you're finished, drag the block to the QBE grid. When dragging multiple fields to the QBE grid, Access always inserts the fields in list order.

#5: Having it all

Although you can easily add all the fields in a data source to the QBE grid, and ultimately, to your query, you probably shouldn't. Add only the fields that your query needs: fields that are part of a criteria expression, fields you need to sort or group by, and fields you need to view.

Keep in mind that bound objects have access to only the data specified in the query. If bound objects need more data, set the query's Output All Fields property to Yes. Then, any bound object will have access to all of the underlying data source's fields. To access this property, click the Properties button in Design view.

#6: Setting query properties

In Design view, you can click the Properties button to display a list of query properties. Alternately, you can right-click the query window's background and choose Properties from the resulting submenu to display the same list. Although the latter method requires an extra click, it's good to know you can access properties this way in case the Query Design toolbar isn't available.

#7: Reading the Jet SQL behind your queries

Access communicates with Jet using Jet SQL. The Query Design window is just a graphical interface that allows you to create the appropriate SQL statement, without actually writing the statement yourself. From the choices you make in the QBE grid, Access creates the SQL statement the Jet engine requires to run the query. If you'd like to see the SQL behind your queries, choose SQL View from the View button on the Query Design toolbar. The quickest way to learn Jet SQL it is to read it.

#8: Combining nested queries

You probably know that you can base one query on another. This arrangement is known as nested queries. Often, you can replace nested queries with a subquery — a SQL statement that contains a second SQL query. However, writing a subquery from scratch can be difficult, even if you're familiar with Jet SQL.

Fortunately, you can use the SQL View window (#7) to help create a subquery. To get started, view the first query in SQL View and copy it to the Clipboard. Next, open the second query and add the WHERE keyword (usually at the end of the statement). Then, paste the first query beneath the new WHERE keyword and enclose the entire copied SQL statement in parentheses. Replace the query reference with the appropriate table name(s). Now, this method isn't foolproof for every subquery, but it's a good start.

#9: Viewing results without running the query

Running a SELECT query retrieves and displays data, but it doesn't change underlying data. For this reason, clicking Run to see the results is a safe choice. But that won't always be the case. If you run an action query — an Update, Delete, or even an Append query — at the wrong time, you could seriously compromise your application.

Fortunately, you can test an action query's results without actually committing its changes. Simply choose Datasheet View from the View menu on the Query Design toolbar. Doing so lets you see what the query will do if you actually run it.

If, for some reason, Datasheet View isn't available, run the action query as a Select to view the results. Once you're sure the query does the job you want, change the query type and run it as an action query.

#10: Building relationships

To create a relationship between two tables, you probably use the Relationships window (click Relationships on the Database toolbar). The resulting window displays existing relationships and allows you to create new ones. You can also create and manipulate relationships in the Query Design window. Simply drag related fields between the field lists in the upper pane. Then, right-click the resulting join line to modify the relationship, if necessary. However, you must remember that any relationship you create this way is valid for that query only. This relationship will not affect any other queries.

#11: Rearranging columns

Order matters in some queries. For instance, if you specify a sort order for more than one field, Access sorts from left to right. Sometimes you must rearrange columns in the QBE grid to get the sorted or grouped results you want. To do so, click the thin bar at the top of the column you want to move to select the entire column. Then, click the selected column, hold down the mouse, and drag the column to its new position. When you close the query, Access will prompt you to save changes you made to the layout. Click Yes only if you want to retain the new column position.

#12: Using SQL View to build and debug

The SQL window (#7) doesn't just display SQL statements. Take full advantage of this view to build and debug SQL statements you execute via code. First, create a SQL statement you intend to use in code in the Query Design window. Then, copy the statement from the SQL View window to a code module. You can also use SQL View to debug SQL statements you're already using in code. Copy the erring statement from the module to the SQL View window and run it. Generally, this window displays a much more helpful error message than the coding environment. Run the statement via the SQL View window as many times as necessary to correct the problem. Then, copy it back to the code module.

 


Susan Sales Harkins is an independent consultant and the author of several articles and books on database technologies. Her most recent book is Mastering Microsoft SQL Server 2005 Express, with Mike Gunderloy, published by Sybex. Other collaborations with Gunderloy are Automating Microsoft Access 2003 with VBA, Upgrader's Guide to Microsoft Office System 2003, ICDL Exam Cram 2, and Absolute Beginner's Guide to Microsoft Access 2003, all published by Que. Currently, Susan volunteers as the Publications Director for Database Advisors. You can reach her at ssharkins@setel.com.

About Susan Harkins

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

Editor's Picks

Free Newsletters, In your Inbox