46
© 2004 The McGraw-Hill Companies, Inc. All rights rese rved The Advantage Series Creating Select Queries Chapter 6 Microsoft Office Access 2003

The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

Embed Size (px)

Citation preview

Page 1: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

The Advantage Series

Creating Select Queries

Chapter 6

Microsoft Office Access 2003

Page 2: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1 Using the Query Design View

• The most common query is the Select query– Gathers and arranges data according to the user’s

criteria

• create, and • customize

select queries

Queries can serve as the basis for forms and reports

The Query design view allows you to…

Page 3: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.1 Creating a Query in Design View

Figure 6.1AC0600 Database window

Page 4: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.1 Creating a Query in Design View

Figure 6.2Query Design window

Table Pane

Split bar for sizing the shared space between the Table pane and query Design grid

Query Design Grid

The intersection of a column and row is sometimes called a cell.

Page 5: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.1 Creating a Query in Design View

Figure 6.3Adding a field to the query Design grid

The mouse pointer changes to a field box when you drag it into the query Design grid.

Page 6: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.1 Creating a Query in Design View

Figure 6.4Dynaset for Student Phone List query

Page 7: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.2 Modifying a Query Object

Figure 6.5Moving a column in the query Design grid

Page 8: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.2 Modifying a Query ObjectFigure 6.6Dynaset for modified Student Phone List query

Page 9: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.3 Specifying Search Criteria

Figure 6.7Dynaset result for “Business” students

Page 10: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.3 Specifying Search CriteriaFigure 6.8Specifying a conditional statement Field name

to evaluate and display

Table that the field belongs to

Conditional statement for limiting the display of records

Page 11: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.1.3 Specifying Search Criteria

Figure 6.9Dynaset result for students over the age of 22

Page 12: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

Operator/Criteria…

Description…

>

Greater than; find numbers or dates that are greater than the specified value. For example, >5000 finds all records where the field value is more than 5,000. (Note: Don’t enter commas in numeric queries.)

>=Greater than or equal to; find numbers or dates that are greater than or equal to the specified value.

<

Less than; find numbers or dates that are less than or equal to the specified value. For example, <#05/27/2005# retrieves all records with a date value less than May 27, 2005. (Note: Date values are surrounded by number symbols in conditional statements.)

<=Less than or equal to; find numbers or dates that are less than or equal to the specified value.

6.1.3 Specifying Search Criteria

Table 6.1Query Operators

Page 13: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

Operator/Criteria…

Description…

Between X And Y List those records that contain values between X and Y inclusive. For example, use Between 21 And 65 in an Age field and use Between #01/01/2005# And #12/31/2005# in a Date field.

X Or Y List those records that contain either X or Y.

Not X List those records not containing X.

Like “Sm?th” List those records in which the first letters are “Sm”, the next letter is unknown, and the last two letters are “th”. Example: Smith, Smyth.

Like “Ch*ng” List those records in which the first letters are “Ch”, the middle letters are unknown, and the last letters are “ng”. Example: Chang, Chickering.

Like “*on*” List those records in which the field value contains the letter combination “on”. Example: Conditional, Monday.

Like “*/*/2005” List those records that end with “2005” in the Date field.

6.1.3 Specifying Search Criteria

Table 6.1Query Operators

Page 14: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.2 Creating Multi-Table Queries

• Reduces redundant information• Relationships among tables can be defined in the

query design view• Access can create relationships among tables

automatically if– the tables contain a field of the same name and data

type, and

– one of the fields is a primary key

Access allows tables to be joined so that information can be shared

Page 15: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.2.1 Adding and Removing Tables

Figure 6.10Relationships window for tables in the AC0600 database

Page 16: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.2.1 Adding and Removing Tables

Figure 6.11Modifying the query Design window

Page 17: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.2.2 Adding and Removing Fields

Figure 6.12Creating a multi-table query

Page 18: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.2.2 Adding and Removing Fields

Figure 6.13Dynaset result for a multi-table query

Page 19: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.2.3 Specifying Multi-Table Criteria

Figure 6.14Dynaset result for courses having instructor surnames beginning with “K”

Page 20: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.2.4 Previewing and Printing the Dynaset

Figure 6.15Print Preview window for a query

Page 21: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3 Enhancing a Select Query

• Sort the results of a query• Apply filters to limit the display of records in a

dynaset• Apply Conditional Logic statements to create

more complex search and retrieval operations– Use AND and/or OR to combine criteria statements

– Specify multiple criteria in different fields, or

– Specify multiple criteria in a single field

With Select queries, Access can also…

Page 22: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.1 Sorting Query Results

Figure 6.16Query Design window with the Students field list

Page 23: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.1 Sorting Query Results

Figure 6.17Dynaset result sorted by the LastName field

Page 24: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.1 Sorting Query Results

Figure 6.18Specifying a multi-key sort order

Primary sort key

Secondary sort key

Page 25: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.2 Applying Filters to the Dynaset

Figure 6.19Dynaset result filtered for City equals Bellevue

Page 26: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.2 Applying Filters to the Dynaset

Figure 6.20Viewing filter criteria in the query Design window

The field list object displays fields from another query object.

The Filter By Form criterion is transferred into the Criteria row.

Page 27: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.3 Specifying Multiple Criteria in Different Fields

Figure 6.21Setting up a query

Page 28: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.3 Specifying Multiple Criteria in Different Fields

Figure 6.22Dynaset result for Arts students living in Kirkland who are over 20 years old

Page 29: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.3 Specifying Multiple Criteria in Different Fields

Figure 6.23Entering criterion on separate rows of the query Design grid

Page 30: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.4 Specifying Multiple Criteria in a Single Field

Figure 6.24Dynaset result for students who live in Redmond or Bellevue

Page 31: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.4 Specifying Multiple Criteria in a Single Field

Figure 6.25Using AND and OR operators in the query Design grid

Page 32: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.3.4 Specifying Multiple Criteria in a Single Field

Figure 6.26Dynaset result for the Student Custom Query

Page 33: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4 Performing Calculations

• Computes a result for each record returned in a dynaset

• Computes a single summary result for all records returned in a dynaset

Row-by-Row calculation…

Aggregate calculation…

Page 34: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.1 Creating a Calculated Field

Figure 6.27Setting up a query

Page 35: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.1 Creating a Calculated FieldFigure 6.28Dynaset result for student grades in COM200

Page 36: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.1 Creating a Calculated Field

Figure 6.29Dynaset result after scaling student grades upward by six percent

Page 37: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.1 Creating a Calculated FieldFigure 6.30Viewing a calculated field in the query Design grid

Page 38: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.2 Setting Field Properties

Figure 6.31Field Properties dialog box

Page 39: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.2 Setting Field PropertiesFigure 6.32Dynaset result showing formatted values in a calculated field

Formatted to display with a fixed number format and three decimal places.

Page 40: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.3 Using the Expression Builder

Figure 6.33Expression Builder dialog box

The Expression box displays your selections as they are made.

Select a main folder in the left-hand list box to display a new set of subcategories and element values in the next two list boxes.

Select common operators by clicking these buttons.

Click this button to paste a value from the right-most bottom box into the Expression box above.

Page 41: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.3 Using the Expression Builder

Figure 6.34Concatenating field values using the ampersand (&) symbol

Page 42: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.3 Using the Expression Builder

Figure 6.35Modifying column widths in the query Design grid

Page 43: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.3 Using the Expression Builder

Figure 6.36Dynaset result for concatenated and calculated fields

Page 44: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.4 Creating Summary Queries

Figure 6.37Creating a summary query

When the Totals button is active, the Total row appears in the Design grid.

The Total drop-down list box displays the calculation options.

Page 45: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.4 Creating Summary Queries

Figure 6.38Grouping calculations in a summary query

Page 46: The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Select Queries Chapter 6 Microsoft Office Access 2003

© 2004 The McGraw-Hill Companies, Inc. All rights reserved

6.4.4 Creating Summary QueriesFigure 6.39Performing multiple calculations in a summary query