0% found this document useful (0 votes)
73 views48 pages

Examples of Query Criteria

This document provides examples of query criteria for different data types in Microsoft Access. It explains how to add criteria to queries in Design view and describes common criteria types like equality, inequality, empty values and wildcard matching that can be used to filter records by text, numeric, date and other field values.

Uploaded by

vincentnjuki7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views48 pages

Examples of Query Criteria

This document provides examples of query criteria for different data types in Microsoft Access. It explains how to add criteria to queries in Design view and describes common criteria types like equality, inequality, empty values and wildcard matching that can be used to filter records by text, numeric, date and other field values.

Uploaded by

vincentnjuki7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Examples of query criteria

Access for Microsoft 365 Access 2021 Access 2019 Access 2016

Query criteria help you zero in on specific items in an Access database. If an


item matches all the criteria you enter, it appears in the query results.

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.

A query criterion is an expression that Access compares to query field values


to determine whether to include the record that contains each value. For
example, = "Chicago" is an expression that Access can compare to values in
a text field in a query. If the value for that field in a given record
is "Chicago", Access includes the record in the query results.

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
 Criteria for Text, Memo, and Hyperlink fields
 Criteria for Number, Currency, and AutoNumber fields
 Criteria for Date/Time fields
 Criteria for Yes/No fields
 Criteria for other fields

Introduction to query criteria


A criterion is similar to a formula — it is a string that may consist of field
references, operators, and constants. Query criteria are also referred to as
expressions in Access.

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 This criterion applies to a Date/Time field, such
("yyyy", as BirthDate. Only records where the number of
[BirthDate], years between a person's birthdate and
Date()) > 30 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
Criteria Description
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 the
full list of functions, operators, special characters, and the syntax for
expressions referring to fields and literals.

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())
1. The City and BirthDate fields include criteria.

2. Only records where the value of the City field is Chicago will satisfy this
criterion.

3. Only records of those who are at least 40 years old 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?

If you have alternate criteria, or two sets of independent criteria where it is


sufficient to satisfy one set, you use both the Criteria and the or rows in the
design grid.
1. The City criterion is specified in the Criteria row.

2. The BirthDate criterion is specified in the or row.

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.
Before you continue with the examples, note the following:

 If the criteria is temporary or changes often, you can filter the query result
instead of frequently modifying the query criteria. A filter is a temporary
criterion that changes the query result without altering the design of the
query. For more information about filters, see the article Apply a filter to
view select records in an Access database.
 If the criteria fields don't change, but the values you are interested in do
change frequently, you can create a parameter query. A parameter query
prompts the user for field values, and then uses those values to create the
query criteria. For more information about parameter queries, see the
article Use parameters in queries and reports.

Criteria for Text, Memo, and Hyperlink


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

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) =
"http://www.microsoft.com/", where Table1 is the name of the table
containing the hyperlink field, Field1 is the hyperlink field, and
http://www.microsoft.com is the URL you want to match.

To include Use this criterion Query result


records that...
Exactly match "China" Returns records where
a value, such the CountryRegion field
as China is set to China.
Do not match Not "Mexico" Returns records where
a value, such the CountryRegion field
as Mexico is set to a
country/region other
than Mexico.
Begin with the Like U* Returns records for all
specified countries/regions whose
string, such as names start with "U",
U such as UK, USA, and so
To include Use this criterion Query result
records that...
on.
Note: When used in an expression, the
asterisk (*) represents any string of
characters — it is also called a wildcard
character. For a list of such characters, see
the article Access wildcard character
reference.

Do not begin Not Like U* Returns records for all


with the countries/regions whose
specified names start with a
string, such as character other than
U "U".
Contain the Like "*Korea*" Returns records for all
specified countries/regions that
string, such as contain the string
Korea "Korea".
Do not contain Not Like "*Korea*" Returns records for all
To include Use this criterion Query result
records that...
the specified countries/regions that
string, such as do not contain the
Korea string "Korea".
End with the Like "*ina" Returns records for all
specified countries/regions whose
string, such as names end in "ina", such
"ina" as China and Argentina.
Do not end Not Like "*ina" Returns records for all
with the countries/regions that
specified do not end in "ina",
string, such as such as China and
"ina" Argentina.
Contain null Is Null Returns records where
(or missing) there is no value in the
values field.
To include Use this criterion Query result
records that...
Do not contain Is Not Null Returns records where
null values the value is not missing
in the field.
Contain zero- "" (a pair of quotes) Returns records where
length strings the field is set to a blank
(but not null) value. For
example, records of
sales made to another
department might
contain a blank value in
the CountryRegion field.
Do not contain Not "" Returns records where
zero-length the CountryRegion field
strings has a nonblank value.
Contains null "" Or Is Null Returns records where
To include Use this criterion Query result
records that...
values or zero- there is either no value
length strings in the field, or the field
is set to a blank value.
Is not empty or Is Not Null And Not "" Returns records where
blank the CountryRegion field
has a nonblank, non-
null value.
Follow a value, >= "Mexico" Returns records of all
such as countries/regions,
Mexico, when beginning with Mexico
sorted in and continuing through
alphabetical the end of the alphabet.
order
Fall within a Like "[A-D]*" Returns records for
specific range, countries/regions whose
To include Use this criterion Query result
records that...
such as A names start with the
through D letters "A" through "D".
Match one of "USA" Or "UK" Returns records for USA
two values, and UK.
such as USA or
UK
Contain one of In("France", "China", Returns records for all
the values in a "Germany", "Japan") countries/regions
list of values specified in the list.
Contain certain Right([CountryRegion], Returns records for all
characters at a 1) = "y" countries/regions where
specific the last letter is "y".
position in the
field value
Satisfy length Len([CountryRegion]) > Returns records for
To include Use this criterion Query result
records that...
requirements 10 countries/regions whose
name is more than 10
characters long.
Match a Like "Chi??" Returns records for
specific countries/regions, such
pattern as China and Chile,
whose names are five
characters long and the
first three characters are
"Chi".
Note: The characters ? and _, when used in
an expression, represent a single
character — these are also called wildcard
characters. The character _ cannot be used
in the same expression with the ? character,
nor can it be used in an expression with
the * wildcard character. You may use the
wildcard character _ in an expression that
also contains the % wildcard character.
Criteria for Number, Currency, and
AutoNumber fields
The following examples are for the UnitPrice 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 Use this Query Result
records that... criterion
Exactly match 100 Returns records where the unit price
a value, such of the product is $100.
as 100
Do not match Not 1000 Returns records where the unit price
a value, such of the product is not $1000.
as 1000
Contain a < 100 Returns records where the unit price
value smaller <= 100 is less than $100 (<100). The second
than a value, expression (<=100) displays records
such as 100 where the unit price is less than or
equal to $100.
Contain a >99.99 Returns records where the unit price
value larger >=99.99 is greater than $99.99 (>99.99). The
than a value, second expression displays records
such as 99.99 where the unit price is greater than or
To include Use this Query Result
records that... criterion
equal to $99.99.
Contain one of 20 or 25 Returns records where the unit price
the two values, is either $20 or $25.
such as 20 or
25
Contain a >49.99 Returns records where the unit price
value that falls and is between (but not including) $49.99
with a range of <99.99 and $99.99.
values -or-
Between
50 and
100
Contain a <50 or Returns records where the unit price
value that falls >100 is not between $50 and $100.
outside a
To include Use this Query Result
records that... criterion
range
Contain one of In(20, 25, Returns records where the unit price
many specific 30) is either $20, $25, or $30.
values
Contain a Like Returns records where the unit price
value that "*4.99" ends with "4.99", such as $4.99,
ends with the $14.99, $24.99, and so on.
specified digits Note: The characters * and %, when used in an expression,
represent any number of characters — these are also called wildcard
characters. The character % cannot be used in the same expression
with the * character, nor can it be used in an expression with
the ? wildcard character. You may use the wildcard character % in an
expression that also contains the _ wildcard character.

Contain null Is Null Returns records where no value is


null (or entered in the UnitPrice field.
missing) values
Contain non- Is Not Returns records where the value is
To include Use this Query Result
records that... criterion
null values Null not missing in the 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.
To Use this criterion Query result
include
records
that ...
Exactly #2/2/2006# Returns records
match a of transactions
value, that took place
To Use this criterion Query result
include
records
that ...
such as on Feb 2, 2006.
2/2/2006 Remember to
surround date
values with the #
character so that
Access can
distinguish
between date
values and text
strings.
Do not Not #2/2/2006# Returns records
match a of transactions
value, that took place
To Use this criterion Query result
include
records
that ...
such as on a day other
2/2/2006 than Feb 2, 2006.
Contain < #2/2/2006# Returns records
values of transactions
that fall that took place
before a before Feb 2,
certain 2006.
date,
To view
such as
transactions that
2/2/2006
took place on or
before this date,
use
the <= operator
To Use this criterion Query result
include
records
that ...

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

instead of
the > operator.
Contain >#2/2/2006# and <#2/4/2006# Returns records
values where the
that fall transactions took
within a place between
date Feb 2, 2006 and
range Feb 4, 2006.

You can also use


the Between ope
rator to filter for
a range of values,
To Use this criterion Query result
include
records
that ...

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 <#2/2/2006# or >#2/4/2006# Returns records
values where the
that fall transactions took
To Use this criterion Query result
include
records
that ...
outside a place before Feb
range 2, 2006 or after
Feb 4, 2006.
Contain #2/2/2006# or #2/3/2006# Returns records
one of of transactions
two that took place
values, on either Feb 2,
such as 2006 or Feb 3,
2/2/2006 2006.
or
2/3/2006
Contain In (#2/1/2006#, #3/1/2006#, Returns records
one of #4/1/2006#) where the
To Use this criterion Query result
include
records
that ...
many transactions took
values place on Feb 1,
2006, March 1,
2006, or April 1,
2006.
Contain DatePart("m", [SalesDate]) = 12 Returns records
a date where the
that falls transactions took
in a place in
specific December of any
month year.
(irrespect
ive of
To Use this criterion Query result
include
records
that ...
year),
such as
Decembe
r
Contain DatePart("q", [SalesDate]) = 1 Returns records
a date where the
that falls transactions took
in a place in the first
specific quarter of any
quarter year.
(irrespect
ive of
year),
To Use this criterion Query result
include
records
that ...
such as
the first
quarter
Contain Date() Returns records
today's of transactions
date 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,
To Use this criterion Query result
include
records
that ...
2006.
Contain Date()-1 Returns records
yesterda of transactions
y's date that took place
the day before
the current day. If
today's date is
2/2/2006, you
see records for
Feb 1, 2006.
Contain Date() + 1 Returns records
tomorro of transactions
w's date that took place
To Use this criterion Query result
include
records
that ...
the day after the
current day. If
today's date is
2/2/2006, you
see records for
Feb 3, 2006.
Contain DatePart("ww", [SalesDate]) = Returns records
dates DatePart("ww", Date()) and of transactions
that fall Year( [SalesDate]) = Year(Date()) that took place
during during the
the current week. A
current week starts on
week Sunday and ends
To Use this criterion Query result
include
records
that ...
on Saturday.
Contain Year([SalesDate])* 53 + Returns records
dates DatePart("ww", [SalesDate]) = of transactions
that fell Year(Date())* 53 + DatePart("ww", that took place
during Date()) - 1 during the last
the week. A week
previous starts on Sunday
week and ends on
Saturday.
Contain Year([SalesDate])* Returns records
dates 53+DatePart("ww", [SalesDate]) = of transactions
that fall Year(Date())* 53+DatePart("ww", that will take
during Date()) + 1 place next week.
To Use this criterion Query result
include
records
that ...
the A week starts on
following Sunday and ends
week on Saturday.
Contain Between Date() and Date()-6 Returns records
a date of transactions
that fell that took place
during during the last 7
the last 7 days. If today's
days date is 2/2/2006,
you see records
for the period Jan
24, 2006 through
Feb 2, 2006.
To Use this criterion Query result
include
records
that ...
Contain Year([SalesDate]) = Year(Now()) And Returns records
a date Month([SalesDate]) = Month(Now()) for the current
that month. If today's
belongs date is 2/2/2006,
to the you see records
current for Feb 2006.
month
Contain Year([SalesDate])* 12 + Returns records
a date DatePart("m", [SalesDate]) = for the previous
that Year(Date())* 12 + DatePart("m", month. If today's
belongs Date()) - 1 date is 2/2/2006,
to the you see records
previous for Jan 2006.
To Use this criterion Query result
include
records
that ...
month
Contain Year([SalesDate])* 12 + Returns records
a date DatePart("m", [SalesDate]) = for the next
that Year(Date())* 12 + DatePart("m", month. If today's
belongs Date()) + 1 date is 2/2/2006,
to the you see records
next for Mar 2006.
month
Contain Between Date( ) And DateAdd("M", A month's worth
a date -1, Date( )) of sales records.
that fell If today's date is
during 2/2/2006, you
the last see records for
To Use this criterion Query result
include
records
that ...
30 or 31 the period Jan 2,
days 2006. to Feb 2,
2006
Contain Year([SalesDate]) = Year(Now()) And Returns records
a date DatePart("q", Date()) = DatePart("q", for the current
that Now()) quarter. If today's
belongs date is 2/2/2006,
to the you see records
current for the first
quarter quarter of 2006.
Contain Year([SalesDate])*4+DatePart("q", Returns records
a date [SalesDate]) = for the previous
that Year(Date())*4+DatePart("q",Date()) quarter. If today's
To Use this criterion Query result
include
records
that ...
belongs -1 date is 2/2/2006,
to the you see records
previous for the last
quarter quarter of 2005.
Contain Year([SalesDate])*4+DatePart("q", Returns records
a date [SalesDate]) = for the next
that Year(Date())*4+DatePart("q",Date()) quarter. If today's
belongs +1 date is 2/2/2006,
to the you see records
next for the second
quarter quarter of 2006.
Contain Year([SalesDate]) = Year(Date()) Returns records
a date for the current
To Use this criterion Query result
include
records
that ...
that falls year. If today's
during date is 2/2/2006,
the you see records
current for the year 2006.
year
Contain Year([SalesDate]) = Year(Date()) - 1 Returns records
a date of transactions
that that took place
belongs during the
to the previous year. If
previous today's date is
year 2/2/2006, you
see records for
To Use this criterion Query result
include
records
that ...
the year 2005.
Contain Year([SalesDate]) = Year(Date()) + 1 Returns records
a date of transactions
that with next year's
belongs date. If today's
to next date is 2/2/2006,
year you see records
for the year 2007.
Contain Year([SalesDate]) = Year(Date()) and Returns records
a date Month([SalesDate]) <= of transactions
that falls Month(Date()) and Day([SalesDate]) with dates that
between <= Day (Date()) fall between Jan
Jan 1 and 1 of the current
To Use this criterion Query result
include
records
that ...
today year and today. If
(year to today's date is
date 2/2/2006, you
records) see records for
the period Jan 1,
2006 to to
2/2/2006.
Contain < Date() Returns records
a date of transactions
that that took place
occurred before today.
in the
past
To Use this criterion Query result
include
records
that ...
Contain > Date() Returns records
a date of transactions
that that will take
occurrs place after today.
in the
future
Filter for Is Null Returns records
null (or where the date of
missing) transaction is
values missing.
Filter for Is Not Null Returns records
non-null where the date of
values transaction is
To Use this criterion Query result
include
records
that ...
known.

Criteria for Yes/No fields


As an example, your Customers table has a Yes/No field named Active, used
to indicate whether a customer's account is currently active. The following
table shows how values entered in the Criteria row for a Yes/No field are
evaluated.

Field value Result


Yes, True, 1, or -1 Tested for a Yes value. A value of 1 or -1
is converted to "True" in the Criteria row
after you enter it.
Field value Result
No, False, or 0 Tested for a No value. A value of 0 is
converted to "False" in the Criteria row
after you enter it.
No value (null) Not tested
Any number other than No results if it's the only criteria value in
1, -1, or 0 the field
Any character string Query fails to run due to Data type
other than Yes, No, mismatch error
True, or False

Criteria for other fields


Attachments In the Criteria row, type Is Null to include records that do
not contain any attachments. Type Is Not Null to include records that
contain attachments.
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:

1. Locate the source table in the Navigation Pane.


2. Open the table in Design view by either:
 Clicking the table, and then pressing CTRL+ENTER
 Right-clicking the table, and then clicking Design View.

3. The data type for each field is listed in the Data Type column of the table
design grid.

Multivalued fields Data in a multivalued field are stored as rows in a


hidden table that Access creates and populates to represent the field. In
query Design view, this is represented in the Field List by using an
expandable field. To use criteria for a multivalued field, you supply criteria for
a single row of the hidden table. To do this:

1. Create a query containing the multivalued field, and open it in Design


view.
2. Expand the multivalued field by clicking the plus symbol (+) next to it — if
the field is already expanded, this is a minus symbol (-). Just below the
name of the field, you will see a field representing a single value of the
multivalued field. This field will have the same name as the multivalued
field, with the string .Value appended.
3. Drag the multivalued field and its single value field to separate columns in
the design grid. If you want to see only the complete multivalue field in
your results, clear the Show check box for the single value field.
4. Type your criteria in the Criteria row for the single value field, using
criteria that is appropriate for whatever type of data the values represent.
5. Each value in the multivalued field will be individually evaluated using the
criteria you supply. For example, you may have a multivalued field that
stores a list of numbers. If you supply the criteria >5 AND <3, any record
where there is at least one value greater than 5 and one value less than 3
will match.

You might also like