You are on page 1of 33

HNDA 2105

Database Management
Systems and Data
Analysis

MS Access – Retrieving Data

1
Retrieving Data
• Access provides several methods for finding
exactly the data you want.
• These methods include:
– Using the Find features
– Sorting records
– Filtering tables
– Using queries

2
Finding Records
• You can use the Find feature
to locate records that
contain a specific value in a
particular field
• The Find feature searches
the datasheet 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

3
Finding and Replacing Data
• You can use the Replace
feature to replace values
that are found with a new
value you specify
• 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

4
Using Wildcards
* Finds any number of characters

? Finds a single alphabetic or numeric character

# Finds a single numeric character

[] Finds any character(s) inside the brackets

! Specifies a logical NOT when use within the brackets


- Finds any one of the range of characters inside the brackets

5
Sorting Records
• By default, records display in ascending order by the
primary key field if one is defined
• To sort a datasheet:
– Click in any record in the column you want to use for the sort
order
– 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

6
Filtering Records
• Filtering enables you to isolate specific records
• Filters use a set of conditions called criteria to
test records and determine whether or 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

7
Filter by Selection

• Filtering by selection filters


records that contain or do not
contain identical data 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

8
Filter by Form
• Filter by Form allows
you to specify
multiple criteria
• To filter by form,
click the Advanced
button and select
Filter by Form to
open a blank Filter
by Form window

9
Creating Queries
• A query asks a question of a table
• A select query is a specialized instruction which selects
and displays specific information from a table
• The resulting set of records is called a result set or a
dynaset
• 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
• A select query is stored as a database object
10
Creating a Select Query in Query
Design View
• Query Design view
includes a field list and
a design grid
• Use the Show Table
dialog box to add tables
to the field list
• Drag fields from the
field list into the design
grid

11
Creating a Select Query with the Query
Wizard
• Although you do not work directly with the
design grid when you use the Query Wizard,
you still provide the same basic information:
– Which tables to use
– Which fields to include in the result set
– How the data in the result set will display

12
Modifying Queries
• 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
• 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

13
Adding Query Criteria
• Criteria that you specify for a query becomes
part of the query design
• To add criteria, click in the Criteria row for a
field and enter an expression

14
Arithmetic Operators
Operator Interpreted As Example

+ Plus [Subtotal] + [SalesTax]

- Minus [Price] – [Discount]

* Multiplied by [UnitPrice] * [Quantity]

/ Divided by [TotalCost] / [Quantity]

\ Integer division [StockQty] \ 12

Mod Modulus [StockQty] Mod 12

^ Power [Quantity] ^ 2

15
Comparison Operators
Operator Interpreted As Example

= Equal to =“NY”

<> Not equal to <>”NY”

> Greater than >32.95

< Less than <32.95

>= Greater than or equal to >=100

<= Less than or equal to <=100

16
Concatenation Operators
• Used to join strings of text together to form
one text string
• The most commonly used concatenation
operator is the ampersand (&) symbol
• Ex: [FirstName] & “ “ & [LastName]

17
Logical Operators
Operator Interpreted As Example

And Combines two LastName = “Anderson” AND


criteria State = “NY”
Or Evaluate each LastName = “Anderson” OR
criterion separately State = “NY”

Not Reverses an LastName = “Smith” AND NOT


expression’s value State = “NY”

18
Special Operators
Operator Interpreted As Example
Between Between two values Between 12 and 36
In In a set or list of values In (“IL”,”NY”,”WA”)
Is Null Field contains a null value Is Null
Is Not Field does not contain a null Is Not Null
Null value
Like Used with wildcards to describe a Like “??ot”
pattern for which Access searches

19
Adding a Logical OR to a Query
• When you use a logical OR, you specify two or
more criteria
• Type an “OR” between criteria expressions if
the values are located in the same field
• Use the Or row in the design grid to evaluate
separate fields

20
Adding a Logical AND to a Query
• When you use a logical AND, you specify two
or more criteria
• Both criteria must be satisfied for a record to
be selected
• Type an “AND” between criteria expressions if
the values are located in the same field
• List multiple criteria for multiple fields on the
same row in the design grid

21
Working with Joined Tables
• 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 window
• 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

22
Performing Calculations in Queries
• You can use queries to calculate values
• You can perform calculations in a query using
functions (called totals)
• You can perform calculations in a query by
creating calculated fields

23
Adding Calculated Fields to a Query
• 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 expression into an
empty field cell in the design grid
• A calculated field does not become part of the
underlying table
• 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

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

25
Using the Expression Builder
• The Expression Builder
helps you create
syntactically accurate
expressions
• Drill down through the
components and
double-click to paste
accurate and complete
identifiers into your
expressions
26
Displaying Totals
• You can add a total row to a query result set
just as you can to a datasheet

27
Grouping and Summarizing Query
Data
• To summarize data, use the Total row in the
Query Design grid

28
Aggregate Functions

Group By Defines the groups for which you StDev Returns the standard deviation of
want to perform calculations the values in a field
Sum Returns the total of the values in a Var Returns the variance of the values
field in a field
Avg Returns the average of the values in First Returns the value from the first
a field record in a table or query
Min Returns the smallest value in a field Last Returns the value from the last
Max Returns the largest value in a field record in a table or query
Count Returns the number of values in a Expression Creates a calculated field that
field (not including null records) includes an aggregate function
Where Specifies criteria for a field you
are not using to define groupings

29
Review Questions
1. Which statement accurately compares filtering to sorting?

a. Filtering removes records from tables, whereas sorting simply


hides records.
b. Filtering hides records in a table, whereas sorting changes the
physical order of the records.
c. Filtering isolates a subset of data, whereas sorting simply changes
the display order.
d. Filtering and sorting both change the physical order of the records.

30
Review Questions
2. Amanda wants to add another table to her query. Which of the
following buttons should she click on the Ribbon?
a. Make Table
b. View
c. Show Table
d. Update

3. Phil is having difficulty achieving the sort order he wants for a query.
He wants to sort in ascending order by state, and then in descending
order by city. Which of the following is most likely the problem?
a. The query is read-only and cannot be modified.
b. The primary key for the table is a number field, making it
impossible to sort on a text field.
c. The Format property for the State field is set to Non-indexed.
d. The City field is further left than the State field in the design grid.

31
Review Questions
4. What function does the Zoom box serve?
a. You can use it to build accurate expressions.
b. You can use it to display expressions and make them easier to
read.
c. You can use it to optimize a query and make it run faster.
d. You can use it to quickly add the fields in a field list to the design
grid.

32
Questions?

33

You might also like