You are on page 1of 32

1

In Supplier table:

Use this
To include records that... criterion Query result

Exactly match a value, "China" Returns records where the CountryRegion field
such as China is set to China.

Do not match a value, Not Returns records where the CountryRegion field
such as Mexico "Mexico" is set to a country/region other than Mexico.

Begin with the specified Like U* Returns records for all countries/regions whose
string, such as U names start with "U", such as UK, USA, and so
on.

NOTE When used in an expression, the


asterisk (*) represents any string of characters —
it is also called a wildcard character.

Do not begin with the Not Like U* Returns records for all countries/regions whose
specified string, such as names start with a character other than "U".
U

Contain the specified Like Returns records for all countries/regions that
string, such as Korea "*Korea*" contain the string "Korea".

Do not contain the Not Like Returns records for all countries/regions that do
specified string, such as "*Korea*" not contain the string "Korea".
Korea

End with the specified Like "*ina" Returns records for all countries/regions whose
string, such as "ina" names end in "ina", such as China and Argentina.

Do not end with the Not Like Returns records for all countries/regions that do
specified string, such as "*ina" not end in "ina", such as China and Argentina.
"ina"

Follow a value, such as >= "Mexico" Returns records of all countries/regions,


Mexico, when sorted in beginning with Mexico and continuing through
alphabetical order the end of the alphabet.

Fall within a specific Like "[A- Returns records for countries/regions whose
range, such as A through D]*" names start with the letters "A" through "D".
D
2

Match one of two values, "USA" Or Returns records for USA and UK.
such as USA or UK "UK"

Match a specific pattern Like "Chi??" Returns records for countries/regions, such as
China and Chile, whose names are five
characters long and the first three characters are
"Chi".

Criteria for Number, Currency, and AutoNumber fields

The following examples are for the Unit Price field in a query that is based on a table that stores
products information. The criterion is specified in the Criteria row of the field in the query
design grid.

To include records Use this


that... criterion Query Result

Exactly match a 100 Returns records where the unit price of the product is
value, such as 100 $100.

Do not match a Not 1000 Returns records where the unit price of the product is
value, such as 1000 not $1000.

Contain a value < 100 Returns records where the unit price is less than $100
3

smaller than a <= 100 (<100). The second expression (<=100) displays
value, such as 100 records where the unit price is less than or equal to
$100.

Contain a value >100 Returns records where the unit price is greater than
larger than a value, >=100 $99.99 (>99.99). The second expression displays
such as 99.99 records where the unit price is greater than or equal
to $99.99.

Contain one of the 20 or 25 Returns records where the unit price is either $20 or
two values, such as $25.
20 or 25

Contain a value that >49 and Returns records where the unit price is between (but
falls with a range of <99 not including) $49 and $99
values -or-
Between 50
and 100

Contain a value that <50 or >100 Returns records where the unit price is not between
falls outside a range $50 and $100.

Contain one of In(20, 25, Returns records where the unit price is either $20,
many specific values 30) $25, or $30.

Contain a value that Like "*4.99" Returns records where the unit price ends with
ends with the "4.99", such as $4.99, $14.99, $24.99, and so on.
specified digits

Contain null null (or Is Null Returns records where no value is entered in the
missing) values UnitPrice field.

Contain non-null Is Not Null Returns records where the value is not missing in the
values UnitPrice field.

Criteria for Date/Time fields

The following examples are for the OrderDate field in a query based on a table that stores
Orders information. The criterion is specified in the Criteria row of the field in the query design
grid.
4

To include records Use this


that ... criterion Query result

Exactly match a #2/2/2006# Returns records of transactions that took place


value, such as on Feb 2, 2006. Remember to surround date
2/2/2006 values with the # character so that Access can
distinguish between date values and text strings.

Do not match a Not #2/2/2006# Returns records of transactions that took place
value, such as on a day other than Feb 2, 2006.
2/2/2006

Contain values that < #2/2/2006# Returns records of transactions that took place
fall before a certain before Feb 2, 2006.
date, such as
2/2/2006 To view transactions that took place on or
before this date, use the <= operator instead of
the < operator.

Contain values that > #2/2/2006# Returns records of transactions that took place
fall after a certain after Feb 2, 2006.
date, such as
2/2/2006 To view transactions that took place on or after
this date, use the >= operator instead of the >
operator.
5

Contain values that >#2/2/2006# Returns records where the transactions took
fall within a date and place between Feb 2, 2006 and Feb 4, 2006.
range <#2/4/2006#
You can also use the Between operator to filter
for a range of values, including the end points.
For example, Between #2/2/2006# and
#2/4/2006# is the same as >=#2/2/2006# and
<=#2/4/2006# .

Contain values that <#2/2/2006# or Returns records where the transactions took
fall outside a range >#2/4/2006# place before Feb 2, 2006 or after Feb 4, 2006.

Contain one of two #2/2/2006# or Returns records of transactions that took place
values, such as #2/3/2006# on either Feb 2, 2006 or Feb 3, 2006.
2/2/2006 or
2/3/2006

Contain today's date Date() Returns records of transactions that took place
on the current day. If today's date is 2/2/2006,
you see records where the OrderDate field is set
to Feb 2, 2006.

Contain yesterday's Date()-1 Returns records of transactions that took place


date the day before the current day. If today's date is
2/2/2006, you see records for Feb 1, 2006.

Contain tomorrow's Date() + 1 Returns records of transactions that took place


date the day after the current day. If today's date is
2/2/2006, you see records for Feb 3, 2006.

Contain a date that < Date() Returns records of transactions that took place
occurred in the past before today.

Contain a date that > Date() Returns records of transactions that will take
occurrs in the future place after today.

Filter for null (or Is Null Returns records where the date of transaction is
missing) values missing.

Filter for non-null Is Not Null Returns records where the date of transaction is
values known.
6

Understand expressions

In Office Access 2007, the term expression is synonymous with formula. An expression consists
of a number of possible elements that you can use, alone or in combination, to produce a
result. Those elements include:

Identifiers — the names of table fields or controls on forms or reports, or the properties
of those fields or controls

Operators, such as + (plus) or - (minus)

Functions, such as SUM or AVG

You can use expressions in a number of ways — to perform a calculation, retrieve the value of a
control, or supply criteria to a query, just to name a few.

For more information about how and where to use expressions, see the article Create an
expression.

Examples of expressions used in forms and reports

The tables in this section provide examples of expressions that calculate a value in a control
located on a form or report. To create a calculated control, you enter an expression in the
ControlSource property of the control, instead of in a table field or query.

The following steps explain how to enter an expression in a text box control on an existing form
or report.

Create a calculated control

1. In the Navigation Pane, right-click the form or report that you want to change and click
Design View on the shortcut menu.

2. On the form or report, right-click the text box control that you want to change (not the
label associated with the text box), and then click Properties on the shortcut menu.

3. If necessary, click either the All tab or the Data tab. Both tabs provide the Control
Source property.
7

4. Click the box next to the Control Source property and type your expression. For
example, you can copy and paste an expression from the Expression column in the table
in the following section.

5. Close the property sheet.

Expressions that combine or manipulate text

The expressions in the following table use the & (ampersand) and the + (plus) operators to
combine text strings, built-in functions to manipulate a text string, or otherwise operate on text
to create a calculated control.

Expression Result

=[FirstName] & " " & Displays the values that reside in table fields called FirstName and
[LastName] LastName. In this example, the & operator is used to combine the
FirstName field, a space character (enclosed in quotation marks),
and the LastName field.

=Left([ProductName], 1) Uses the Left function to display the first character of the value of
a field or control called ProductName.

=Right([AssetCode], 2) Uses the Right function to display the last 2 characters of the
value in a field or control called AssetCode.

Expressions that perform arithmetic operations

You can use expressions to add, subtract, multiply, and divide the values in two or more fields
or controls. You can also use expressions to perform arithmetic operations on dates. For
example, suppose you have a Date/Time table field named RequiredDate. In the field, or in a
control bound to the field, the expression =[RequiredDate] - 2 returns a date/time value equal
to two days before the current values in the RequiredDate field.

Expression Result

=[Quantity]*[UnitPrice] The product of the values of the Quantity and Price fields or
controls.
8

Expressions that count, sum, and average values

You can use a type of function called an aggregate function to calculate values for one or more
fields or controls. For example, you can calculate a group total for the group footer in a report,
or an order subtotal for line items on a form. You can also count the number of items in one or
more fields or calculate an average value.

The expressions in the following table show some of the ways to use functions such as Avg,
Count, and Sum.

Expression Description

=Avg([Quantity]) Uses the Avg function to display the average of the values of a
table field or control named "quantity."

=Count([OrderID]) Uses the Count function to display the number of records in the
OrderID control.

=Sum([Quantity]*[Price]) Uses the Sum function to display the sum of the product of the
values of the Quantity and the Price controls.

For more information about using aggregate functions and totaling the values in field and
columns, see the articles Sum data by using a query, Count data by using a query, Count the
rows in a datasheet, and Display column totals in a datasheet.

Expressions that manipulate and calculate dates

Tracking dates and times is a fundamental database activity. For example, you can calculate
how many days have elapsed since the invoice date to age your accounts receivable. You can
format dates and times in numerous ways, as shown in the following table.

Expression Description

=Date() Uses the Date function to display the current date in the form of
mm-dd-yy, where mm is the month (1 through 12), dd is the day
(1 through 31), and yy is the last two digits of the year (1980
through 2099).
9

=Format(Now(), "ww") Uses the Format function to display the week number of the year
for the current date, where ww represents weeks 1 through 53.

=DatePart("yyyy", Uses the DatePart function to display the four-digit year of the
[OrderDate]) value of the OrderDate control.

=DateDiff("d", Uses the DateDiff function to display the number of days'


[OrderDate], difference between the values of the OrderDate and ShippedDate
[ShippedDate]) controls.

=[Order Date] + 30 Uses arithmetic operations on dates to calculate the date 30 days
after the date in the InvoiceDate field or control.
QUERIES AND CRITERIAS:

DateDiff ("yyyy", This criterion applies to a Date/Time field, such as BirthDate. Only
[BirthDate], Date()) records where the number of years between a person's birthdate
> 30 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.

Expressions that use dates in matching criteria

The expressions in the following table demonstrate the use of dates and related functions in
criteria expressions.

Field Expression Description

ShippedDate #2/2/2007# Displays orders shipped on February 2, 2007.

ShippedDate Date() Displays orders shipped today.

OrderDate < Date( ) - 30 Uses the Date function to display orders


more than 30 days old.

OrderDate Year([OrderDate])=2007 Uses the Year function to display orders with


order dates in 2007.

OrderDate DatePart("q", Uses the DatePart function to display orders


[OrderDate])=4 for the fourth calendar quarter.

ShippedDate Between #1/5/2007# And Uses the Between...And operator to display


10

#1/10/2007# orders shipped no earlier than 5-Jan-2007


and no later than 10-Jan-2007.

Examples of field validation rule expressions

You can create a validation rule for a field or control by using an expression. Access then
enforces the rule when data is entered into the field or control. To create a validation rule, you
modify the ValidationRule property of the field or control. You should also consider setting the
ValidationText property, which holds the text that Access displays when the validation rule is
violated. If you don't set the ValidationText property, Access displays a default error message.

Add a validation rule to a field

1. In the Navigation Pane, right-click the table that you want to change and click Design
View on the shortcut menu.

2. Click the field that you want to change.

3. Click the Validation Rule property box, located in the lower section of the table
designer.

4. Type the expression, or click the Build button to the right of the property box to
create an expression by using the Expression Builder.

NOTE Do not precede the expression with the = operator when you create a
validation rule.

The examples in the following table demonstrate the validation rule expressions for the
ValidationRule property and the associated text for the ValidationText property.

ValidationRule property ValidationText property

<> 0 Please enter a nonzero value.

0 Or > 100 Value must be either 0 or more than 100.

Like "K???" Value must be four characters, beginning with the letter
K.
11

< #1/1/2007# Enter a date prior to 1/1/2007.

>= #1/1/2007# And < Date must occur in 2007.


#1/1/2008#

For more information about validating data, see the article Create a validation rule to validate
data in a field.

Comparison operators

You use the comparison operators to compare values and return a result that is True, False, or
Null.

Operator Purpose Example

< Returns True if the Value1 < Value2


first value is less than
the second value.

<= Returns True if the Value1 <= Value2


first value is less than
or equal to the
second value.

> Returns True if the Value1 > Value2


first value is greater
than the second
value.

>= Returns True if the Value1 >= Value2


first value is greater
than or equal to the
second value.

= Returns True if the Value1 = Value2


first value is equal to
the second value.

<> Returns True if the Value1 <> Value2


first value is not
equal to the second
value.
12

Display column totals in a datasheet Commented [SA1]: 1/11/10

 Microsoft Office Access 2007 provides a new tool called the Totals row that simplifies
the process of summing a column of data.
 You can also use the Totals row for other calculations such as averages, counting the
number of items in a column, and finding the minimum or maximum value in a column
of data.

The Totals row makes it easier and faster to use a set of aggregate functions — functions that
calculate values across a range of data.

Understand Sum and the other aggregate functions

This table lists and describes the aggregate functions that Access provides in the Totals row.
Remember that Access provides additional aggregate functions, but you must use them in
queries

Function Description Use with the data type(s)

Average Calculates the average value for a column. Number, Decimal, Currency,
The column must contain numeric, Date/Time
currency, or date/time data. The function
ignores null values.

Count Counts the number of items in a column. All data types except complex
For counting Text data types. repeating scalar data, such as a
column of multivalued lists.
multiple values.

Maximum Returns the item with the highest value. Number, Decimal, Currency,
For text data, the highest value is the last Date/Time
alphabetic value; and Access ignores case.
The function ignores null values.

Minimum Returns the item with the lowest value. Number, Decimal, Currency,
For text data, the lowest value is the first Date/Time
13

alphabetic value; and Access ignores case.


The function ignores null values.

Sum Adds the items in a column. Works only Number, Decimal, Currency
on numeric and currency data.

The following section explains how to use each function in a Totals row.

Sum values by using the Totals row

If you need to sum a column of data or use another aggregate function quickly, you can add a
Totals row to a table, query result set, or a split form — a form that contains a datasheet.

Add a Totals row

1. Open a table or query in Datasheet view, or open a split form in Form view. To do so, in
the Navigation Pane, right-click the table, query, or form and click Design View or Form
View on the shortcut menu.

2. On the Home tab, in the Records group, click Totals.

A new Total row appears below the star row in your datasheet or split form.

3. In the Total row, click the field that you want to sum, and then select SUM from the list.
14

For information about the function, see the section Understand the aggregate functions,
earlier in this article.

Remove a Totals row

On the Home tab, in the Records group, click Totals.

Count the number of values in a column

You use the Count function when you need to count the number of items in a column. The
Count function runs only against a single column of data and returns a single value. The
function works with all data types.

Count items in a column

1. Open a table, query result set, or form. You can use any view except Design view.

2. On the Home tab, in the Records group, click Totals.


15

A new Totals row appears below the star row in your datasheet.

3. In the Total row, click the field that you want to count and select COUNT from the list.

Using Functions
To control how the values would display in the query, start a query in Design View or open
a query in Design View. In the bottom section of the window, in the box of the field name,
type the expression. For example, if you have a column named Gender and that display
the genders as Male or as Female but you want to display only M or F respectively, you
can use the Left() function in an expression as Left(Gender, 1):
16

Summary Queries
Introduction
Consider the following list of students:
17

A query is referred to as summary if it provides one or various analytic statistics about the
records.

Practical Learning: Introducing Summary Queries


1. Start Microsoft Access
2. From the resources that accompany our lessons, open the Bethesda Car Rental1
database
3. On the Ribbon, click Create and, in the Queries section, click Query Design
4. In the Show Tables dialog box, click Company Assets, click Add, and click Close
5. In the top section of the window, double-click Category
6. Double-click Category again
7. Double-click Purchase Price
8. Double-click Purchase Price again

Creating a Summary Query


As always, when creating a query, you can use the Query Wizard or display one in Design
View. If you are working in Design View, to make a query summarize its data:
18

 In the Show/Hide section of the Ribbon, click the Totals button


 Right-click the bottom section of the query window and click Totals

After applying the Totals feature, each column of the query would become equipped with
a row named Total.

Although you can create a summary query with all the fields or any field(s) of a query, the
purpose of the query is to summarize data, not to review the records, which you would do
with a normal select query. For a good summary query, you should select a column where
the records hold categories of data. This means that the records in the resulting list have
to be grouped by categories.

For example, if you want to get the number of students by gender from a list of students,
you would select the column that holds that information, but you can select other columns
also:

When the results come up, they would be grouped by their categories:
19

As stated already, the purpose of a summary query is to provide some statistics.


Therefore, it is normal that you be interested only in the column(s) that hold(s) the
desired statistics and avoid the columns that are irrelevant:
20

If you select (only) the one column that holds the information you want, in the resulting
list, each of its categories would display only once:

Practical Learning: Creating a Summary Query


 In the bottom section of the window, right-click a cell and click Totals

Summarizing the Values


To get the types of statistics you want, in the Design View of the query, add the same
column one more time, and click the Total box that corresponds to the column:
21

In reality, a summary query uses some of the functions that ship with Microsoft Access:

 Count: Microsoft Access uses the Count() function to count the number of
occurrences of the category in the column and produces the total
22

 First: Microsoft Access uses the First() function to get the first occurrence of the
value in the category
23

 Last: Microsoft Access uses the Last() function to get the last occurrence of the
value in the category
 Consider the following table that shows the list of employees and the time they
worked:

Consider the following query that wants to summarize the time worked:
24
25

If the column holds numeric values:


o Sum: The Sum() function is used to sum up the values in the category

o Avg: The sum of values in a category would be divided by the number of


occurrences in that category to get the average. Here is an example:
26

o Min: The lowest value of the category would be produced from


the Min() function

The result is the lowest time worked for each employee:


27

o Max: The highest value of the category would be produced using


the Max() function. Here is an example:

 Expression: Consider the following query that we saw earlier:


28

Imagine that you want to get the percentage of occurrences of each gender. To get such a value, you can
write an expression such as (Count(Gender) / 164) * 100. If you want to do this in a summary query,
you can specify the Total type as Expression. Here is an example:

 Where: Notice that the above query has an empty (or null) record. This makes that
record quite useless for the query. We know that you can set a condition in
the Criteria box of the column used as the reference, which in this case would be
Gender. Instead of using the referential column, you can add the same column one
more time, set its Total to Where and, in its Criteria box, enter a WHERE type of
statement. Also, this additional column should not be shown. This means that, if
creating the query visually, you must clear its Show check box. Here is an example:
29

This would produce:

Practical Learning: Summarizing


1. In the bottom section of the window, change the header of the second column
to Qty: Category
2. Click its Total combo box and select Count
3. Change the header of the third column to Total Spent: Purchase Price
4. Click its Total combo box and select Sum
5. Change the header of the fourth column to Average: Purchase Price
30

6. Click its Total combo box and select Avg

7. Right-click the title bar and click Datasheet View

8. Close the query


9. When asked whether you want to save, click No

1. Open the Altair Realtors2 database


2. On the Ribbon, click Create
3. In the Queries section, click Query Design
4. In the Show Tables dialog box, double-click Properties and click Close
31

5. On the Ribbon, click the Totals button


6. In the list of fields, double-click Property Type, Market Value, Market Value, and
Property Type
7. In the bottom section of the window, change the header of the second column
to Cheapest: Market Value
8. Click its Total combo box and select Min
9. Change the header of the third column to Most Expensive: Market Value
10. Click its Total combo box and select Max
11. For the fourth column, set its Total to Where (clear its Show check box if necessary)
12. Set its Criteria to Is Not Null and press Enter

10. Right-click the query and click SQL View


32

11. Right-click the title bar and click Datasheet View

12. Close the query


13. When asked whether you want to save, click No

You might also like