Lesson 5: Creating Microsoft Access Queries

You can use a query to view a subset of your data or to answer questions about your data. For example, if you want to view a list of student names and email addresses, but you do not want to see addresses and other data, you can create a query that displays the student’s first name, last name, and email address only. Alternatively, if you want to know which students live in DE, you can restrict your list to those students. This lesson teaches you how to create a query.

Open Tables or Queries in Query Design View

A query can be based on tables or on other queries. To create a query, you open the tables or queries on which you are going to base your query in Query Design view, and then use the options in Design view to create your query. You then click the Run button to display the results. You can save queries for later use.

To open tables or queries in Query Design view:

Query Design

  1. Activate the Create tab.
  2. Click the Query Design button in the Other group. The Show Table dialog box appears.
  3. Activate the Tables tab if you want to base your query on tables, activate the Queries tab if you want base your query on queries or activate the Both tab if you want to base your query on both tables and queries.
  4. Click to choose the table or query on which you want to base your query.
  5. Click Add. The table appears in the window.
    1. Click to choose the next table or query on which you want to base your query.
    2. Continue clicking tables or queries until you have all the tables and queries you plan to use.
  6. Click Close. Access changes to Query Design view.

Display All Records and All Fields

In Query Design view, each table has an option that allows you to display all of the fields and all of the records in a table. This option appears on the field line on the drop-down menu as the table name followed by a period and an asterisk (tablename.*).

To display all records and all fields:

Display All

  1. Open a table or query in Query Design view.
  2. Click the down-arrow in the first field on the Field row and then select the tablename.* option. The table name appears on the table line.
  3. Click the Run button. Access retrieves all of the fields and records for the table and displays them in Datasheet view.

Display All

 

Change from Datasheet View to Query Design View

After you run a query, you can easily change back to Query Design view and make modifications to your query or create a new query.

To change to Query Design view:

Query Design View

  1. Activate the Home tab.
  2. Click the down-arrow below View in the Views group. A menu appears.
  3. Click Design View. Access changes to Query Design view. You can modify your query.

Tip Tip: You can also click the Design button Design Button in the lower-right corner of the Access window to change to Design view.

Retrieve a Single Column

You can use an Access query to retrieve a single column of data. Instead of choosing the tablename.* option on the Field line in Query Design view, choose the name of the field you want to retrieve.

To retrieve a single column:

Retrieve Single column

  1. Open a table or query in Query Design view.
  2. Choose the field name you want to display in the field line.
  3. Click the Run button. Access retrieves the column you chose.

Retrieve a Single Column

Retrieve Multiple Columns

You can use an Access query to retrieve multiple columns of data. On the Field line in Query Design view, choose the field name of each field you want to retrieve in the order you want to retrieve them.

To retrieve multiple columns:

Retrieve Multiple Columns

  1. Open a table or query in Query Design view.
  2. Choose the field names you want to retrieve in the order you want to retrieve them.
  3. Click the Run button. Access retrieves the columns you chose.

Retrieve Multiple Columns

Sort a Query

When creating a query, you can sort the rows you retrieve in ascending or descending order by choosing the option you want on the Sort row in Query Design view.

To perform a sort:

Sort

  1. Open a table or query in Query Design view.
  2. Choose the field names you want to retrieve in the order you want to retrieve them.
  3. Under the field you want to sort, click the down-arrow and then choose Ascending or Descending.
  4. Click the Run button. Access retrieves the columns you chose and displays the rows in the order you specified.

Sort Column

Sort Multiple Columns in a Query

As you learned in the previous section, you can sort the rows your query returns. You can also create sorts within a sort. For example, you can sort by state and then within a state, you can sort by last name and then by first name. You specify the sort in the order you want the sort to occur. If you want to sort by state and then by last name within a state and then by first name within last name, you enter the sort in the following order: city, last name, first name. Your sort order may not agree with the order in which you want to display fields. In such a case, you can use fields that do not display to enter your sort order. To prevent a field from displaying, deselect the Show box on the Show row.

To sort multiple columns:

Sort Multiple Columns

  1. Open a table or query in Query Design view.
  2. Choose the field names you want to retrieve in the order you want to retrieve them.
  3. Choose the field names you want to sort by in the order you want to sort.
  4. Under the fields you want to sort by, choose Ascending or Descending.
  5. Deselect the Show button for the columns you do not want to display.
  6. Click the Run button. Access retrieves the columns you chose and displays the rows in the order you specified.

Sort Multiple Columns

Retrieve Specific Records

In the examples so far, you have been retrieving all of the records (rows) in your table. You can, however, specify which records you wish to retrieve. For example, you can retrieve only those students who live in DE, only the student whose student number is 5, or only those students whose birth date is 2/16/88.

You use logical operators such as = (equal), <> (not equal), > (greater than), or < (less than) to restrict the records you retrieve. For example, if you only want to display students who live in DE, enter = "DE" in the State column on the Criteria line. Access will only retrieve records where the value in the State column is equal to DE. Selection criteria are not case-sensitive, so Access will retrieve records where the entry is DE, de, De, or dE.

Logical Operators
Operator Meaning Field Type Entry Format

=

Equal to

Character
Number
Date

= "DE"
= 5
= #2/16/88#

<> 

Not equal to

Character
Number
Date

<> "DE"
<> 5
<> #2/16/88#

Greater than

Character
Number
Date

> "DE"
> 5
> #2/16/88#

>=

Greater than or equal to

Character
Number
Date

> = "DE"
> = 5
> = #2/16/88#

Less than

Character
Number
Date

< "DE"
< 5
< #2/16/88#

<=

Less than or equal to

Character
Number
Date

<= "DE"
<= 5
<= #2/16/88#

In

Equal to any item in a list

Character
Number
Date

In ("DE", "NJ")
In (5, 9, 17)
In (#2/16/88#, #2/3/90#, #12/15/88#)

Not In 

Not equal to any item in a list

Character
Number
Date

Not In ("DE", "NJ")
Not In (5, 9, 17)
Not In (#2/16/88#, #2/3/90#, #12/15/88#)

Between

Between two values,
greater than or equal to one and less than or equal to the other

Character
Number
Date

Between "C" And "F"
Between 5 And 10
Between #1/1/88# And #12/31/88#

Not Between

Not between two values

Character
Number
Date

Not Between "C" And "F"
Not Between 5 And 10
Not Between #1/1/88# And #12/31/88#

Is Null

The value is missing from the field

Character
Number
Date

Is Null
Is Null
Is Null

Is Not Null

The value is not missing from the field

Character
Number
Date

Is Not Null
Is Not Null
Is Not Null

Like

Like a specified pattern.
* means any series of characters.
? means any single character.

Character
Number
Date

Like "S*"
Like "1*"
Not Applicable

Not Like

Not like a specified pattern.
* means any series of characters.
? means many single character.

Character
Number
Date

Like "S*"
Like "1*"
Not Applicable

When using the Like and Not Like criteria, where you place the asterisk(*) or question mark (?) determines the type of search Access performs. Like "Jo*" finds all records in the field that begin with Jo. It would find Jones, Johnson, and Jordan. Like "*son" finds all records in the field that end with son. It would find Stevenson, Jackson, and Peterson. Like "*456*" finds all records that contain 456 anywhere in the field. It would find 456123789, 123456789, and 123789456. The sequence Like "?en" finds all three character field entries where the second and third characters are en. It would find Ben, Len, and Jen. The sequence Like "Jo?" finds all three character field entries where the first and second characters are Jo. It would return Joe, Joy, and Jon. The sequence Like "T?m" finds all three character field entries where the first and third characters are T and m. It would return Tim, Tom, and Tam.

To retrieve specific records:

Retrieve Specific Records

  1. Open a table or query in Query Design view.
  2. Choose the field names you want to retrieve in the order you want to retrieve them.
  3. Choose the field names you want to sort by in the order you want to sort.
  4. Under the fields you want to sort by, choose Ascending or Descending.
  5. Deselect the Show button for columns you do not want to display.
  6. Enter your selection criteria on the Criteria line.
  7. Click the Run button. Access retrieves the columns you chose and displays the rows in the order you specified.

Retrieve Specific Records

Apply Multiple Criteria

You can apply multiple criteria to the same table. If you place two criteria on the same line, Access will only retrieve records where both criteria are met. For example, if you want all records where the State is equal to "DE" and the Last Name is equal to Smith, you would set the State field to = "DE" and the Last Name field to = "Smith" and you would place both criteria on the same line.

If you place one set of criteria on the Criteria line and the second set of criteria on the Or line, Access will retrieve records if either criteria are met. For example, you want all records where the State is equal to "DE" or the Last Name is equal to Smith. You would set the State field to = "DE" and the Last Name field to = "Smith" and you would place one set of criteria on the Criteria line and the other set of criteria on the Or line. Access will bring back all records where the state equals DE and all records where the Last Name is equal to Smith no matter what the State is. You can add additional and and or statements by using the lines below the Or line. For And clauses, place the criteria on the same line; for Or clauses, place the criteria on separate lines.

To apply multiple criteria:

Apply Multiple Criteria

  1. Open a table or query in Query Design view.
  2. Choose the field names you want to retrieve in the order you want to retrieve them.
  3. Choose the field names you want to sort by in the order you want to sort.
  4. Under the fields you want to sort by, choose Ascending or Descending.
  5. Enter your selection criteria on the Criteria line and the Or line, as needed.
  6. Deselect the Show button for columns you do not want to display.
  7. Click the Run button. Access retrieves the columns you chose and displays the rows in the order you specified.

Apply Multiple Criteria

Create a Query That Uses Two or More Tables

If you want to view data from two or more tables or queries, you can create a query that pulls the data from multiple tables or queries. The tables and queries from which you pull your data should have a relationship.

To create a query that uses two or more tables:

Query That Uses Two or More Tables

  1. Open the tables and/or queries you want to use in Query Design view.
  2. Choose the field names you want to retrieve in the order you want to retrieve them.
  3. Choose the field names you want to sort by in the order you want to sort. Under the fields you want to sort by, choose Ascending or Descending.
  4. Enter your selection criteria, if necessary (Not applicable in this example).
  5. Deselect the Show button for columns you do not want to display (Not applicable in this example).
  6. Click the Run button. Access retrieves the columns you chose and displays the rows in the order you specified.

Save a Query

After you create a query, you can save it. You can rerun a saved query at any time. If you change the data on which the saved query is based, you will see the changes when you rerun the query.

To save a query:

Save a Query

  1. Click the Save button on the Quick Access toolbar. Access saves the query unless you are saving for the first time. If you are saving for the first time, the Save As dialog box appears.
  2. Type the name you want to give your query.
  3. Click OK. Access saves the query. You can now access the query by using the Navigation pane.

 Tip Tip: You can also save by right-clicking a query’s tab and then selecting Save from the menu that appears. Access saves the query unless you are saving for the first time. If you are saving for the first time, the Save As dialog box appears. Type the name you want to give the query and then click OK. Access saves the query. You can now access the query by using the Navigation pane.

 Tip Tip: After you have saved a query, you can run it by opening the Navigation pane and then clicking the name of the query.

Modify a Query

Once created, a query can be modified. Simply open the query in Query Design view and make the changes. You can add columns, change the sort order, change the criteria, and make other changes.

In Query Design view, the Query Setup group offers several options that can assist you. Use the Insert Rows button  to insert a row in the criteria area. Click anywhere in the row before which you want to insert a new row and then click the Insert Rows button.

Use the Insert Columns button Insert Column Button to insert a column. Click anywhere in the column before which you want to insert a column and then click the Insert Column button .

Use the Delete Rows button Delete Row Button to delete a row in the criteria area. Click anywhere in the row you want to delete and then click the Delete Row button.

Use the Delete Columns button Delete Column Button to delete a column. Click anywhere in the column you want to delete and then click the Delete Column button Delete Column Button.

Use a Query to Make a Table

You can use a query to create a table. This is useful when you want to create a new table that includes the fields and data from an existing table.

To create a table:

Query to Mkae a Table

  1. Open the table or query on which you want to base your new table on in Query Design view.
  2. Enter the criteria on which you want to base your new table.
  3. Click the Make Table button. The Make Table dialog box appears.
  4. Type the name you want to give your new table.
  5. Click OK.
  6. Click Run. You see the following prompt.

Query to Make Table

  1. Click Yes.
  2. Close the query. (Right-click the query’s tab and then click Close.)
  3. Double-click the new table’s name in the Navigation pane to view the new table.

Create a Parameter Query

If instead of entering predetermined criteria, you want to prompt users when a query runs, you can create a parameter query. You create a parameter query by enclosing a question in square brackets ([]). For example, if you want to create a parameter query that asks users which State they want to use from the Student’s table, you would type [Which State?] on the Criteria line under the State column. When the query runs, Access will prompt the user for the answer to your question.

To create a parameter query:

Parameter Query

  1. Open a table or query in Query Design view.
  2. Create your query.
  3. On the Criteria line, type the prompt within square brackets.
  4. Click the Run button. Access prompts you.

Parameter Query

  1. Respond to the prompt.
  2. Click OK. Access displays the results of your query in Datasheet view.

Parameter Query

 Note Note: If you want to make your user prompt more flexible, use one of the following formats.


Like "*" & [Prompt] & "*"

Returns all records that contain the value you enter.
Example: If you enter ad, Access returns all records that include the sequence ad anywhere in the field.

Like "*" & [Prompt]

Returns all records that end with the value you enter.
Example: If you enter S, Access returns all records that end with S.

Like [Prompt] & "*"

Returns all records that begin with the value you enter.
Example: If you enter S, Access returns all records that begin with S.

> [Prompt]

 

 

Note: You can also use < (less than) ,<= (less than or equal to) >=, >= (greater than or equal to), or <> (not equal)

Find all records with a value greater than the value you enter.
Example: If you enter 5, Access returns all records that are greater than 5.

 


Legal Dot Privacy