Access 2010: Creating Queries
Access 2010: Creating Queries
Table of Contents
INTRODUCTION TO QUERIES ..................................................................................................................... 2
QUERY JOINS .................................................................................................................................................... 2
INNER JOINS ........................................................................................................................................................ 3
OUTER JOINS ...................................................................................................................................................... 3
CHANGE A JOIN PROPERTY ................................................................................................................................. 4
REMOVING A JOIN ............................................................................................................................................... 4
CREATE QUERIES ........................................................................................................................................... 4
THE BASICS OF CREATING SELECT QUERIES ....................................................................................................... 5
USE THE QUERY WIZARD TO BUILD A QUERY .................................................................................................... 5
CREATE A SELECT QUERY IN DESIGN VIEW ........................................................................................................ 6
OPEN AN EXISTING QUERY ................................................................................................................................. 6
USING CRITERIA IN A QUERY .................................................................................................................... 6
ADD CRITERIA TO THE QUERY ............................................................................................................................ 7
Examples of query criteria ............................................................................................................................. 7
SPECIFY CRITERIA BY USING AND OR OR ......................................................................................................... 8
AND Operator ................................................................................................................................................ 8
OR Operator ................................................................................................................................................... 8
CRITERIA FOR TEXT, MEMO, AND HYPERLINK FIELDS....................................................................................... 9
Hyperlink criteria are a little tricky .............................................................................................................. 11
CRITERIA FOR NUMBER, CURRENCY, AND AUTONUMBER FIELDS .................................................................. 11
CRITERIA FOR DATE/TIME FIELDS .................................................................................................................... 13
CRITERIA FOR OTHER FIELDS............................................................................................................................ 17
USE PARAMETERS IN QUERIES................................................................................................................ 17
USING WILDCARD CHARACTERS IN STRING COMPARISONS ............................................................................ 17
CREATE A PARAMETER QUERY ......................................................................................................................... 18
RUN A PARAMETER QUERY ............................................................................................................................... 18
MATCHING PART OF A FIELD VALUE WITH A PARAMETER STRING ................................................................... 18
SPECIFY PARAMETER DATA TYPES IN THE QUERY PARAMETER DIALOG BOX .................................................. 19
USING EXPRESSIONS IN A QUERY........................................................................................................... 20
USE EXPRESSIONS AS QUERY CRITERIA ............................................................................................................ 20
ADD CALCULATIONS TO THE QUERY ................................................................................................................ 21
ENTER EXPRESSION CRITERIA IN THE QUERY DESIGN GRID .............................................................................. 21
CALCULATE GROUP TOTALS BY USING A TOTALS QUERY ................................................................................ 22
USE THE EXPRESSION BUILDER .............................................................................................................. 23
WHY USE THE EXPRESSION BUILDER? ............................................................................................................. 23
NEW TOOLS FOR ENTERING EXPRESSIONS IN ACCESS 2010 ............................................................................. 23
IntelliSense and Quick Tips ......................................................................................................................... 23
Quick Info and Help ..................................................................................................................................... 24
OVERVIEW OF THE EXPRESSION BUILDER ....................................................................................................... 24
Expression box ............................................................................................................................................. 25
Expression elements, categories, and values ................................................................................................ 25
START THE EXPRESSION BUILDER FROM A QUERY .......................................................................................... 26
Introduction to queries
When you want to review, add, change, or delete data from your database consider using a query.
Using a query, you can answer very specific questions about your data that would be difficult to answer by
looking at table data directly. You can use queries to filter your data, to perform calculations with your data,
and to summarize your data. You can also use queries to automate many data management tasks and to review
changes in your data before you commit to those changes.
A query is a request for data results, for action on data, or for both. You can use a query to answer a simple
question, to perform calculations, to combine data from different tables, or even to add, change, or delete table
data. Queries that you use to retrieve data from a table or to make calculations are called select queries.
Queries that add, change, or delete data are called action queries.
Sometimes you may want to review all of the data from a table, but at other times, you may want to review
only the data from certain fields, or you may want to review data only if certain fields meet certain criteria. To
review data using criteria, you use a select query.
A select query is a type of database object that shows information in Datasheet view. A query can get its data
from one or more tables, from existing queries, or from a combination of the two. The tables or queries from
which a query gets its data are referred to as its recordsource.
Whether you create simple select queries by using a wizard or by working in Design view, the steps are
essentially the same. You choose the recordsource that you want to use and the fields that you want to include
in the query — and, optionally, you specify criteria to refine the results.
After you have created a select query, you run it to see the results. Running a select query is simple — you
just open it in Datasheet view. After saving, you can then reuse it whenever you need, for example, as a
recordsource for a form, report, or another query.
In a well-designed database, the data that you want to present by using a form or report is often located in
several different tables. By using a query, you can assemble the data that you want to use before you design
your form or report.
Query Joins
Relational databases consist, at the most basic level, of tables that bear logical relationships to each other. You
use relationships to connect tables on fields that they have in common. A relationship is represented in a query
by a join.
When you add tables to a query, Microsoft Office Access 2010 creates joins that are based on relationships
that have been defined between the tables. You can manually create joins in queries, even if they do not
represent relationships that have already been defined. If you use other queries (instead of, or in addition to,
tables) as sources of data for a query, you can create joins between the source queries, and also between those
queries and any tables that you use as sources of data.
Joins behave similarly to query criteria in that they establish rules that the data must match to be included in
the query operations. Unlike criteria, joins also specify that each pair of rows that satisfy the join conditions
will be combined in the recordset to form a single row.
When you include multiple tables in a query, you use joins to help you get the results you are looking for. A
join helps a query return only the records from each table you want to see, based on how those tables are
related to other tables in the query.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 2
There are four basic types of joins: inner joins, outer joins, cross joins, and unequal joins. Inner and outer joins
are most common.
Inner joins
Inner joins are the most common type of join. They tell a query that rows from one of the joined tables
correspond to rows in the other table, on the basis of the data in the joined fields. When a query with an inner
join is run, only those rows where a common value exists in both of the joined tables will be included in the
query operations.
Most of the time, you don't need to do anything to use an inner join. If you previously created relationships
between tables in the Relationships window, Access automatically creates inner joins when you add related
tables in query Design view. If referential integrity is enforced, Access also displays a "1" above the join line
to show which table is on the "one" side of a one-to-many relationship and an infinity symbol (∞) to show
which table is on the "many" side.
The purpose of using referential integrity is to prevent orphan records and to keep references synchronized so
that you don't have any records referencing other records that no longer exist. You enforce referential integrity
by enabling it for a table relationship. Once enforced, Access rejects any operation that would violate
referential integrity for that table relationship. Access rejects updates that change the target of a reference, and
also deletions that remove the target of a reference.
Removing a join
If you create a join by mistake, for example, a join between two fields that have dissimilar data types, you can
delete it. To delete the join:
1) In the query design grid, click the join you want to remove.
2) Press DELETE.
a) Alternatively, you can right-click the join you want to remove, and then click Delete
Create queries
Sometimes, the process of building and using queries is a simple matter of selecting fields from a table,
perhaps applying some criteria, and then viewing the results. But what if, as is more often the case, the data
you need is spread out in more than one table? You may have cases in which a query that is based on one table
gives you the information you need, but pulling data from another table would help to make the query results
even clearer and more useful. For example, suppose you have a list of employee IDs that appear in your query
results. You realize it would be more useful to view the employee name in the results, but the employee names
are located in a different table. Fortunately, you can build a query that combines information from multiple
sources.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 4
The basics of creating select queries
Access provides two primary ways to create select queries — the Query Designer and the Query Wizard.
Regardless of the tool you use, you follow some common steps when you create a select query:
Start by choosing a record source for the query. A
record source can be one or more tables, one or
more queries, or a combination of the two. The
picture shows a table open in the Query Designer.
From the record source, select the fields that you
want to see in the query.
Add any sorting, filtering, or other selection
criteria to your queries.
For example, if you use the criteria shown in the
picture, the query will only return data for assets purchased after May First of 2010.
After you finish adding fields and any selection criteria, run your query to see if it gives you the correct
results.
You can refine your query by adding or removing fields, or by changing your selection criteria until you have
just the data you need, and then you can put the query to work.
Use the Query Wizard to build a query
1) If you are not sure, check that the tables have a defined relationship in the Relationships window
2) Create tab on the Ribbon / Queries group / Query Wizard.
3) In the New Query dialog box, click Simple Query Wizard, and then click OK.
4) In the Tables/Queries combo box, select the table that contains the basic information you want included
in your query.
5) In the Available Fields list, click the first field you want to include in your query, and then click the
single right arrow button to move that field to the Selected Fields list.
a) Do the same with each additional field from that table that you want to include in your query. These
can be fields that you want returned in the query output, or fields that you want to use to limit the rows
in the output by applying criteria.
b) If you want to include additional fields that are not in the current table, in the Tables/Queries combo
box, select the table that contains the related data you want to use to enhance your query results.
c) After adding the fields that you want to use to the Selected Fields list, click Next.
7) If the Wizard displays the “Would you like a detail or summary query?” page, click either Detail or
Summary.
a) If you do not want your query to perform any aggregate functions (Sum, Avg, Min, Max, Count,
StDev, or Var), choose a detail query.
b) If you do want your query to perform an aggregate function, choose a summary query.
c) After you make your choice, click Next.
6) Enter a title for the query.
7) Click Finish to view the results.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 5
Create a select query in Design view
A select query is used to create subsets of data that you can use to answer specific questions. It can also be
used to supply data to other database objects. Once you create a select query, you can use it whenever you
need.
1) On the Create tab, in the Queries group, click Query Design.
a) The Show Table dialog box appears.
2) In the dialog box, click the table that you want to use in the query, click Add to place the table in the upper
section of the designer, and then click Close.
a) Alternatively, you can also double-click the table to add it to the upper section of the designer, and then
click Close.
3) Add the fields that you want to use in your query to the design grid.
a) You can double-click each field, or drag and drop each field on a blank cell in the Field row.
4) In the query design grid, use the Criteria row to enter field criteria.
a) To use a field criterion without displaying the field in the query results, clear the check box in the
Show row for that field.
b) To sort the results based on the values in a field, in the query design grid, click Ascending or
Descending (depending on which way you want to sort the records) in the Sort row for that field.
5) Design tab / Results group / Run.
a) Access displays the query output in Datasheet view.
When you run a select query, Access displays the
results in a datasheet. The result is called a record
set, and you can work with it in the same way that
you work with a datasheet. For example, you can
add or change data, and Access will write your
changes to the tables that serve as the record
sources for your query.
You can also use your record sets to provide data
for forms, reports, and even other queries.
For example, if you want a report on the computers that you need to replace, you can create a query that
returns that data, and then quickly build your report. Note that the picture shows this - the query returns only
those records where an asset has been marked for retirement.
Open an existing query
If you are not familiar with the Navigation Pane, you may not know how to open a query that already exists.
The Navigation Pane is a feature that replaces the Database Window from versions prior to Access 2007.
You'll see the Navigation Pane along the left side of your screen.
To open any database object, including a query, you can double-click the object in the Navigation Pane.
>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", [BirthDate], Date()) > 30 This criterion applies to a Date/Time field, such as
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.
OR Operator
To specify alternate criteria, use both the Criteria and Or rows in the design grid. All records that meet the
criteria defined either in the Criteria row or in the Or row are included in the result.
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.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 8
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 changes a query result without altering the design of the query.
If the criteria fields don't change, but the values that you want to use do change frequently, you can create
a parameter query. A parameter query asks you for input when you run the query, and then uses your input
to create the query criteria.
Do not match a value, Not "Mexico" Returns records where the CountryRegion
such as Mexico field is set to a country/region other than
Mexico.
Begin with the specified Like U* Returns records for all countries/regions
string, such as U whose 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
specified string, such as whose names start with a character other
U than "U".
Contain the specified Like "*Korea*" Returns records for all countries/regions that
string, such as Korea contain the string "Korea".
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 9
Criteria for Text, Memo, and Hyperlink fields
To include records Use this criterion Query result
that...
Do not contain the Not Like "*Korea*" Returns records for all countries/regions that
specified string, such as do not contain the string "Korea".
Korea
End with the specified Like "*ina" Returns records for all countries/regions
string, such as "ina" whose names end in "ina", such as China
and Argentina.
Do not end with the Not Like "*ina" Returns records for all countries/regions that
specified string, such as do not end in "ina", such as China and
"ina" Argentina.
Contain null (or missing) Is Null Returns records where there is no value in
values the field.
Do not contain null Is Not Null Returns records where the value is not
values missing in the field.
Contain zero-length "" (a pair of quotes) Returns records where the field is set to a
strings 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 zero- Not "" Returns records where the CountryRegion
length strings field has a nonblank value.
Contains null values or "" Or Is Null Returns records where there is either no
zero-length strings value in the field, or the field is set to a
blank value.
Is not empty or blank Is Not Null And Not "" Returns records where the CountryRegion
field has a nonblank, non-null value.
Fall within a specific Like "[A-D]*" Returns records for countries/regions whose
range, such as A through names start with the letters "A" through "D".
D
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 10
Criteria for Text, Memo, and Hyperlink fields
To include records Use this criterion Query result
that...
Match one of two values, "USA" Or "UK" Returns records for USA and UK.
such as USA or UK
Contain one of the values In("France", "China", Returns records for all countries/regions
in a list of values "Germany", "Japan") specified in the list.
Contain certain characters Right([CountryRegion], 1) = "y" Returns records for all countries/regions
at a specific position in where the last letter is "y".
the field value
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".
Note: The character ?, when used in an
expression, represents a single character —
it is also called a wildcard character.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 11
Criteria for Number, Currency, and AutoNumber fields
To include records that... Use this criterion Query Result
Exactly match a value, such 100 Returns records where the unit price of the product
as 100 is $100.
Do not match a value, such as Not 1000 Returns records where the unit price of the product
1000 is not $1000.
Contain a value smaller than a < 100 Returns records where the unit price is less than
value, such as 100 <= 100 $100 (<100). The second expression (<=100)
displays records where the unit price is less than or
equal to $100.
Contain a value larger than a >99.99 Returns records where the unit price is greater than
value, such as 99.99 >=99.99 $99.99 (>99.99). The second expression displays
records where the unit price is greater than or equal
to $99.99.
Contain one of the two values, 20 or 25 Returns records where the unit price is either $20 or
such as 20 or 25 $25.
Contain a value that falls with >49.99 and <99.99 Returns records where the unit price is between (but
a range of values -or- not including) $49.99 and $99.99.
Between 50 and 100
Contain a value that falls <50 or >100 Returns records where the unit price is not between
outside a range $50 and $100.
Contain one of many specific In(20, 25, 30) Returns records where the unit price is either $20,
values $25, or $30.
Contain a value that ends with Like "*4.99" Returns records where the unit price ends with
the specified digits "4.99", such as $4.99, $14.99, $24.99, and so on.
Note: The character *, when used in an expression,
represents any series of characters — it is also called
a wildcard character.
Contain null null (or missing) Is Null Returns records where no value is entered in the
values UnitPrice field.
Contain non-null values Is Not Null Returns records where the value is not missing in
the UnitPrice field.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 12
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.
Do not match a value, such Not #2/2/2010# Returns records of transactions that
as 2/2/2010 took place on a day other than Feb 2,
2010.
Contain values that fall < #2/2/2010# Returns records of transactions that
before a certain date, such took place before Feb 2, 2010.
as 2/2/2010 To view transactions that took place
on or before this date, use the <=
operator instead of the < operator.
Contain values that fall after > #2/2/2010# Returns records of transactions that
a certain date, such as took place after Feb 2, 2010.
2/2/2010 To view transactions that took place
on or after this date, use the >=
operator instead of the > operator.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 13
Criteria for Date/Time fields
To include records that ... Use this criterion Query result
Contain values that fall >#2/2/2010# and <#2/4/2010# Returns records where the
within a date range transactions took place between Feb
2, 2010 and Feb 4, 2010.
You can also use the Between
operator to filter for a range of
values, including the end points. For
example, Between #2/2/2010# and
#2/4/2010# is the same as
>=#2/2/2010# and <=#2/4/2010# .
Contain values that fall <#2/2/2010# or >#2/4/2010# Returns records where the
outside a range transactions took place before Feb 2,
2010 or after Feb 4, 2010.
Contain one of two values, #2/2/2010# or #2/3/2010# Returns records of transactions that
such as 2/2/2010 or took place on either Feb 2, 2010 or
2/3/2010 Feb 3, 2010.
Contain one of many values In (#2/1/2010#, #3/1/2010#, Returns records where the
#4/1/2010#) transactions took place on Feb 1,
2010, March 1, 2010, or April 1,
2010.
Contain a date that falls in a DatePart("m", [SalesDate]) = 12 Returns records where the
specific month (irrespective transactions took place in December
of year), such as December of any year.
Contain a date that falls in a DatePart("q", [SalesDate]) = 1 Returns records where the
specific quarter (irrespective transactions took place in the first
of year), such as the first quarter of any year.
quarter
Contain today's date Date() Returns records of transactions that
took place on the current day. If
today's date is 2/2/2010, you see
records where the OrderDate field is
set to Feb 2, 2010.
Contain yesterday's date Date()-1 Returns records of transactions that
took place the day before the current
day. If today's date is 2/2/2010, you
see records for Feb 1, 2010.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 14
Criteria for Date/Time fields
To include records that ... Use this criterion Query result
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 15
Criteria for Date/Time fields
To include records that ... Use this criterion Query result
Contain a date that belongs Year([SalesDate]) = Year(Now()) Returns records for the current
to the current quarter And DatePart("q", Date()) = quarter. If today's date is 2/2/2010,
DatePart("q", Now()) you see records for the first quarter
of 2010.
Contain a date that belongs Year([SalesDate])*4+DatePart("q",[S Returns records for the previous
to the previous quarter alesDate]) = quarter. If today's date is 2/2/2010,
Year(Date())*4+DatePart("q",Date()) you see records for the last quarter of
-1 2009.
Contain a date that belongs Year([SalesDate])*4+DatePart("q",[S Returns records for the next quarter.
to the next quarter alesDate]) = If today's date is 2/2/2010, you see
Year(Date())*4+DatePart("q",Date()) records for the second quarter of
+1 2010.
Contain a date that falls Year([SalesDate]) = Year(Date()) Returns records for the current year.
during the current year If today's date is 2/2/2010, you see
records for the year 2010.
Contain a date that belongs Year([SalesDate]) = Year(Date()) - 1 Returns records of transactions that
to the previous year took place during the previous year.
If today's date is 2/2/2010, you see
records for the year 2009.
Contain a date that belongs Year([SalesDate]) = Year(Date()) + 1 Returns records of transactions with
to next year next year's date. If today's date is
2/2/2010, you see records for the
year 2011.
Contain a date that falls Year([SalesDate]) = Year(Date()) and Returns records of transactions with
between Jan 1 and today Month([SalesDate]) <= dates that fall between Jan 1 of the
(year to date records) Month(Date()) and Day([SalesDate]) current year and today. If today's
<= Day (Date()) date is 2/2/2010, you see records for
the period Jan 1, 2010 to to 2/2/2010.
Contain a date that occurred < Date() Returns records of transactions that
in the past took place before today.
Contain a date that occurrs > Date() Returns records of transactions that
in the future will take place after today.
Filter for null (or missing) Is Null Returns records where the date of
values transaction is missing.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 16
Criteria for Date/Time fields
To include records that ... Use this criterion Query result
Filter for non-null values Is Not Null Returns records where the date of
transaction is known.
Yes/No In the Criteria row, type Yes to include records where the check box is selected. Type No to
include records where the check box is not selected.
Attachment 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 There are two basic 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 look for numbers; that is, >2.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 17
Create a parameter query
1) Create a select query, and then open the query in Design view.
2) In the Criteria row of a field for which you want a parameter applied, type the text that you want the
parameter dialog box to display, surrounded by square brackets, for example:
[What is the last name of the student you are looking for?]
3) When you run the parameter query, the prompt appears in
a dialog box without the square brackets.
4) Repeat step 2 for each parameter you want the query to
collect and apply.
a) To make your parameter more flexible, you can use
wildcard characters as part of the prompt.
Run a parameter query
A parameter query prompts you for a value when you run it. When you supply the value, the parameter query
applies it as a field criterion. Which field it applies the criterion to is specified in the query design. If you do
not supply a value when prompted, the parameter query interprets your input as an empty string.
1) Locate the query in the Navigation Pane.
2) Do one of the following:
a) Double-click the query you want to run.
b) Click the query you want to run, then press ENTER.
3) When the parameter prompt appears, enter a value to apply as a criterion.
Matching part of a field value with a parameter string
You may want a little variability in the way your query applies a parameter. For example, you might want a
query to accept a text string and match that to any part of a field. You can do this by using the Like keyword
in combination with wildcard characters. For example, you might want your query to prompt for a
country/region of origin, but to match whenever the relevant field value contains the parameter string. To do
this:
1) Create a select query, and then open the query in Design view.
2) In the Criteria row of the field for which you want the parameter applied, type
Like "*"&[Type the text that you want to use as a prompt, and then type ]&"*"
Ex.: Like “*”&[Enter the country/region of origin]&“*”
a) When you run the parameter query, the prompt appears in the dialog box without the square brackets,
and without the Like keyword or wildcard characters.
b) When the query accepts the parameter, it matches on values that contain the parameter string. For
example, the parameter string us matches rows where the parameter field has a value of Australia and
rows where the value is USA.
You can also use the Like keyword and wildcard characters to specify that a parameter should match the
beginning or ending of a field value. To match the beginning of a field value, omit the quotation marks, the
wildcard character, and the ampersand (&) that precede the opening square bracket. To match the ending of a
field value, omit the ampersand, the quotation marks, and the wildcard character that follow the closing square
bracket.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 18
Using the Like keyword gives more
flexibility to the prompt.
Using this example, a user could enter
the first initial of the last name, a series
of characters, or a full last name.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 19
To specify the data type for parameters in a query,
use the following procedure:
1) With the query open in Design view, on the
Design tab, in the Show/Hide group, click
Parameters.
2) In the Query Parameters dialog box, in the
Parameter column, type the prompt for each
parameter for which you want to specify the data
type. Make sure that each parameter matches the
prompt that you use in the Criteria row of the
query design grid.
3) In the Data Type column, select the data type for
each parameter.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 20
Add calculations to the query
A well designed database does not store simple calculated values in tables. For example, a table might store a
person's date of birth but not their current age. If you know both today's date and the person's date of birth,
you can always calculate their current age, so there is no need to store that in the table. Instead, you create a
query that calculates and displays the pertinent value. The calculations are made every time you run the query,
so if the underlying data changes, so do your calculated results. When creating basic calculations, use the
following syntax:
+
-
Label: [FieldName] [FieldName] or number
*
/
5) Change the value in the cell in the Total row to whatever function you require from the drop down list.
6) Click Run to run the query and display the results in Datasheet view.
a) Note that Access appends "FunctionNameOf" to the beginning of the name of the
field that you performed a calculation on.
b) To change the column heading to something more meaningful, switch back to
Design view, and right-click in the Field row of the column in the design grid /
choose Properties / change the “Caption” property to whatever you want displayed
as a column label / close the Properties box.
7) Save the query.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 22
Use the Expression Builder
Expressions are used in many different contexts in Access, and are similar to formulas in Microsoft Office
Excel. The Expression Builder provides easy access to the names of the fields and controls in your database,
and to many of the built-in functions that are available to you when you write expressions. You can use as
many or as few of these features as you want, depending on your own needs or preferences.
Why use the Expression Builder?
Expressions consist of functions, operators, constants, and identifiers (for example, the names of fields, tables,
forms, and queries). The Expression Builder enables you to easily look up and insert these components, and
thereby enter expressions more quickly and accurately. In addition, the Expression Builder helps you
determine which components are appropriate for the context in which you are typing the expression.
You can use the Expression Builder to create a new expression, or you can select from prebuilt expressions,
including expressions for displaying page numbers, the current date, and the current date and time.
You can start the Expression Builder from most of the places where you would write expressions manually,
such as the Control Source property of a control, or the Validation Rule property of a table field. As a rule, if
you see the Build button when you click in a property box, or if you see the word expression in a menu,
you can click it to start the Expression Builder.
New tools for entering expressions in Access 2010
Access 2010 includes new tools that help you enter expressions more quickly and accurately. These tools are
available not only in the Expression Builder, but also in the property sheet and most other places where you
can enter expressions.
IntelliSense and Quick Tips
As soon as you begin typing an identifier or function name, IntelliSense displays a drop-down list of possible
values. You can continue typing, or you can double-click the correct value in the list to add it to the
expression. Alternatively, you can use the up- and down-arrow keys to select the value you want, and then
press TAB or ENTER to add it to your expression. For example, if you begin typing the word “Format”, the
IntelliSense list displays all the functions that begin with “Format”.
Tip: To hide the IntelliSense drop-down list, press ESC. To show it again, press CTRL+SPACEBAR.
While the IntelliSense list is displayed, a short description, or Quick Tip, appears to the right of the currently
selected item. The first item in the list is selected by default, but you can select any item in the list to view its
Quick Tip. The Quick Tip can help you determine the purpose of a function, or what kind of control or
property the item is.
IntelliSense displays possible
functions and other identifiers
as you type an expression.
Quick Tips display a short
description of the selected
item.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 23
Quick Info and Help
While you are typing a function in an expression, the Quick Info feature displays the syntax of the function, so
that you know exactly which arguments are required for the function. Optional arguments are enclosed in
square brackets ([]). While the Quick Info is displayed, you can click on the function name to open a Help
topic for more information.
Quick Info displays the syntax of the
function. Click the function name to open
a Help topic about the function.
Optional arguments are enclosed in square
brackets. The argument that you are
currently typing is displayed in bold text.
Instructions and Help link: Get information about the context in which you are entering the expression.
Expression box: Type your expression here, or add expression elements by double-clicking items in the element
lists below.
Note: If you do not see the element lists (items 3, 4, and 5), click More >> to the right of the Expression box.
Expression Elements list: Click an element type to view its categories in the Expression Categories list.
Expression Categories list: Click a category to view its values in the Expression Values list. If there are no values
in the Expression Values list, double-click the category item to add it to the Expression box.
Expression Values list: Double-click a value to add it to the Expression box.
Help and information about the selected expression value: If available, click the link to see a Help article about
the selected expression value.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 24
Expression box
The upper section of the Expression Builder contains a box where you construct your expression. You can
type your expression in the box manually, using IntelliSense and the other tools described in the previous
section. Or, if you want, you can use the three element lists below the box to select and add elements.
Note: If you do not see the three lists in the lower section of the Expression Builder, on the right side of the
Expression Builder dialog box, click More >>.
Expression elements, categories, and values
When expanded, the lower section of the expression builder contains three lists from which you can select
elements for your expression.
The Expression Elements list displays the top-level elements that are available for you to build an
expression, such as database objects, functions, constants, operators, and common expressions. The
contents of this list are different depending on the context you are in. For example, if you are typing an
expression in the Control Source property of a form, the list contains different items than if you are typing
an expression in the Validation Rule property of a table.
The Expression Categories list contains specific elements or categories of elements for the selection that
you make in the Expression Elements list. For example, if you click Built-In Functions in the Expression
Elements list, the Expression Categories list displays function categories.
The Expression Values list displays the values, if any, for the elements and categories that you selected in
the left and middle lists. For example, if you click Built-In Functions in the left list and then click a
function category in the middle list, the Expression Values list displays all the built-in functions for the
selected category.
Add an element to an expression by using the element lists
1) Click an item in the Expression Elements list.
a) For example, to insert a built-in function, expand Functions, and then click Built-in Functions.
b) The middle and right lists change to display the appropriate values
2) If applicable, click a category in the middle list.
a) The right list changes to display the appropriate values.
3) Double-click an item in the right list.
a) For this example, double-click IIf.
Access adds the item to the expression box. In this example, Access adds IIf (<<expression>>,
<<truepart>>, <<falsepart>>).
4) Replace any placeholder text with valid argument values.
a) Placeholder text is indicated by angle brackets (<< >>). In this example, the placeholders are
<<expression>>, <<truepart>>, and <<falsepart>>.
b) In the expression box, click the placeholder, and then either type the argument manually or select an
element from the element lists.
Tip: To view a Help topic that contains more information about the valid arguments for a function,
select the function in the Expression Values list, and then click the link at the bottom of the
Expression Builder.
5) If the expression contains other elements, they may be separated by the placeholder <<Expr>>. You must
replace this placeholder with an operator before the overall expression will be valid.
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 25
Start the Expression Builder from a query
1) Open your query in Design View.
2) Click the cell in the design grid that will contain the expression.
a) For example, click the Criteria cell for the column where you want to supply criteria, or click the
Field cell for the column where you want to create a calculated field.
3) On the Design tab, in the Query Setup group, click Builder.
a) Alternatively, you can right click in the criteria row under the column you are adding the expression to
and choose “Build…”
Handout: Access 2010 Creating Queries Topics came directly from Microsoft Access 2010 Help.
ICT Training, Maxwell School of Syracuse University Page 26