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…
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Query in Design View Figure 6.1 AC0600 Database window
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Query in Design View Figure 6.2 Query 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.
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Query in Design View Figure 6.3 Adding a field to the query Design grid The mouse pointer changes to a field box when you drag it into the query Design grid.
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Query in Design View Figure 6.4 Dynaset for Student Phone List query
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Modifying a Query Object Figure 6.5 Moving a column in the query Design grid
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Modifying a Query Object Figure 6.6 Dynaset for modified Student Phone List query
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Search Criteria Figure 6.7 Dynaset result for “Business” students
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Search Criteria Figure 6.8 Specifying a conditional statement Field name to evaluate and display Table that the field belongs to Conditional statement for limiting the display of records
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Search Criteria Figure 6.9 Dynaset result for students over the age of 22
© 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, (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 Specifying Search Criteria Table 6.1 Query Operators
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Operator/ Criteria… Description… Between X And YList 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 YList those records that contain either X or Y. Not XList 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 Specifying Search Criteria Table 6.1 Query Operators
© 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
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Adding and Removing Tables Figure 6.10 Relationships window for tables in the AC0600 database
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Adding and Removing Tables Figure 6.11 Modifying the query Design window
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Adding and Removing Fields Figure 6.12 Creating a multi-table query
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Adding and Removing Fields Figure 6.13 Dynaset result for a multi-table query
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Multi-Table Criteria Figure 6.14 Dynaset result for courses having instructor surnames beginning with “K”
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Previewing and Printing the Dynaset Figure 6.15 Print Preview window for a query
© 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…
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Sorting Query Results Figure 6.16 Query Design window with the Students field list
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Sorting Query Results Figure 6.17 Dynaset result sorted by the LastName field
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Sorting Query Results Figure 6.18 Specifying a multi-key sort order Primary sort key Secondary sort key
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Applying Filters to the Dynaset Figure 6.19 Dynaset result filtered for City equals Bellevue
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Applying Filters to the Dynaset Figure 6.20 Viewing 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.
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Multiple Criteria in Different Fields Figure 6.21 Setting up a query
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Multiple Criteria in Different Fields Figure 6.22 Dynaset result for Arts students living in Kirkland who are over 20 years old
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Multiple Criteria in Different Fields Figure 6.23 Entering criterion on separate rows of the query Design grid
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Multiple Criteria in a Single Field Figure 6.24 Dynaset result for students who live in Redmond or Bellevue
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Multiple Criteria in a Single Field Figure 6.25 Using AND and OR operators in the query Design grid
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Specifying Multiple Criteria in a Single Field Figure 6.26 Dynaset result for the Student Custom Query
© 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…
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Calculated Field Figure 6.27 Setting up a query
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Calculated Field Figure 6.28 Dynaset result for student grades in COM200
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Calculated Field Figure 6.29 Dynaset result after scaling student grades upward by six percent
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating a Calculated Field Figure 6.30 Viewing a calculated field in the query Design grid
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Setting Field Properties Figure 6.31 Field Properties dialog box
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Setting Field Properties Figure 6.32 Dynaset result showing formatted values in a calculated field Formatted to display with a fixed number format and three decimal places.
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Using the Expression Builder Figure 6.33 Expression 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.
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Using the Expression Builder Figure 6.34 Concatenati ng field values using the ampersand (&) symbol
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Using the Expression Builder Figure 6.35 Modifying column widths in the query Design grid
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Using the Expression Builder Figure 6.36 Dynaset result for concatenated and calculated fields
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Summary Queries Figure 6.37 Creating 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.
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Summary Queries Figure 6.38 Grouping calculations in a summary query
© 2004 The McGraw-Hill Companies, Inc. All rights reserved Creating Summary Queries Figure 6.39 Performing multiple calculations in a summary query