Access For Microsoft 365 Access 2019 Access 2016 Access 2013 Access 2010 Access 2007
Access For Microsoft 365 Access 2019 Access 2016 Access 2013 Access 2010 Access 2007
Access For Microsoft 365 Access 2019 Access 2016 Access 2013 Access 2010 Access 2007
Using a query makes it easier to view, add, delete, or change data in your Access database. Some
other reasons for using queries:
Note: If you want to try out the queries in the examples, use an Access desktop database.
In a well-designed database, the data that you want to present through a form or report is usually
located in multiple tables. A query can pull the information from various tables and assemble it
for display in the form or report. A query can either be a request for data results from your
database or for action on the data, or for both. A query can give you an answer to a simple
question, perform calculations, combine data from different tables, add, change, or delete data
from a database. Since queries are so versatile, there are many types of queries and you would
create a type of query based on the task.
Major query Use
types
Select To retrieve data from a table or make calculations.
Action Add, change, or delete data. Each task has a specific type of action query. Action queries are
available in Access web apps.
If you want to review data from only certain fields in a table, or review data from multiple tables
simultaneously or maybe just see the data based on certain criteria, a select query type would be
your choice. For more information, see create a simple select query.
For example, if your database has a table with a lot of information about products and you want
to review a list of products and their prices, here’s how you’d create a select query to return just
the product names and the respective price:
Top of Page
For example, if you have a database for a store that sells food items and you want to review
orders for customers who live in a particular city. Say that the data about orders and data about
customers are stored in two tables named Customers and Orders respectively. If each table has a
Customer ID field, which forms the basis of a one-to-many relationship between the two tables.
You can create a query that returns orders for customers in a particular city, for example, Las
Vegas, by using the following procedure:
Clearing the Show check box prevents the query from displaying the city in its results, and
typing Las Vegas in the Criteria row specifies that you want to see only records where the value
of the City field is Las Vegas. In this case, the query returns only the customers that are located
in Las Vegas. You don’t need to display a field to use it with a criterion.
7. In the Orders table, double-click Order ID and Order Date to add these fields to the
next two columns of the query design grid.
8. On the Design tab, in the Results group, click Run. The query runs, and then displays a
list of orders for customers in Las Vegas.
9. Press CTRL+S to save the query.
Top of Page
If you frequently want to run variations of a particular query, consider using a parameter query.
When you run a parameter query, the query prompts you for field values, and then uses the
values that you supply to create criteria for your query.
Continuing from the previous example where you learnt to create a select query that returns
orders for customers located in Las Vegas, you can modify the select query to prompt you to
specify the city each time that you run the query. To follow along, open the database that you
created in the previous example:
1. In the Navigation Pane, right-click the query named Orders by City (that you created in
the previous section), and then click Design View on the shortcut menu.
2. In the query design grid, in the Criteria row of the City column, delete Las Vegas, and
then type [For what city?].
The string [For what city?] is your parameter prompt. The square brackets indicate that you
want the query to ask for input, and the text (in this case, For what city?) is the question that the
parameter prompt displays.
Note: Neither a period (.) nor an exclamation point (!) can be used as text in a parameter prompt.
3. Select the check box in the Show row of the City column, so that the query results will
display the city.
4. On the Design tab, in the Results group, click Run. The query prompts you to enter a
value for City.
5. Type New York, and then press ENTER to see orders for customers in New York.
What if you don't know what values you can specify? You can use wildcard characters as part of
the prompt:
In this parameter prompt, the Like keyword, the ampersand (&), and the asterisk (*) enclosed in
quotation marks allow the user to type a combination of characters, including wildcard
characters, to return a variety of results. For example, if the user types *, the query returns all
cities; if the user types L, the query returns all cities that start with the letter "L;" and if the user
types *s*, the query returns all cities that contain the letter "s."
The query runs, and then displays orders for customers in New York.
You can also specify what type of data a parameter should accept. You can set the data type for
any parameter, but it is especially important to set the data type for numeric, currency, or
date/time data. When you specify the data type that a parameter should accept, users see a more
helpful error message if they enter the wrong type of data, such as entering text when currency is
expected.
If a parameter is set to accept text data, any input is interpreted as text, and no error message is
displayed.
To specify the data type for parameters in a query, use the following procedure:
For more information, see using parameters to ask for input when running a query.
Top of Page
The Total row in a datasheet is very useful, but for more complex questions, you use a totals
query. A totals query is a select query that allows you to group and summarize data, like when
you want to see total sales per product. In a totals query, you can use the Sum function (an
aggregate function), to see total sales per product.
Use the following procedure to modify the Product Subtotals query that you created in the
previous example so that it summarizes product subtotals by product.
Note: Although they have similar names, the Totals row in the design grid and the Total row in
a datasheet are not the same:
You can group by field values by using the Totals row in the design grid.
You can add a datasheet Total row to the results of a totals query.
When you use the Totals row in the design grid, you must choose an aggregate function
for each field. If you do not want to perform a calculation on a field, you can group by the field.
In the second column of the design grid, in the Total row, select Sum from the drop-
down list.
On the Design tab, in the Results group, click Run. The query runs, and then displays a
list of products with subtotals.
Press CTRL+S to save the query. Leave the query open.
For more information, see Display column totals in a datasheet using a Totals row.
Top of Page
For example if you have a database for some products you’d like to sell. This database has a
table called Orders Details that has information about the products in fields such as, price of each
product and the quantities. You can calculate the subtotal by using a query that multiplies the
quantity of each product by the unit price for that product, multiplies the quantity of each product
by the unit price and discount for that product, and then subtracts the total discount from the total
unit price. If you created the sample database in the previous example, open it and follow along:
For more information, see Display column totals in a datasheet using a Totals row.
Top of Page
When you use tables to record transactions or store regularly occurring numeric data, it is useful
to be able to review that data in aggregate, such as sums or averages. In Access, you can add a
Totals row to a datasheet. Total row is a row at the bottom of the datasheet that can display a
running total or other aggregate value.
1. Run the Product Subtotals query you created earlier, and leave the results open in
Datasheet view.
2. On the Home tab, click Totals. A new row appears at the bottom of the datasheet, with
the word Total in the first column.
3. Click the cell in the last row of the datasheet named Total.
4. Click the arrow to view the available aggregate functions. Because the column contains
text data, there are only two choices: None and Count.
5. Select Count. The content of the cell changes from Total to a count of the column
values.
6. Click the adjoining cell (the second column). Note that an arrow appears in the cell.
7. Click the arrow, and then click Sum. The field displays a sum of the column values.
8. Leave the query open in Datasheet view.
Top of Page
Now suppose that you want to review product subtotals, but you also want to aggregate by
month, so that each row shows subtotals for a product, and each column shows product subtotals
for a month. To show subtotals for a product and to show product subtotals for a month, use a
crosstab query.
You can modify the Product Subtotals query again so that the query returns rows of product
subtotals and columns of monthly subtotals.
For more information about crosstab queries, see Make summary data easier to read by using a
crosstab query.
Top of Page
For example, suppose that you want to send data for Chicago orders to a Chicago business
partner who uses Access to prepare reports. Instead of sending all your order data, you want to
restrict the data that you send to data specific to Chicago orders.
You can build a select query that contains Chicago order data, and then use the select query to
create the new table by using the following procedure:
To run a make-table query, you may need to enable the database content.
Note: If you see a message beneath the Ribbon about enabling the database, click Enable
content.If your database is already in a trusted location, you will not see the Message Bar.
Note: If there is already a table with the same name that you specified, Access deletes that table
before running the query.
For more information about using make table queries, see Create a make table query.
Top of Page
Create an append query
You can use an append query to retrieve data from one or more tables and add that data to
another table.
For example, suppose that you created a table to share with a Chicago business associate, but you
realize that the associate also works with clients in the Milwaukee area. You want to add rows
that contain Milwaukee area data to the table before you share the table with your associate. You
can add Milwaukee area data to the Chicago Orders table by using the following procedure:
1. Open the query named "Chicago Orders Query" you created earlier in Design view.
2. On the Design tab, in the Query Type group, click Append. The Append dialog box
opens.
3. In the Append dialog box, click the arrow in the Table Name box, select Chicago
Orders from the drop-down list, and then click OK.
4. In the design grid, in the Criteria row of the Ship City column, delete 'Chicago', and then
type 'Milwaukee'.
5. In the Append To row, select the appropriate field for each column.
In this example, the Append To row values should match the Field row values, but that is not
required for append queries to work.
Note: While running a query that returns a large amount of data you might get an error message
indicating that you will not be able to undo the query. Try increasing the limit on the memory
segment to 3MB to allow the query to go through.
For more information about append queries, see Add records to a table by using an append
query.
Top of Page
You can use an update query to change the data in your tables, and you can use an update query
to enter criteria to specify which rows should be updated. An update query provides you an
opportunity to review the updated data before you perform the update.
Important: An action query cannot be undone. You should consider making a backup of any
tables that you will update by using an update query. An update query is not available in Access
web apps.
In the previous example, you appended rows to the Chicago Orders table. In the Chicago Orders
table, the Product ID field shows the numeric Product ID. To make the data more useful in
reports, you can replace the product IDs with product names, use the following procedure:
Tip: You can use an update query to delete field values by using an empty string ("") or NULL
in the Update To row.
When you open the Chicago Orders table, you will see that the numeric values in the Product ID
field have been replaced by the product names from the Products table.
For more information about update queries, see Create and run an update query.
Top of Page
You can use a delete query to delete data from your tables, and you can use a delete query to
enter criteria to specify which rows should be deleted. A delete query provides you an
opportunity to review the rows that will be deleted before you perform the deletion.
For example, say that while you were preparing to send the Chicago Orders table from the
previous example, to your Chicago business associate, you notice that some of the rows contain a
number of empty fields. You decided to remove these rows before you send the table. You could
just open the table and delete the rows manually, but if you have many rows to delete and you
have clear criteria for which rows should be deleted, you might find it helpful to use a delete
query.
You can use a query to delete rows in the Chicago Orders table that do not have a value for
Order ID by using the following procedure:
We’ll enter the date we want, then Access will run the query using
the date we entered as a search term.
When you’re running parameter queries, search terms act as variable criteria,
which are query criteria that change each time you run the query. For instance,
let's say we own a bakery and want to create a query that will quickly look up
orders that were placed on a certain date. We could create a parameter query
with variable criteria in the Date field. This way, each time we run the query a
dialog box will appear prompting us to enter the date we want our query to
search for.
We’ll enter the date we want, then Access will run the query using the date we
entered as a search term.
Not all records containing similar information are duplicates. For instance,
records of two orders that were placed on different dates but that contained
identical items would not be duplicate records. Likewise, not all duplicate
records contain completely identical information. For example, two customer
records could refer to the same person but include different addresses. The
record with the out-of-date address would be the duplicate record.
Fortunately, Access makes it easy to search for and locate potential duplicate
records. Note that Access won’t delete the records for you or help you figure out
which one is current—you’ll have to do these things for yourself. If you’re
familiar with the data in your database, though, getting rid of duplicate records
will be a manageable task.
Once you decide which record to delete, make sure you won’t be
losing any information you might need. In our example, before we
deleted our duplicate record we found all of the orders linked to that
record’s ID number and replaced it with the ID number of the record
we decided to keep.
Lesson 19: Query Criteria Quick Reference
Guide
When entering the criteria, write them exactly as they are written in the second
column, replacing x with your search term—or in the case of dates,
replacing mm/dd/yyyy with the desired date.