Quamut: the go to how to.
 
 
 
Published_by_bn Sign In Help_but My_quamut_but
 
 
 
   Access 2007 found in Computers & Technology  :  Software  :  Microsoft A   A   A
text size
 
Add to my favorites Send this Quamut to a friend del.icio.us
 

How to Build Access Queries

If you create an Access database with more than one table, you’ll need to use queries to search, filter, add, modify, or calculate the data across your database’s various tables. You can also use queries to refine or modify existing queries you’ve already built. Once you’ve built a query, you can run (or call) it on one or more tables or queries in your current database.

When you run a query, the query returns (displays) data, and Access displays it as a new object. For instance, if you create a query to display only certain fields in a table, Access will open a new document tab in which the results of your query appear after you run the query. There are two main ways to create Access queries:
  • Create a query with the Access Query Wizard.
  • Create your own query from scratch.

How to Use the Access Query Wizard

If you need to create only a basic query, such as the four types of queries described below, you can use the Query Wizard. To use the Query Wizard, click on the Query Wizard icon at Create > Other and then follow the prompts in the dialog box that pops up to create your query. You’ll be able to choose from among four different types of queries:
  • Simple query: The most basic query, a simple query lets you specify which fields to display from a single table. For instance, if you have a customer contact info table with dozens of fields, you might create a simple query that returns the two most important fields, such as the customer’s name and phone number.
  • Crosstab query: This query works much like a simple query but lets you perform a basic calculation, such as finding an average, on one or more fields in the table.
  • Find duplicates query: This query finds and displays duplicate data in the fields of a table or another query.
  • Find unmatched query: Finds and displays rows in one table that do not match the data in the rows of another table. This is the only Query Wizard query that lets you work with data in more than one table.

How to Create Queries from Scratch

Since Access’s built-in queries are limited, you’ll usually have to create your own custom queries rather than use the Query Wizard. To do so, follow the steps below.

1. Create a New Query

To start the process of building your own query, click on the Query Design icon at Create > Other. When you click on it, Access will switch to Design view, and a Design tab will appear in the Ribbon that contains all of the commands you need to build your query.

2. Choose a Type of Query

The actions you’d like your query to perform determine the type of query you should use. The available queries are:
  • Select: Used to retrieve and display data from tables or other queries that you’ve already built and run
  • Make Table: Selects records from one or more tables in a database and saves those records in a new table
  • Append: Adds records to existing tables with data that you supply or import
  • Update: Makes changes to records in existing tables
  • Crosstab: Used to analyze data based on two sets of values—one appears down the left side of the table and one across the top of the table
  • Delete: Removes specific data or entire fields or records from existing tables
  • Union: Combines and displays the data returned by one or more select queries
  • Pass-Through: Creates queries that run directly on ODBC database servers, such as SQL databases
  • Data Definition: Used to make major changes to database objects, such as deleting entire tables

3. Select Tables or Existing Queries

Next, you need to tell Access which tables or existing queries you’d like the query to run on.
  1. To specify which tables a query should run on, click on the Show Table icon at Design > Query Setup.
  2. In the Show Table dialog box that pops up, select the tables and/or queries to add by highlighting them and clicking Add, and then click Close.

4. Specify Fields

When building a query in Design view, the Document pane is split in half horizontally:
  • Top half: Includes a small window for each of the tables or queries you chose to run the new query on
  • Bottom half: Shows a design grid with a list of categories for the query that you need to select in order to tell Access exactly what the query should do
One category you need to select is called Field. It lets you specify which field(s) the query will run on in the tables or queries you specified in step 3. To specify the field(s) to use:
  1. Select the table or query you’d like to work on by clicking on the blue bar at the top of its window in the top half of the document pane.
  2. Specify the field(s) in each table or query that you’d like to run the query on by clicking on the name of the field in the table’s or query’s window. To use all the fields, click on the asterisk at the top of the list. Each field you click on will subsequently appear in the design grid.

5. Specify the Calculation for Each Field

To specify which type of calculation Access should perform on each field you chose in step 4, follow these steps:
  1. Click on the Totals icon at Create > Show/Hide.
  2. In the design grid, click on the field for which you’d like to specify a calculation.
  3. Choose a calculation type, such as:
    1. Sum: Totals the values in a field
    2. Average: Calculates the field’s average value
    3. Min: Returns the lowest value in the field
    4. Max: Returns the highest value in the field
    5. Count: Calculates the number of non-null (not blank) values in a field
    6. StDev: Calculates the field’s standard deviation
    7. Var: Calculates the variance of values in a field
    8. Where: Specifies fields on which the query will not run (these fields are not shown in the query results)
When you run a query, the results of a calculation in a field aren’t stored in the underlying table—the calculation is rerun each time you run the query.

6. Specify Criteria for Fields

Criteria are limits that make a query return only those records in a field that match certain traits you specify. You specify these traits by creating expressions that you can then enter into each field’s “Criteria” cell in the design grid. In Access, an expression is a mathematical or logical formula (consisting of functions, table fields, constants, controls, and so on) that can be applied to table data. You can use expressions to calculate values, validate data, and set a default value for a field or control. To create an expression and add it to a field:
  1. Click on the field’s Criteria cell in the design grid.
  2. Click on the Builder icon at Design > Query Setup.
  3. In the Expression Builder dialog box that pops up, build your expression using the arithmetic words and symbols provided, such as “=,” “and,” “or,” and “not.”
  4. Once you’ve built your expression, click OK. Access will insert the expression into the Criteria cell you clicked on in step 1.
Note that you’re not required to specify criteria for every field. If you don’t specify criteria for a field, your query will return all the records in that field.

7. Sort the Output of Certain Fields

To sort the output of certain fields (the data that your query returns), click on the field’s Sort cell in the design grid and choose Ascending, Descending, or Not Sorted—if you don’t choose to sort a field, Access uses Not Sorted by default.

8. Run the Query

To run a query, click on the Run icon at Design > Results. Access will perform the query’s actions and display results in a new document tab (if applicable).
 
 
  Acknowledgments & Disclaimer
 
 
 
Download the PDF
for just $2.95
 
Access 2007
 
Complete guide
Handy, portable format
 
Access 2007 Chart
 
Buynow_button