Beruflich Dokumente
Kultur Dokumente
Using a query makes it easier to view, add, delete, or change data in your
Access database. Some other reasons for using queries:
1. Open the database and on the Create tab, click Query Design.
2. In the Show Table box, on the Tables tab, double-click the Products table
and then close the dialog box.
3. In the Products table, let’s say that you have Product Name and List Price
fields. Double-click the Product Name and List Price to add these fields to
the query design grid.
4. On the Design tab, click Run. The query runs, and displays a list of products
and their prices.
1. Open the database. On the Create tab, in the Query group, click Query
Design.
2. In the Show Table dialog box, on the Tables tab, double-
click Customers and Orders.
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.
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
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.
1. Select the check box in the Show row of the City column, so that the query
results will display the city.
2. On the Design tab, in the Results group, click Run. The query prompts you
to enter a value for City.
3. 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:
4. On the Home tab, in the Views group, click View, and then click Design
View.
5. In the query design grid, in the Criteria row of the City column, type Like
[For what city?]&"*".
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.
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:
To add criteria to an Access query, open the query in Design view and
identify the fields (columns) you want to specify criteria for. If the field is not
in the design grid, double-click the field to add it to the design grid and
then enter the criterion in the Criteria row for that field. If you’re not sure
how to make this happen, see Introduction to queries.
Here are some examples of commonly used criteria you can use as a
starting point to create your criteria. The examples are grouped by data
types.
In this topic
Introduction to query criteria
The following tables shows some sample criteria and explains how they
work.
Criteria Description
>25 and <50 This criterion applies to a Number field, such as Price or
UnitsInStock. It includes only those records where the
Price or UnitsInStock field contains a value greater than
25 and less than 50.
DateDiff ("yyyy", This criterion applies to a Date/Time field, such as
[BirthDate], Date()) > 30 BirthDate. Only records where the number of years
between a person's birthdate and today's date is
greater than 30 are included in the query result.
Is Null This criterion can be applied to any type of field to show
records where the field value is null.
As you can see, criteria can look very different from each other, depending
on the data type of the field to which they apply and your specific
requirements. Some criteria are simple, and use basic operators and
constants. Others are complex, and use functions, special operators, and
include field references.
This topic lists several commonly used criteria by data type. If the examples
given in this topic do not address your specific needs, you might need to
write your own criteria. To do that, you must first familiarize yourself with
Here, you will see where and how you add the criteria. To add a criteria to a
query, you must open the query in Design view. You then identify the fields
for which you want to specify criteria. If the field is not already in the design
grid, you add it by either dragging it from the query design window to the
field grid, or by double-clicking the field (Double-clicking the field
automatically adds it to the next empty column in the field grid.). Finally,
you type the criteria in the Criteria row
Criteria that you specify for different fields in the Criteria row are combined
by using the AND operator. In other words, the criteria specified in the City
and BirthDate fields are interpreted like this:
City = "Chicago" AND BirthDate < DateAdd (" yyyy ", -40, Date())
2. Only records where the value of the City field is Chicago will satisfy this
criterion.
4. Only records that meet both criteria will be included in the result.
What if you want only one of these conditions to be met? In other words, if
you have alternate criteria, how do you enter them?
Criteria specified in the Criteria and or rows are combined using the OR
operator, as shown below:
City = "Chicago" OR BirthDate < DateAdd (" yyyy ", -40, Date())
If you need to specify more alternatives, use the rows below the or row.
A criterion that you specify for a Hyperlink field is, by default, applied to the
display text portion of the field value. To specify criteria for the destination
Uniform Resource Locator (URL) portion of the value, use
the HyperlinkPart expression. The syntax for this expression is as
follows: HyperlinkPart([Table1].[Field1],1) =
Do not begin with the Not Like U* Returns records for all
specified string, such as U countries/regions whose
names start with a character
other than "U".
Contain the specified string, Like "*Korea*" Returns records for all
such as Korea countries/regions that
contain the string "Korea".
Do not contain the specified Not Like "*Korea*" Returns records for all
string, such as Korea countries/regions that do not
contain the string "Korea".
End with the specified string, Like "*ina" Returns records for all
such as "ina" countries/regions whose
names end in "ina", such as
China and Argentina.
Contain null null Is Null Returns records where no value is entered in the
(or missing) UnitPrice field.
values
Contain non- Is Not Null Returns records where the value is not missing in
Lookup fields There are two types of Lookup fields: those that look up
values in an existing data source (by using a foreign key), and those that are
based on a list of values specified when the Lookup field is created.
Lookup fields that are based on a list of specified values are of the Text data
type, and valid criteria are the same as for other text fields.
The criteria you can use in a Lookup field based on values from an existing
datasource depend on the data type of the foreign key, rather than the data
type of the data being looked up. For example, you may have a Lookup field
that displays Employee Name, but uses a foreign key that is of the Number
data type. Because the field stores a number instead of text, you use criteria
that work for numbers; that is, >2.
If you do not know the data type of the foreign key, you can inspect the
source table in Design view to determine the data types of the field. To do
this:
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.
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 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:
1. Run the Product Subtotals query, 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.
You can modify the Product Subtotals query again so that the query returns
rows of product subtotals and columns of monthly subtotals.
1. On the Home tab, in the Views group, click View, and then click Design
View.
2. In the Query Setup group, click Show Table.
3. In the Show Table dialog box, double-click Orders, and then click Close.
4. On the Design tab, in the Query Type group, click Crosstab. In the design
grid, the Show row is hidden, and the Crosstab row is displayed.
5. In the third column of the design grid, right-click the Field row, and then
click Zoom on the shortcut menu. The Zoom box opens.
6. In the Zoom box, type or paste the following: Month: "Month " &
DatePart("m", [Order Date])
7. Click OK.
8. In the Crosstab row, select the following values from the drop-down
list: Row Heading for the first column, Value for the second column,
and Column Heading for the third column.
9. On the Design tab, in the Results group, click Run. The query runs, and
then displays product subtotals, aggregated by month.
10. Press CTRL+S to save the query.
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.
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:
In this example, the Append To row values should match the Field row
values, but that is not required for append queries to work.
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:
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:
If you want to only delete a few records, you don’t need a query. Just open
the table in Datasheet view, select the fields (columns) or records (rows) that
you want to delete, and then press DELETE.
In this article
Choosing a query type
Additional Information
Troubleshooting tips
Verify that you have the necessary permissions to delete records from the
database. If you are not sure, contact your system administrator or the
database designer.
Make sure that you have enabled content in the database. By default,
Access blocks all action queries (delete, update, and make-table queries)
unless you first trust the database.
Ask other users of the database to close all tables, forms, queries, and
reports that use the data that you want to delete. This helps avoid lock
violations.
Before you edit or delete records, making a backup copy of the database is
a good idea in case you want to reverse your changes.
To revert to a backup, close and rename the original file so that the backup
copy can use the name of the original version. Assign the name of the
original version to the backup copy, and open the renamed backup copy in
Access.
The table appears as a window in the upper section of the query design
grid. From the list of fields, double-click the asterisk (*) to add all of the
fields in the table to the design grid.
An example of when you might want to use this option: Suppose you want
to remove all of the pending orders for a customer. To find just those
records, you’d add the Customer ID and Order Date fields to the query
design grid, and then you enter the ID number of the customer, and the
date on which that customer's orders became invalid.
1. Click the Create tab and in the Queries group, click Query Design.
2. Select the table which has the data you want to delete (if the table is
related, select the table on the "one" side of the relationship), click Add,
and then click Close.
3. Double-click the asterisk (*) to add all of the fields in the table to the
design grid. Adding all the table fields enables the delete query to remove
entire records (rows) from the table.
Optionally, you can enter criteria for one or more fields in the Criteria row of
the designer, and then clear the Show check box for each criteria field.
4. On the Design tab, in the Results group, click View, and then
click Datasheet View.
5. Verify that the query returns the records that you want to set to NULL or a
zero-length string (a pair of double-quotation marks with no space
between them ("").
6. As needed, repeat steps 3 to 5 and change the fields or criteria until the
query returns only the data that you want to delete, and then press
CTRL+S to save the query.
7. To run the query, double-click the query in the Navigation Pane.
Determine which records reside on the "one" side of the relationship and
which reside on the "many" side.
If you need to delete records on the "one" side of the relationship and the
related records on the "many" side, you enable a set of rules called
Referential Integrity, and you enable cascading deletes. Steps in this section
explain Referential Integrity, and how to perform both tasks.
If you need to delete records only on the "one" side of the relationship, you
first delete that relationship, and then delete the data.
If you need to remove data only on the "many" side of the relationship, you
can create and run your delete query without having to change the
relationship.
To find out which records reside on the "one" and "many" sides of a
relationship, on the Database Toolstab, in the Relationships group,
click Relationships. The Relationships tab displays the tables in your
database and the relationships. Each relationship is depicted as a line that
connects tables between fields.
The following figure shows a typical relationship. Most, if not all, of the
relationships in a database have a "one" side and a "many" side. The
When you delete records on the "one" side of the relationship, you also
delete all of the related records on the "many" side of the relationship.
However, when you delete records on the "many" side of a relationship, you
typically don't delete records on the "one" side.
Follow these steps only when you need to delete data on the "one" and
"many" sides of a relationship.
Make a note of the fields involved in the relationship so that you can restore
the relationship after you delete your data.
3. Right-click the relationship (the line) connecting the tables involved in the
deletion operation, and then click Delete on the shortcut menu.
Note: To restore the relationship, follow the previous steps to open the Relationships pane,
and then drag the primary key field from the "one" table and drop it on the foreign key field
of the "many" table. The Edit Relationship dialog box appears. If the old relationship
enforced referential integrity, select Enforce Referential Integrity, and then click Create.
Otherwise, just click Create.
Criteria Effect
> 234 Returns all numbers greater than 234. To find all
numbers less than 234, use < 234.
>= "Cajhen" Returns all records from Cajhen through the end of
the alphabet.
Between #2/2/2010# And Returns dates from 2-Feb-2010 through 1-Dec-2010
#12/1/2010# (ANSI-89). If your database uses the ANSI-92 wildcard
characters, use single quotation marks (') instead of
pound signs (#). Example: Between '2/2/2010' And
'12/1/2010'.
Not "Germany" Finds all records where the exact contents of the field
are not exactly equal to "Germany." The criterion will
return records that contain characters in addition to
"Germany," such as "Germany (euro)" or "Europe
(Germany)".
Not "T*" Finds all records except those beginning with T. If your
database uses the ANSI-92 wildcard character set, use
the percent sign (%) instead of the asterisk (*).
Not "*t" Finds all records that do not end with t. If your
database uses the ANSI-92 wildcard character set, use
the percent sign (%) instead of the asterisk (*).
In(Canada,UK) In a list, finds all records containing Canada or UK.
Like "[A-D]*" In a Text field, finds all records that start with the
letters A through D. If your database uses the ANSI-92
wildcard character set, use the percent sign (%) instead
of the asterisk (*).
To fix this problem, set the query's Unique Records property to Yes.
When you see that message, take the following step to enable the blocked
content:
On the Security Warning Message Bar, click Enable Content, and run your
query again.