59
Examining Examining data using data using Microsoft Microsoft Access Access Queries Queries Using Criteria and Calculation Using Criteria and Calculation SESSION 3.2 This section covers specifying an exact match condition in a query, changing a datasheets appearance, using a comparison operator and “And / Or” logical operators.

Examining data using Microsoft Access Queries Using Criteria and Calculations SESSION 3.2 This section covers specifying an exact match condition in a

Embed Size (px)

Citation preview

Examining data Examining data using Microsoft using Microsoft Access QueriesAccess Queries

Using Criteria and CalculationsUsing Criteria and Calculations

SESSION 3.2

This section covers specifying an exact match condition in a query, changing a datasheets appearance, using a comparison operator and “And / Or” logical operators.

Before we continue!

Let’s stop for a moment to consider a possible hazard that might take place if we are not careful.

Unlike many other database programs, Microsoft Access has two kinds of queries.

The 1st is the Select query,

The second is called an Action query.

In the past, the primary purpose, and in most cases the only purpose of a query was to “Select” specific records for viewing.

In Access, this type of query is referred to as a “Select Query”

Access, on the other hand has a second type of query, called an “Action Query”.

An Action Query is a query that adds, changes, or deletes multiple table records at one time.

We could, for example, create an “Action Query” that will delete selected records.

To cause this action to occur, we would “RUN” the query.

The purpose of a Select query is to “VIEW” the datasheet with just the records selected by the criteria used. To cause this to happen we could also “RUN” the query.

To test an Action query, we will need to “VIEW” it’s datasheet before we actually “RUN” the query.

If we get into the habit of “Running” select queries to “VIEW” the datasheet, when we want to test an “ACTION” query to see if it will return the proper data, we might by accident, “RUN” the query thinking that we will see it’s datasheet.

The “ounce of prevention” to use here, in order to prevent the need for a “pound of cure” would be to use the “VIEW” method whenever we want to see the datasheet instead of “Running” the query.

Use the “RUN” method only when you need to cause an Action query to do the action that it was designed for.

To test an Action query (view the Datasheet) before running it, use the “VIEW” method, and to view the datasheet of a Select query, also use the “VIEW” method.

In this session we will look at defining record selection by using criteria and creating calculated fields in Select queries.

The Criteria Row

Just as you can display selected fields from a table in a query’s datasheet, you can display selected records.

To tell Access which records you want to select, you can specify a condition as part of a query.

A condition is a criterion, or rule, that determines which records are selected.

To define a condition for a field, you place the condition in the field’s Criteria text box in the design grid.

A condition usually consists of an operator, often a comparison operator, and a value.

A comparison operator asks Access to compare the values of a database field to the condition value and to select all the records for which the relationship is true.

For example, the condition

>1000.00

for the InvoiceAmt field selects all records in the Order table having InvoiceAmt field values greater than 1000.00

Operator Meaning

= Equal to (optional, default operator)

Example

=“Hall”

Operator Meaning

< Less than

Example

<#1/1/99#

Operator Meaning

<= Less than or equal to

Example

<=100

Operator Meaning

> Greater than

Example

>100

Operator Meaning

>= Greater than or equal to

Example

>=100

Operator Meaning

< > Not equal to

Example

<>”Hall”

Operator Meaning

Between … And ...

between two values (inclusive)

Example

Between 50 And 100

Operator Meaning

IN () in a list of values

Example

In (“Hall”, “Seeger”)

Operator Meaning

Like matches a pattern that includes wildcards

Example

Like “706*”

These operators will be placed on the criteria row below, in the design grid.

It’s worthwhile to mention here that you can use multiple selection criteria in queries.

In other words, you can place specific criteria in as many fields as you have in the query.

We will see an example of this in today’s exercise.

Another important function that you will use queries for is to create new fields by calculating values.

These calculations can be either numeric or can manipulate text.

An example of manipulating text would be to combine the City, State and Zip fields into one field or to extract the

Area Code from the phone # field and place it in a separate field.

To perform a calculation, you define an expression containing a combination of database fields, constants, and operators.

To perform a calculation in a Query, you add a calculated field to the query. A calculated field is a field that displays the results of an expression. A calculated field appears in a query datasheet but does not exist in a table.

FieldName

Expression

Separator

An Expression in a query is made up of three components:

Calculated Field

When you “VIEW” or “RUN” a query that contains a calculated field, Access evaluates the expression defined by the calculated field and displays the resulting value in the datasheet.

Microsoft Access will calculate an expression in the following order:

1. Parentheses2. Exponents (to power of)3. Multiplication4. Division5. Addition6. Subtraction

You can remember this with the phrase: Please Excuse My Dear Aunt Sally.

1. Parentheses2. Exponents (to power of)3. Multiplication4. Division5. Addition6. Subtraction

Another function of a query is to calculate statistical information, such as totals, counts, and averages.

Aggregate functions in a query perform arithmetic operations on selected records.

Queries that perform this kind of operation are sometimes referred to as Aggregate Queries.

To create an aggregate query you can click on the Aggregate button on the toolbar.

When you create an Aggregate Query a new row will appear in the Design Grid.

You can select the function that you want Access to perform by clicking on the selection arrow in the Totals row in the column that you want the function performed in. A list of available functions will appear.

An alternative method of creating an aggregate query is to use “View” from the Menu Bar and clicking on “Totals”

Finally in today’s session we will look at Lookup fields.

Lookup Fields are created by changing the data type in the table to “Lookup”.

This can be done in two ways:1) Manually and 2) with a wizard.

A lookup field provides the user when entering data with a selection box to choose the data that he or she wants to enter into a field.

The selection box will use either another table that contains the appropriate data or you can enter the data into the properties of the field in a value list.

To manually create a lookup field, first select the field and then click on the lookup tab in the field properties section.

Select Combo Box

Select Table/Query or Value List to enter your own values.

If you select Table/Query above select the table or query name to use here.

If you select value list, type the values in here.

Select the column in the table or query or value list that contains the data you want inserted into your field when you make your selection.

Tell Access how many columns you want to appear in your selection list when you activate the lookup box.

Tell Access whether or not you want column heads to appear when you activate your lookup box. (Yes or No)

Tell Access how wide each column will be in your lookup box.

Tell Access how many rows will appear in your lookup box. If there are more rows in the source of data a scroll bar will appear.

Tell Access how wide the lookup box will be. This is generally the total of the column widths above.

And finally, indicate that you want the user to either be able to type anything in this field or just what appears in the list.

And finally, indicate that you want the user to either be able to type anything in this field or just what appears in the list.

Page AC 3.37

Quick Check Review

Quick Check Quick Check ReviewReview

Let’s take a fewmoments to break upinto discussion groups.Each group will discussthe quick check questions on page AC 3.37 in your books. We will then review the answers at the end of the discussion.

Quick Check Review Quick Check Review Session 3.2Session 3.2

1) A(n) ________ is a criterion, or rule, that determines which records are selected for a query datasheet.

Quick Check Review Quick Check Review Session 3.2Session 3.2

2) In the design grid, where do you place the conditions for two different fields when you use the And logical operator? The Or logical operator?

Quick Check Review Quick Check Review Session 3.2Session 3.2

3) To perform a calculation in a query, you define a(n) ___________ containing a combination of database fields, constants, and operators.

Quick Check Review Quick Check Review Session 3.2Session 3.2

4) How does a calculated field differ from a table field?

Quick Check Review Quick Check Review Session 3.2Session 3.2

5) What is an aggregate function?

Quick Check Review Quick Check Review Session 3.2Session 3.2

6) The ________ operator divides selected records into groups based on the values in a field.

And Finally!

As we saw in the last session,Barbara Hennessey and heroffice staff need to track information about all theorders for which bills have been paid. For Barbara’s request you will need to create a query that will display only those records in the Order Table with the value of 1/15/99 in the BillingDate field.

Both Barbara and Leonardare beginning to see thevalue of what this programhas and now have begun torequest much more information.

As a result, the staff at Valle Coffee have once again requested your assistance,

At a recent company meeting, Leonard Valle and other Valle Coffee employees discussed theimportance of regularly monitoring the businessactivity of the company’s restaurant customers. For example, Kim Carpenter and her marketing staff track customer activity to develop new strategies for promoting Valle Coffee Products.

Barbara Hennessey and heroffice staff need to trackinformation about all theorders for which bills weresent out on a specific date so that they can determine better the bills have been paid.

In addition, Leonard isinterested in analyzingthe payment history ofrestaurant customers todetermine which customerspay their invoices in a timely manner, which customers have higher invoice amounts, and so on.

All of these informationalneeds can be satisfied byqueries that retrieveinformation from theRestaurant database.

You have been requested to help the people at Valle Coffee with creating queries that will answer their questions.

Your assignment today is to do the class exercises starting on Page AC 3.19 through page AC 3.37.AndPage AC 5.04 through page AC 5.08