You are on page 1of 28

Retrieving the Data

You Want (Queries)


CC 105
 Using a query makes it easier to view, add,
delete, or change data in your Access database
Module
 Some other reasons for using queries: Find
Overview specific quickly data by filtering on specific
criteria (conditions)
Retrieving Data
Finding
Using
Records
Sorting
the Find
records
features

Filtering Creating
tables Queries
Finding and
Replacing
Data
Retrieving Data
Finding
Records
Using
Sorting
the Find
records • You can use the Find feature to locate
features
records that contain a specific value in a
particular field
Filtering Creating • The Find feature searches the datasheet
tables Queries and moves the record selector to the first
record that matches the value you specify
• Click the Find command button to open the
Find and Replace dialog box
Retrieving Data
Finding and
Replacing Data
Using
Sorting
the Find
records • Finding and Replacing Data
features
• You can use the Replace feature to replace
values that are found with a new value you
Filtering Creating specify
tables Queries • Type the string or value you want to find in
the Find What text box
• Type the string or value with which you want
to replace the found value in the Replace
With text box
Retrieving Data Sorting Records
• Sorting Records
Using
the Find
Sorting • By default, records display in ascending order by the
features
records primary key field if one is defined
• To sort a datasheet:
• Click in any record in the column you want to use
Filtering Creating for the sort order
tables Queries • Click either the Ascending or Descending button
• To remove a sort, click the Remove Sort button in the
Ribbon
• When you sort by more than one column, the first
column selected is the secondary sort key; the last
column selected is the primary sort key
• Sorting merely rearranges the data in a datasheet
Filtering
Retrieving Data
Tables
Using
the Find
Sorting
records
• Filtering enables you to isolate specific
features records
• Filters use a set of conditions called criteria
Filtering Creating to test records and determine whether or
tables Queries not they should be displayed
• When a filter is applied, only a subset of the
data displays
• Filtering affects only which records are
displayed in the datasheet; it does not
affect the underlying data
Filtering
Retrieving Data
Tables
Using
Sorting
the Find
records
features Filter by Selection
•Filtering by selection filters records that
Filtering Creating contain or do not contain identical data
tables Queries in a specific field
•To filter by selection, place the cursor in
the column that you want to filter on, click
Selection and select an appropriate
option
Filtering
Retrieving Data
Tables
Using
Sorting
the Find
records
features
Filter by Form
Filtering Creating • Filter by Form allows you to specify
tables Queries multiple criteria
• To filter by form, click the Advanced
button and select Filter by Form to open
a blank Filter by Form window
Creating
Retrieving Data
Queries
Using
Sorting
the Find
records • A query asks a question of a table
features
• A select query is a specialized instruction
which selects and displays specific
Filtering Creating information from a table
tables Queries
• To create a query:
• Specify the table(s) from which the
query will select data
• Specify which table fields will be
included
• Specify how the result set will be
organized for display
Creating a Query Design view includes a field list and a
design grid
Select Query
in Query Use the Show Table dialog box to add
tables to the field list
Design View
Drag fields from the field list into the design
grid
Although you do not work directly with the
design grid when you use the Query Wizard,
you still provide the same basic information:

Creating a Which tables to use


Select Query
with the Which fields to include in the result set
Query Wizard
How the data in the result set will display
To add or delete fields,
drag them onto or off of
the design grid

To rearrange columns,
drag a column to a new
position in the design grid
Modifying
Queries Use the Sort row in the
design grid to sort the
records in the result set

To hide a field in a query,


clear the Show check box
in the design grid
Criteria that you specify for a
query becomes part of the
query design
Adding Query
Criteria
To add criteria, click in the
Criteria row for a field and
enter an expression
Criteria that you specify for a
query becomes part of the
query design
Adding Query
Criteria
To add criteria, click in the
Criteria row for a field and
enter an expression
Op
Interpreted
erat Example
As
or
Arithmetic + Plus [Subtotal] + [SalesTax]
Operators - Minus [Price] – [Discount]
* Multiplied by [UnitPrice] * [Quantity]
/ Divided by [TotalCost] / [Quantity]
\ Integer division [StockQty] \ 12
Mod Modulus [StockQty] Mod 12
^ Power [Quantity] ^ 2
Exampl
Operator Interpreted As
e
= Equal to =“NY”
Comparison <> Not equal to <>”NY”
Operators > Greater than >32.95
< Less than <32.95
>= Greater than or equal to >=100
<= Less than or equal to <=100
 Used to join strings of text together to form one
text string
Concatenation  The most commonly used concatenation
Operators operator is the ampersand (&) symbol
 Ex: [FirstName] & “ “ & [LastName]
Operator Interpreted As Example
And Combines two LastName =
criteria “Anderson” AND
State = “NY”
Logical Or Evaluate each LastName =
criterion “Anderson” OR
Operators State = “NY”
separately
Not Reverses an LastName =
expression’s value “Smith” AND NOT
State = “NY”
Operator Interpreted As Example
Between Between two values Between 12
and 36
In In a set or list of values In (“IL”,”NY”,”WA”)
Special Is Null Field contains a null value Is Null
Operators Is Not Null Field does not contain a Is Not Null
null value
Like Used with wildcards to Like “??”
describe a pattern for
which Access searches
 When you use a logical OR, you specify two or
more criteria
Adding a
 Type an “OR” between criteria expressions if
Logical OR to the values are located in the same field
a Query  Use the Or row in the design grid to evaluate
separate fields
 When you use a logical AND, you specify two
or more criteria
Adding a  Both criteria must be satisfied for a record to
be selected
Logical AND  Type an “AND” between criteria expressions if
to a Query the values are located in the same field
 List multiple criteria for multiple fields on the
same row in the design grid
 You can add fields from multiple tables to a
query
 Access automatically joins the tables in the
design grid if you created relationships
between the tables in the Relationships
Working with window
Joined Tables  If you did not explicitly create relationships,
Access creates join lines if:
 Each table has a field with the same name
and data type
 One of the join fields is a primary key
 You can use queries to calculate values
Performing
Calculations in  You can perform calculations in a query
using functions (called totals)
Queries
 You can perform calculations in a query
by creating calculated fields
 Use calculated fields when you want to
perform calculations that are not available
through the use of functions
 To create a calculated field, type an
Adding expression into an empty field cell in the
Calculated design grid
Fields to a  A calculated field does not become part of
the underlying table
Query
 A calculated field always shows results based
on the most current data in the database
 When you add a calculated field, the first part
of the entry displays as the field name for the
calculated field
 To apply a format to a query field, specify the
format in the field’s Property Sheet

Formatting
Query Fields
 To apply a format to a query field, specify the
format in the field’s Property Sheet

Formatting
Query Fields
 You can add a total row to a query
result set just as you can to a datasheet

Displaying
Totals

You might also like