microsoft access xp (2002) - advanced...

32
Group/Summary Operations Change Join Properties Not Equal Query Parameter Queries Working with Text IIF Queries Expression Builder Backing up Tables Action Queries (Update, Make Table, Delete, & Append) Crosstab Queries Microsoft Access XP (2002) - Advanced Queries

Upload: others

Post on 08-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

Group/Summary Operations

Change Join Properties

Not Equal Query

Parameter Queries

Working with Text

IIF Queries

Expression Builder

Backing up Tables

Action Queries

(Update, Make Table, Delete, & Append)

Crosstab Queries

Microsoft Access XP (2002) -

Advanced Queries

Page 2: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 2 of 32

Table of Contents

GROUP/SUMMARY OPERATIONS ............................................................................................ 3

Using Summary Operators ........................................................................................................... 3

JOIN PROPERTIES ........................................................................................................................ 8

Linking When Data Doesn’t Match ............................................................................................. 8

NOT EQUAL BETWEEN FIELDS .............................................................................................. 11

PARAMETER QUERIES ............................................................................................................. 12

Parameter Query in a Text Field ................................................................................................ 12

Parameter Query Using a Range in a Number Field .................................................................. 12

Parameter Query Using a Range of Dates ................................................................................. 13

MANIPULATING TEXT FIELDS ............................................................................................... 14

Joining Text Fields (Catenation) ............................................................................................... 14

Display Partial Text (Truncation) .............................................................................................. 14

IIF STATEMENTS ........................................................................................................................ 15

IIF Statements using Text .......................................................................................................... 15

Calculations in IIF Statements ................................................................................................... 16

Nested IIF Statements ................................................................................................................ 16

Using ‘OR’ in IIF Statements .................................................................................................... 18

Using ‘AND’ in IIF Statements ................................................................................................. 18

USING THE EXPRESSION BUILDER ....................................................................................... 19

BACKING UP A TABLE ............................................................................................................. 22

BACKING UP A DATABASE FILE ............................................................................................ 22

ACTION QUEIRES....................................................................................................................... 23

Update Query: Raise the Credit Limit of all U.S. Customers by $500 ..................................... 24

Update Query: Replace the Manufacturer “General Foods” with “Bob’s Foods” .................... 25

Make Table Query: Create a Table of Only Those Customers Living in the UK ...................... 26

Delete Query: Delete all UK Customers from Sweets Customers ........................................... 27

Append Query: Copy Records from One Table to Another ...................................................... 28

CROSSTAB QUERIES ................................................................................................................. 29

Page 3: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 3 of 32

GROUP/SUMMARY OPERATIONS

Group/Summary operations allow the user to perform mathematical operations on unfiltered data in a

table or multiple tables, data which meets a specific criteria, or data which has been broken down into

groups. Operators available are sum, count, average, standard deviation, etc. The summary operations

are performed in queries and can include one or linked tables.

Using Summary Operators

Use the “Totals” icon when you wish to perform summary operations. Note that because the

Totals icon affects the way queries normally operate, you should only activate the “Totals” icon

when you wish to perform summary operations.

When the Totals row is active, a summary operator must be placed in the Totals row beneath each

field placed on the grid.

When performing summary operations involving multiple tables, only have the tables necessary to

complete the links present. Having tables present that are not somehow involved in creating the

link can yield erroneous results.

Summary Operators:

Group By: This will place identical data together in groups and then list one representative from each

group in the answer. Note that if “Group By” is used in more than one field then Access

will consider both fields as one when searching for identical data.

Sum: Sums the numbers in the table or group.

Count: Lists how many members are in the table or group.

StDev: Finds the Standard Deviation of the fields in the table or group.

Var: Finds the Variance of the fields in the table or group.

Average: Finds the Average of the fields in the table or group.

Min: Returns the smallest number found in the table or group.

Max: Returns the largest number found in the table or group.

First: Returns the data from the first record in the table or the first record in each group.

Last: Returns the data from the last record in the table or the last record in each group.

Where: Used when employing a criteria and summary operators.

Expression: Used when employing calculations and summary operators in a query.

Using Sum: What is the Total Quantity of Products Sold?

The “Order Detail” table keeps track of the quantities sold.

1. Select the “Tables” tab in the Database Window.

2. Select the “Order Detail” table.

3. From the menu, click on: INSERT - QUERY

4. Select “Design View” and then click on “Ok”.

5. Double click the field Quantity to place it on the grid.

6. Click on the “Totals” icon to perform the summary operation.

7. Click in the Total row for the Quantity field.

8. Change the Total operator to by clicking the down arrow and select: Sum

9. Run the query (write the answer down for later use).

Page 4: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 4 of 32

Grouping & Sum: How Many of Each Candy Have Been Sold?

1. Return to “Design” view.

2. Click & drag Candy ID to the query grid.

3. Set Candy ID’s Total operator to: Group by

4. Run the query .

You should see each Candy ID listed only once and its total quantity sold.

Grouping & Count: How Many Times has each Candy Been Ordered?

1. Return to “Design” view.

2. Add another Quantity field to the query grid.

2. Set the Total for the newly added Quantity field to: Count

3. Run the query .

Grouping & Linking: List the Sales People and the Total Quantity Each has Sold

This involves the following tables: “Order Detail”, “Orders”, & “Sweets Employees”

1. Return to “Design” view.

2. Clear the grid: EDIT - CLEAR GRID

3. Add “Orders” & “Sweets Employees”:

a. Click the icon “Show Table”

b. Double click “Orders”

c. Double click “Sweets Employees”

4. Close the Show Table box.

5. Set up the grid as shown:

6. Sort by Quantity in descending order.

7. Click “Totals” if it’s not active.

8. Set the Total operator for First Name

& Last Name to: Group By

9. Set the Quantity Total to: Sum

10. Run the query .

Grouping and Linking: How Many Orders has Each Salesperson Taken?

1. Return to “Design” view.

2. Change the Total for Quantity to: Count

3. Run the query .

Page 5: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 5 of 32

Grouping with a Criteria: Which Customers have Placed 2 or More Orders?

1. Close the current query.

2. Click on the Tables tab.

3. Select the table: “Orders”

4. From the menu, click on: INSERT - QUERY

5. Select Design View then click “Ok”.

6. Click on the “Show Table” icon.

7. Double click: “Sweets Customers”

8. Close the Show Table window.

9. Set up the query grid as shown.

10. Run the query .

Note that Customer ID must come from the “Orders” table and that we could have used any

field from “Orders” and we would have gotten the same answer. This is because Access is counting

how many items exist in each group. It does not matter what the items are. However, if the fields

being counted contain blanks, you will get an inaccurate count. It is therefore, a good idea to use a

field such as Customer ID or Order ID that you can be fairly certain contains no blanks.

Using Where: What was each Sales Person’s Total Quantity Sold in May of 1989?

1. Return to “Design” view.

2. Use the menu to clear the grid: EDIT - CLEAR GRID

3. Click on the “Show Table” icon.

4. Double click the table: “Order Detail”

5. Close the Show Table window.

6. Make sure the Total row is turned on.

7. Set up the query grid as shown.

8. Run the query .

Note the use of “Where” in

the Total row beneath Order Date and that

Show is not checked. This

is because we are

requesting a single total

quantity for each group over a range of dates.

Page 6: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 6 of 32

Grouping with Calculations: How Much Money has Each Company Spent with You?

This query will use linked tables and a calculation between two tables to arrive at a dollar amount.

1. Return to “Design” view.

2. Use the menu to clear the grid: EDIT - CLEAR GRID

3. Click on the “Show Table” icon.

4. Double click the table: “General Candy Info”

5. Close the Show Table window.

6. Make sure the Total row is activated.

7. Set up the query grid as shown.

The equation is:

Spent: [Cost Per Unit]/[Quantity]

8. Run the query .

To format the numbers with $ signs:

1. Return to “Design” view.

2. Right click the equation for Spent:

3. Select Properties from the list.

4. In the Format box, select: Currency

6. Rerun the query .

Page 7: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 7 of 32

Expression: By Percent, do a Breakdown of the Quantity Sold of Each Candy.

This query requires that you know the total quantity sold. To get the total quantity sold, see the first

query example shown in this handout.

1. From the Database window, click on the Table tab.

2. Select the table: “Order Detail”

3. From the menu, click on: INSERT - QUERY.

4. Select Design View and click on “Ok”.

5. Click on the “Show Table” icon.

6. Double click the table: “General Candy Info”

7. Close the View Table window.

8. Set up the query as shown:

9. Activate the Totals row.

10. Set the Totals row options

11. Right-click the equation.

12. Select Properties.

13. Click in the Format box.

14. Select “Percent”

15. Close Properties .

16. Run the query .

Page 8: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 8 of 32

JOIN PROPERTIES

Linking When Data Doesn’t Match

This example shows what happens in a linked query when both tables do not contain the same data in

the linked fields. Note that linked fields do not always have to contain exactly the same data; the

purpose of this example is to increase your understanding of field links.

Below are two tables. On the left is a table that lists every product we carry. On

the right is a table of only those products sold. (Some are listed more than once

because they have been sold more than once in different orders.) Note that in the

table on the right, we have never sold Candy ID number 2 (Milky Way).

Lists every product we carry. Lists only those products sold.

When the tables are linked through their common field of Candy ID , the query

produces the data below. Note that Candy ID number 2 is not listed (Milky Way).

Milky Way is not listed because although it does exist in one of the tables, its

Candy ID does not exist in both tables. By default, Access will only list records

when the data contained in the linking fields exists in both tables.

Note that this is not a bad thing, if you have not sold Candy 2, you probably would

not want to list it anyway.

Page 9: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 9 of 32

Changing Join Properties: Which Candies Haven’t We Sold?

We would like to know which candies that we carry have never been sold. Or in other words, which

candy ID’s are listed in “General Candy Info” but not listed in “Order Details”? To accomplish

this, we will need to change the properties of the query.

1. From the Database window, click on the Table tab.

2. Select the table: “General Candy Info”

3. From the menu, click on: INSERT - QUERY.

4. Select Design View and click on “Ok”.

5. Click on the “Show Table” icon.

6. Double click the table: Order Detail

7. Close the Show Table window.

8. Set up the query as follows:

9. Double click the line connecting the two tables.

10. Select the option: Include ALL records from

“General Candy Info” and only those

records from “Order Detail” where the

joined fields are equal.

11. Click on “Ok”.

12. Run the query .

Brands that were not sold will have blanks in the Candy ID from the “Order Detail” table.

13. Return to “Design” view.

14. Add the Is Null criteria as shown.

15. Run the query again.

This Candy ID

comes from

“Order Detail”

This Candy ID

comes from

“G.C.I.”

Page 10: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 10 of 32

Changing Join Properties: Which Sweet’s Employees Have Never Taken an Order?

The table “Sweets Employees” contains a list of all of our employees. If they have made any sales,

their Employee ID would exist in the table “Orders”. What we would like to know is, which

employees exist in our employees table but do not exist in our orders table?

1. From the Database window, click on the Table tab.

2. Select the table: “Orders”.

3. From the menu, click on: INSERT - QUERY.

4. Select Design View and click on “Ok”.

5. Click on the “Show Table” icon.

6. Double click the table: “Sweets Employees”

7. Close the Show Table window.

8. Double click the line connecting the two tables.

9. Select the option shown.

10. Click on “Ok”.

11. Construct the query grid as shown.

12. Run the query .

Page 11: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 11 of 32

If the criteria is in the Address field of the “Sweets Customers”

table, then the code refers to the Ship Address from “Orders”.

.

NOT EQUAL BETWEEN FIELDS

This example demonstrates how to find two fields that do not contain matching data in corresponding

records. Note that this is not the same as the examples of joins above. Here, were are not interested in

the linking fields.

Syntax: <>[Table].[Field]

Which Customers have a Billing Address that is Different Than Their Shipping Address?

The “Orders” table contains the shipping addresses and the “Sweets Customers” table contains the

billing addresses.

1. From the Database window, click on the Table tab.

2. Select the table: “Orders”.

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Click on the “Show Table” icon.

6. Double click the table: “Sweets Customers”

7. Close the Show Table window.

8. This query can be constructed in two ways, see the examples below.

9. Run the Query .

If the criteria is in the Ship

Address field of the “Orders”

table, then the code refers to the

Address from “Sweets

Customers”.

Page 12: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 12 of 32

PARAMETER QUERIES

A parameter query is a query which asks the user questions when the query is executed. Parameter

queries are useful when the same fields are being queried but the criteria changes slightly. To create a

parameter query, simply place your question in [ ] where you would normally place the criteria.

Parameter Query in a Text Field

This parameter query will prompt the user to enter the manufacturer they are searching for.

1. From the Database window, click on the Tables tab.

2. Select the table: “General Candy Info”.

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Set up the query as shown.

6. Run the query .

7. Type in a manufacturer ( Nabisco for example)

8. Press “ENTER”

9. Save the query as “Search by Manufacturer”

10. Close the query.

11 In the Database window, click on the Queries tab.

12. Double click the query “Search by Manufacturer”

13. Type in: General Foods and click on “Ok”

Parameter Query Using a Range in a Number Field

This parameter query will prompt the user to enter the minimum cost per unit desired.

1. From the Database window, click on the Tables tab.

2. Select the table: “General Candy Info”

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Set up the query as shown.

6. Run the query .

7. Type in 50 and press “ENTER”

Page 13: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 13 of 32

Parameter Query Using a Range of Dates

We wish to find employees who were born during a certain period. This parameter query asks the user

for a beginning date and an ending date and lists only those records which match the criteria.

1. From the Database window, click on the Tables tab.

2. Select the table: “Sweets Employees”

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Set up the query as shown.

6. Run the query .

7. Type in a beginning date and press “ENTER”.

8. Type in an ending date and press “ENTER”.

Parameter Query: Multiple Criteria

This query requests both a country and a credit limit using the “Sweets Customers” table.

1. Set up the query as shown.

2. Run the query .

Page 14: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 14 of 32

MANIPULATING TEXT FIELDS

Joining Text Fields (Concatenation)

This example demonstrates how to take to separate columns and join them together in one column. We

will perform a query on the “Sweets Employees” table that will join Last Name followed by a

comma, a space, and then First Name .

Use the & symbol to join text fields.

Surround extra characters with quotes “ “.

Field names must be in square braces [ ].

The new column should be given a name by the user followed by a colon.

1. From the Database window, click on the Tables tab.

2. Select the table: “Sweets Employees”

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Set up the query as shown.

The code is: Name: [Last Name]&”, “&[First Name]

6. Run the query .

Display Partial Text (Truncation)

This example will display just the first letter of the first name and follow it with a period, a space, and

then the last name. (i.e. L. Callahan instead of Linda Callahan). We will also sort by Last Name.

To truncate, use the LEFT function.

Syntax: Left([Field Name],Number of letters to display going left to right)

Code: Name: Left([First Name],1)&”. “&[Last Name]

1. From the Database window, click on the Tables tab.

2. Select the table: “Sweets Employees”

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Set up the query as shown.

6. Note that it is sorting by Last Name in Ascending order, but that the Last Name field will

not show.

7. Run the query .

Page 15: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 15 of 32

STATUS: IIF([Cost Per Unit]>50,”Approval Needed”, “OK to Reorder”)

The condition What to do if

the condition is

true.

What to do if

the condition is

false.

Note that you can press

SHIFT + F2 on your

keyboard for more room

to type.

IIF STATEMENTS

IIF Statements in Access are used to evaluate data based upon a predetermined condition and then take

an action depending on what the evaluation found.

Syntax: IIF(Condition,What to do if condition is True, What to do if condition is false)

IIF Statements using Text

This IIF statement will look at the Cost Per Unit in the “General Candy Info” table. If an

item cost over $25, Approval is required to reorder, else it is ok to reorder.

Text within IIF statements must be enclosed by quotes.

1. From the Database window, click on the Tables tab.

2. Select the table: “General Candy Info”

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Set up the query as shown.

6. Run the Query .

In the status column, items costing $50 and under should have OK to Reorder and items costing

over $50 should have Approval needed .

Note that is was not necessary to include the Cost Per Unit column on the qrid.

Page 16: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 16 of 32

Calculations in IIF Statements

Sweets Candies gives a volume discount when a customer orders 1,000 or more of an item. The

discount is 50%.

This query will involve two tables: “General Candy Info” which contains the Cost Per Unit &

“Order Detail” which contains the Quantity ordered.

1. From the Database window, click on the Tables tab.

2. Select the table: “General Candy Info”

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Click on the “Show Tables” icon.

6. Double click the table: “Order Detail”

7. Close the Show Table window.

8. Construct the query as shown.

9. Run the query .

Nested IIF Statements

When there are more than two possible outcomes, nested IIF statements can be used. Nested IIF

statements are simply an IIF statement within an IIF statement.

Exercise: We are giving bonus based on hire date

Employees hired prior to 8/2/88 are given $500.

Employees hired between 8/2/88 and 1/1/91 are given $100.

And employees hired after 1/1/91 are given a free dinner for two.

Cost: IIf([Quantity]>=1000,[Quantity]*0.5,[Quantity])*[Cost Per Unit]

Condition If True If False

The IIF Statement

Page 17: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 17 of 32

1. From the Database window, click on the Tables tab.

2. Select the table: “Sweets Employees”

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Construct the query as shown.

6. Run the Query .

Notes:

Access reads IIF Statements from left to right and stops reading as soon as it finds a match. For

example, 1/1/66 is less than 8/2/88 which would indicate $500; however, 1/1/66 is also less than

1/1/91 which would indicate $100, but because Access stops reading after the True part of

condition 1, it never sees condition 2 and any mention of 1/1/91.

Note that the IIF Statement ends in two closing round brackets. )). The number of opening and

closing brackets must be equal.

Bonus: IIf([Hire Date]<#8/2/88#,"$500",IIf([Hire Date]<#1/1/91#,"$100","Free Dinner"))

Condition 1 If Condition

2 is True

If Condition

1 is False

Condition 2 If Condition

1 is True

If Condition

2 is False

Page 18: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 18 of 32

Using ‘OR’ in IIF Statements

When the “Or” operator is used in IIF statements, data need only meet one of the possible conditions to

validate the true portion of the IIF statement.

Example:

Sweets Candies does business in three countries, USA, Canada, and the UK. They would like a list

that states whether the country is foreign or domestic.

1. From the Database window, click on the Tables tab.

2. Select the table: “Sweets Customers”

3. From the menu, click on: INSERT - QUERY

4. Select “Design View” and click on “Ok” .

5. Construct the query as shown.

6. Run the query .

Using ‘AND’ in IIF Statements

When the “And” operator is used in IIF statements, data must meet all of the possible conditions to

validate the true portion of the IIF statement.

In this example, if a customer is from the USA and their credit limit is over $30,000, they are

considered a “Preferred” customer, else they are a “Normal” customer.

Status: IIf([Country]="Canada" Or [Country]="UK","Foreign","Domestic")

Condition using the OR

operator. i.e. If country equals

Canada or UK…

What to do if

condition is

true.

What to do if

condition is

False.

Status: IIf([Country]="USA" And [Credit Limit]>30000,"Preferred","Normal")

Condition using AND True False

Page 19: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 19 of 32

USING THE EXPRESSION BUILDER

The Expression Builder is a tool that can be used to help you build equations in queries, forms, and

reports. It lists all Access functions, operators, and fields.

Exercise:

To get the dollar amount on an item, we multiply: [Cost Per Unit] x [Quantity] x [Ship Rate]

There are two different ship rates based on weather they are domestic or foreign customers:

Domestic Customers (USA) pay 10%.

Foreign Customers pay 25%.

We will use the Expression Builder to create the following equation. Note that the expression builder

will also place the table names in the equation. This is acceptable.

1. From the Database window, click on the Tables tab.

2. Select the table: “Orders”.

3. From the menu, click on: INSERT - QUERY.

4. Select “Design View” and click on “Ok” .

5. Click on the “Show Tables” icon.

6. Add following tables: “Order Detail”, “General Candy Info”, and “Sweets Customers”

7. Close the Show Tables window.

8. Add the fields shown to the grid:

9. Click in the next available blank column in the field row (to the right of Cost Per Unit ).

Total: IIF([Country]="USA",[COST PER UNIT]*1.10,[COST PER UNIT]*1.25)*[Quantity]

Condition True section.

(Cost with 10%)

False section.

(Cost with 25%)

Page 20: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 20 of 32

10. Click on the “Build” icon:

11. Double click the Functions folder.

12. Click Built-in-Functions.

13. Select the All category.

14. Select IIF in the right column.

15. Click on the “PASTE” button.

16. In the expression window, click on

the <<expr>> to select it.

17. Double click the Tables folder.

18. Click the table: “Sweets Customers”

19. Click the field: Country

20. Click on “PASTE”

21. Place the cursor after: [Country]

22. Click the = operator.

23. Select the <<expr>>

24. Type: “USA” (in quotes).

25. Select <<truepart>>.

26. Click in Tables folder: “General Candy Info”.

27. Select the field: Cost Per Unit

28. Click on “PASTE”

Construct your equations here.

Common operators.

Click add to the

equation.

Click a folder to

view its contents.

Double click folders

with + signs to

expand the folder.

Page 21: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 21 of 32

29. Click after [Cost Per Unit].

30. Click on the multiplication operator: * 31. Select the <<Expr>> code.

32. Type: 1.10

33. Select <<falsepart>>

34. Select the General Candy Info folder again.

35. Select the field Cost Per Unit and click on “PASTE”

36. Place the cursor after [Cost Per Unit]

37. Click on the multiplication operator: * 38. Select the <<Expr>> code.

39. Type: 1.25

40. Click after the closing )

41. Click the multiplication operator: *

42. Click on the Tables folder.

43. Select the table: “Order Detail”

44. Select the field: Quantity

45. Click on “PASTE”

46. Click in front of the equation.

47. Type: Total: 46. Click on “Ok”

47. Press “ENTER” to leave the field.

48. Run the query .

Page 22: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 22 of 32

BACKING UP A TABLE

Making a backup copy of a table is invaluable if you wish to protect your data from accidental

deletions. Note that this only backs up the table and not the entire database file. If you wish to back up

the entire file and all the objects it contains (tables, forms, reports, queries), see below “Backing up a

Database File”.

1. Click on the Tables tab in the main Database window.

2. Select but do not open the table: “General Candy Info”

3. From the menu, click on: EDIT - COPY

4. From the menu, click on: EDIT - PASTE

5. Type in a name for the new table.

6. Select the option: Structure and Data

7. Click on “Ok” .

A copy of General Candy Info is created within the

Database window.

BACKING UP A DATABASE FILE

This uses the Window’s Explorer to backup the entire database file including all tables, forms, reports,

and queries it contains.

1. From the Windows Task Bar, right-mouse click the word “START”.

2. From the pop-up list, select: Explorer

3. Browse to and select the file to be copied.

4. From the menu, click on: EDIT - COPY

5. From the menu, click on: EDIT - PASTE

A copy of the file is created. The copy will be named: Copy of Original File Name

Page 23: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 23 of 32

ACTION QUEIRES

By default, queries are SELECT queries which means they select data but they do not alter it.

ACTION queries are another type of query which do alter data in some way.

It is strongly recommended that before performing an action query that you back up your table.

You can not undo an action query!

There are several types of action queries:

Update Query

Update queries can be used to make global changes to your records based upon a criteria. For example,

you would like to raise the credit limit of those people who live in the USA by 10%.

Delete Query

Delete queries can be used to delete records that meet a specific criteria. For example, you would like

to delete the records of all customers who have not make a purchase in the last five years.

Make Table Query

Make table queries are used to make a new table. The new table can include some or all fields from the

old table(s) and some or all of the records from the old table(s). For example, you wish to make a table

of only those people who have made purchases under $5.

Append Query

Append Queries take data from one table and add it to another. This is useful when you have two

people doing data entry into the same type of table and you now wish to merge the two together.

Page 24: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 24 of 32

Update Query: Raise the Credit Limit of all U.S. Customers by $500

This query takes what already exists in a field and alters it mathematically. Note that to include the old

data in some form in the update, the old field name must be present in the calculation.

1. From the Database window, click on the Tables tab.

2. Select the table: “Sweets Customers”

3. From the menu, select: INSERT - QUERY.

4. Select “Design View”

5. Construct the query as shown.

6. Run the query to verify

that this will only affect people

living in the USA.

Note that if “USA” was not present, $500 would be added to the credit limit of all records.

7. Return to “Design View”

8. From the menu, click on: QUERY - UPDATE QUERY

9. Place the code in the Update To row in the Credit Limit column.

Note that the code is: [Credit Limit]+500. If you would have typed just: +500 or 500 without the

[Credit Limit] in front of it, Access would have replaced all U.S. customer’s credit limit with 500.

10. Run the query .

11. Click on “Yes” if your

message reads as follows.

Note that if you run this query

again, you will add another $500

to the credit limits.

Also note that Access does not

display the results of the action query. To see the results, change the query back to a select query and

run it again.

12. From the menu, click on: INSERT - QUERY

13. Select: Select Query

14. Run the query .

Page 25: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 25 of 32

Update Query:

Replace the Manufacturer “General Foods” with “Bob’s Foods”

This update query takes data in a text field and replaces it with other data.

1. From the Database window, click on the Tables tab.

2. Select the table: “General Candy Info”

3. From the menu, select: INSERT - QUERY.

4. Select “Design View”

5. Construct the query as shown.

6. Run the query to verify that this will only affect General Foods manufactureres.

Note that if “General Foods” was not present, all manufacturers would change to “Bob’s Foods”.

7. Return to “Design View”

8. From the menu, click on: QUERY - UPDATE QUERY

9. Place the code in the Update To row in the Manufacturer column.

Note that it is not necessary to include the old field name in the

code. This is because we are completely replacing old data

rather than altering it as in the last example.

10. Run the Query .

11. Click on “Yes” if your message reads as follows.

Note that Access does not display

the results of the action query. To

see the results, change the query

back to a select query and run it

again. Be sure to remove the criteria

“General Foods” or the query will

return an empty record set. (General

Foods no longer exists.)

12. From the menu, click on: INSERT - QUERY

13. Select: Select Query

14. Run the query .

Page 26: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 26 of 32

Make Table Query:

Create a Table of Only Those Customers Living in the UK

This query will copy only those customers who live in the UK into a new table called “UK Customers”.

Note that you can include any columns desired in the new table but because we will be using this table

in a later exercise, we will place all of the columns in the new table.

1. From the Database window, click on the Tables tab.

2. Select the table: “Sweets Customers”

3. From the menu, select: INSERT - QUERY.

4. Select “Design View”

5. Double click the table name “Sweets Customers” (All fields are selected.)

6. Click & drag any one of the selected fields to the first column of the data grid. (All fields are

placed on the grid.)

7. Move to the Country column and place the letters

UK in the criteria row.

8. Run the query to verify that only UK customers will be

placed into the new table.

9. Return to “Design View”

10. From the menu, click on: QUERY - MAKE TABLE QUERY

11. Fill out the pop-up box as shown.

12. Click on “Ok” .

13. Run the Query .

14. When the information box opens,

click on “Yes” to create the new

table.

To see the new table:

a. Close the query. (Save it if you wish.)

b. Click on the Tables tab in the main Database window.

c. Double click the new table (“UK Customers”) to open it.

Page 27: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 27 of 32

Delete Query: Delete all UK Customers from Sweets Customers

From the “Sweets Customers” table, this query will delete the records of any customer living in the

UK.

1. In the Database window, click on the Tables tab.

2. Select the table: “Sweets Customers”

3. From the menu, select: INSERT - QUERY.

4. Select “Design View”

5. Set up the query grid as shown. Note

that placing Company Name on the

grid was not necessary.

6. Run the query to verify only UK

customers will be deleted.

7. Return to “Design View”

8. From the menu, click on: QUERY - DELETE QUERY

Note that a Delete row is added and the

“where” keyword is placed in the row for you.

9. Run the query .

10. Click “Yes” when you get the warning

about deleting rows.

Note that Access does not display the results of the action query. To see the results, change the query

back to a SELECT query and run it again. Be sure to remove the criteria “UK” or the query will

return an empty record set. (Records for UK customers no longer exist.)

Page 28: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 28 of 32

Append Query: Copy Records from One Table to Another

This query will copy records from the table “UK Customers” to the table “Sweets Customers”.

Append Guidelines

The table being appended to should always be backed up prior to executing the append.

The query is based upon the table where data is being appended (copied) from.

If you get an error message when attempting to append, consider any complications caused by a

keyed table. For example, if you are appending into a table that has a primary key set but you are

not appending any data into keyed field, Access will not allow the append because keyed fields can

not contain blanks. Another problem sometimes arises when data is being appended into the keyed

field. If the data being added duplicates data that is already in the keyed field, then Access will not

allow the append.

When appending from one table to another, if the field names are an exact match, Access will place

data from one table into its corresponding field in the other table. When the field names do not

match, the user must tell Access the corresponding fields.

The field names do not have to be in the same order in both tables.

Not all fields (columns) need to be copied into the table.

Not all records need to be copied into the new table. You can append based upon a criteria.

Appending has no affect on the table being appending from but can dramatically affect the table

being appended to.

1. In the Database window, click on the Tables tab.

2. Select the table: “UK Customers” (Created above in the Make Table Query example.)

3. From the menu, select: INSERT - QUERY.

4. Select “Design View”

5. Double click the table name “UK Customers” (All fields are selected.)

6. Click & drag any one of the selected fields to the first column of the data grid.

(All fields are placed on the grid and will be appended to the Sweets Customers table.)

7. From the menu, click on: QUERY - APPEND QUERY

8. Select “Sweets Customers” as the table you are appending to.

9. Make sure Current Database is selected and click on “Ok” .

Page 29: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 29 of 32

This is the field being appended from.

This is the table being appended from.

This is the field data is being

copied to. Note that if the field

names between the two tables were

not spelled the same, you would

need to select the field name

yourself from a drop down list.

10. Verify that the From and To field names match for each column.

11. Run the query .

12. Click “Yes” when you get the message about the append taking place.

13. Close the query and save if desired.

14. Open the “Sweets Customers” table.

There should be customers listed who do live in the UK.

CROSSTAB QUERIES A crosstab query takes data that is in a list and summarizes it into columns and rows. They are useful

for performing data analysis by category. A crosstab query is not an action query and has no effect on

the table it is based on. To perform a crosstab query, at least 3 different columns are needed. Crosstab

queries are simple to create using the “Crosstab Query Wizard”. Crosstab queries can also be created

manually but that procedure can be somewhat tedious.

In this example, we have a list of movies made between 1972 and 1994. The list contains the name of

the movie, its rating, release date, profit made, production company, and the type of movie it is. We

would like to know the total profit for each year broken down by movie rating.

1. In the main Database window, click on the Tables tab.

2. From the menu, click on: INSERT - QUERY

3. Select Crosstab Query Wizard and click on “Ok” .

4. When asked for the table to base the query on, select the table “MOVIES” and click “NEXT” .

Page 30: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 30 of 32

5. Select Rating for the Row Headings and then click on “NEXT” .

6. Select Year for the Column Headings and then click on “NEXT” .

The Row Headings have been set to Rating and the Column Headings have been set to Years . The data that goes where these two intersect must now be selected.

7. Select Profit as the field to be calculated.

8. Select Sum as the function which is to be performed upon the profit field.

9. Make sure Yes, include row sums is checked.

Page 31: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 31 of 32

10. Click on “NEXT” .

11. Type a name for the query.

12. Click on “FINISH” .

You data is now summarized into columns and rows as shown below. Where Rating and Year intersect is the total profit for that category of movie. For example, the total profit for all G rated

moves in 1972 was $109,890,890.00.

Total of Profit is a special summary column which contains the total profit for all movies

regardless of the year it was made, broken down by rating. For example, the total profit for all G rated

movies was $2,185,869,138.00.

Page 32: Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS

USC - Marshall School of Business AIS – User Support

Access_2002-Adv_Queries.doc April 23, 2003 Page 32 of 32

AUTOMATICALLY DELETE DUPLICATE RECORDS

This procedure will delete all duplicate records from a table leaving only one record with the original

data. The process takes several steps and involves:

Creating a duplicate structure of the table that contains duplicates without any data in it.

Making primary keys of all the fields that contain duplicates

Running an append query from the original table to the new table.

Because keyed tables do not allow duplicate records, Access will not allow any duplicates to be copied

to the new table. In other words, you will get only on instance of the duplicate record.

Step A: Copy the Structure of the Table Containing Duplicates

1. Click on the Tables tab.

2. Right mouse click “Sweets Customers” and select: Copy

3. Click the “Paste” button.

4. Name the table: “Sweets Customers without Duplicates”

5. Select: Structure Only

6. Click “Ok” .

Step B: Assign Primary Keys to Duplicate Fields

1. Open “Sweets Customers without Duplicates” in “Design View” .

2. Assign a primary key to the Company Name field.

3. Save and close the table.

Step C: Append from the Original Table to the New Table

1. Single click the “Sweets Customers” table.

2. From the menu, click: INSERT - QUERY - DESIGN VIEW

3. In the field list box, double click the asterisk (*) to tell Access to include all columns.

4. From the menu, select: QUERY - APPEND QUERY

5. Select “Sweets Customers without

Duplicates” as the table to append to.

6. Click on “Ok” .

7. Run the query .

8. Click “Yes” to append all records.

9. Click “Yes” when access give you an error

message.

If you open “Sweets Customers without

Duplicates” , it now contains records but no duplicates.