You are on page 1of 86

Access Tutorial 5 Creating Advanced Queries and Enhancing Table Design

COMPREHENSIVE

Objectives

XP

Review table and object naming standards Use the Like, In, Not, and & operators in queries Filter data using an AutoFilter Use the IIf function to assign a conditional value to a calculated field in a query Create a parameter query

New Perspectives on Microsoft Office Access 2007

Objectives
Use query wizards to create a crosstab query, a find duplicates query, and a find unmatched query Create a top values query Modify table designs using lookup fields, input masks, and data validation rules Identify object dependencies Review a Memo fields properties Designate a trusted folder
New Perspectives on Microsoft Office Access 2007

XP

Reviewing the Panorama Database


Figure 5-1 See tables, queries, forms, and reports as database objects on the Navigation Pane

XP

New Perspectives on Microsoft Office Access 2007

Reviewing the Panorama Database Figure 5-1

XP

New Perspectives on Microsoft Office Access 2007

Review the Field Naming Standards in the tblContract Table

XP

In the Tables group on the Navigation Pane, double-click the tblContract to open that table in Datasheet view. Notice that each column heading name contains spaces. In the Views group of the Home tab, click the View button and display the table in Design view. Note that none of the field names has spaces. These spaces were eliminated in the case that Belmont Landscapes needs to upscale to SQL Server or Oracle, which do not permit spaces in object and field names.

New Perspectives on Microsoft Office Access 2007

Panorama Database Field Names and Caption Property Settings Figure 5-2

XP

New Perspectives on Microsoft Office Access 2007

Using a Pattern Match in a Query

XP

A pattern match selects records with a value for the designated field that matches the pattern of the simple condition value The Like comparison operator selects records by matching field values to a specific pattern that includes one or more of these wildcard characters: asterisk (*), question mark (?), and number symbol (#) Figure 5-4
New Perspectives on Microsoft Office Access 2007 8

Using a Pattern Match in a Query Figure 5-4 XP

New Perspectives on Microsoft Office Access 2007

Using a Pattern Match in a Query

XP

In the Results group on the Query Tools Design tab, click the Run button. The query displays 24 records with the area code 616 in the Phone field, as shown in Figure 5-5. Note that the hyphens have been removed from the Phone field vales. We will modify the Phone field later in this tutorial. Close the qry616AreaCode query.

New Perspectives on Microsoft Office Access 2007

10

Records Displayed by qry616AreaCode Query Figure 5-5 XP

New Perspectives on Microsoft Office Access 2007

11

Using a List-of-Values Match in a Query


A list-of-values match selects records whose value for the designated field matches one of two or more simple condition values The In comparison operator lets you define a condition with a list of two or more values for a field

XP

New Perspectives on Microsoft Office Access 2007

12

Creating a Query Using a List-of Values Match

XP

Copy qry616AreaCode by right-clicking it and clicking Copy on the shortcut menu. Name the copied query qryHollandRockfordSaugatuckCustomers in the Query Name text box and then press the Enter key. In the Queries group on the Navigation Pane, right-click qryHollandRockfordSaugatuckCustomers to select it and display the shortcut menu. Click Design View on the shortcut menu to open the query in Design view and then close the Navigation Pane.
New Perspectives on Microsoft Office Access 2007 13

Creating a Query Using a List-of Values Match

XP

Click the Phone Criteria text box, press the F2 key to highlight the entire condition, and then press the Delete key to remove the condition. Right-click the City Criteria text box to open the shortcut menu, click Zoom to open the Zoom dialog box, and then type In(Holland,Rockford,Saugatuck). Click the OK button to close the Zoom dialog box, and then save and run the query. Access displays 13 records with Holland, Rockford, and Saugatuck in the City field. Close the query.
New Perspectives on Microsoft Office Access 2007 14

Record Selection Based on Matching Field Values to a List of Values Figure 5-6

XP

New Perspectives on Microsoft Office Access 2007

15

Completed qryHollandRockfordSaugatuck Customers in Design View

XP

New Perspectives on Microsoft Office Access 2007

16

Completed qryHollandRockfordSaugatuck Customers in Datasheet View

XP

New Perspectives on Microsoft Office Access 2007

17

Using the Not Operator in a Query


The Not logical operator negates a criterion or selects records for which the designated field does not match the criterion

XP

New Perspectives on Microsoft Office Access 2007

18

Creating a Query Using the Not Logical Operator

XP

Right-click qryHollandRockfordSaugatuckCustomers and then click Rename on the shortcut menu. Enter Non between qry and Holland in the querys name. Open the query qryNonHollandRockfordSaugatuckCustomers. Click the City Criteria text box, open the Zoom dialog box, click at the beginning of the expression, type Not, and then press the spacebar, as shown in Figure 5-7. Click the OK button and then save and run the query. Access displays 29 customer records where the City field value is not Holland, Rockford, or Saugatuck. Close the query.
New Perspectives on Microsoft Office Access 2007 19

Creating a Query Using the Not Logical Operator Figure 5-7

XP

New Perspectives on Microsoft Office Access 2007

20

Using an AutoFilter to Filter Data

XP

Open the qrySmallContractsOrWinterSignings query in Design view. Run the query and then click the arrow on the City column heading to display the AutoFilter menu, as shown in Figure 5-8. Click the (Select All) check box to deselect all check boxes, click the East Grand Rapids check box, and then click the Grand Rapids check box, as shown on the second slide. Click the OK button. Access displays four records, as shown in Figure 5-9.
New Perspectives on Microsoft Office Access 2007 21

Using an AutoFilter to Filter Data in a Query Dataset

XP

New Perspectives on Microsoft Office Access 2007

22

Using an AutoFilter to Filter Data in a Query Dataset

XP

New Perspectives on Microsoft Office Access 2007

23

Using an AutoFilter to Filter Data in a Query Dataset

XP

New Perspectives on Microsoft Office Access 2007

24

Assigning a Conditional Value to a Calculated Field

XP

The & (ampersand) operator is a concatenation operator that joins text expressions The IIf (Immediate If) function assigns one value to a calculated field or control if a condition is true, and a second value if the condition is false The IsNull function tests a field value or an expression for a null value; if the field value or expression is null, the result is true; otherwise, the result is false

New Perspectives on Microsoft Office Access 2007

25

Assigning a Conditional Value to a Calculated Field


Click the Create tab on the Ribbon and then in the Other group on the Create tab, click the Query Design button to open the Show Table dialog box on top of the Query Window in Design view. Open the tblCustomer in the Table list box, click the Add button, and then Close the Show Table dialog box. Double-click the title bar of the tblCustomer field list to highlight all records and then drag the highlighted fields to second columns Field text box. Right-click the first columns Field text box and then click Build in the shortcut menu to open the Expression Builder dialog box.
New Perspectives on Microsoft Office Access 2007

XP

26

Assigning a Conditional Value to a Calculated Field

XP

Type Customer: and then press the spacebar. Double-click Functions in the left column, click Built-In Functions in the left column, scroll down the middle column and click Program Flow, click IIF in the right column, and then click the Paste button, as shown in Figure 5-10. Click the first <<Expr>> and then press the Delete key. Click the <<expr>> in the expression box, click Inspection in the middle column, click IsNull in the right column, click the Paste button, click <<varexpr>> in the expression box, and then type Company.
New Perspectives on Microsoft Office Access 2007 27

Assigning a Conditional Value to a Calculated Field

XP

New Perspectives on Microsoft Office Access 2007

28

Assigning a Conditional Value to a Calculated Field

XP

Click <<falsepart>> and then type Company to replace it. Click <<truepart>> and then type LastName & , & FirstName to replace it, as shown in Figure 5-12. Click the right side of the Customer Sort text box and then click Ascending. In the Show/Hide group on the Query Tools Design tab, click the Property Sheet button. The property sheet opens and displays properties for the calculated field, as shown in Figure 5-13.
New Perspectives on Microsoft Office Access 2007 29

Assigning a Conditional Value to a Calculated Field Figure 5-12

XP

New Perspectives on Microsoft Office Access 2007

30

Assigning a Conditional Value to a Calculated Field Figure 5-13

XP

New Perspectives on Microsoft Office Access 2007

31

Assigning a Conditional Value to a Calculated Field


Save the query as qryCustomersByName. Run the query and then resize all columns to best fit, as shown in Figure 5-14. Save the query and then close it.

XP

New Perspectives on Microsoft Office Access 2007

32

Assigning a Conditional Value to a Calculated Field Figure 5-14

XP

New Perspectives on Microsoft Office Access 2007

33

Creating a Parameter Query

XP

A parameter query displays a dialog box that prompts the user to enter one or more criteria values when the query is run Create a select query that includes all fields to appear in the query results. Also choose the sort fields and set the criteria that do not change when you run the query Decide which fields to use as prompts when the query runs. In the Criteria text box for each of these fields, type the prompt you want to appear in a message box when you run the query, and enclose the prompt in brackets

New Perspectives on Microsoft Office Access 2007

34

Creating a Parameter Query

XP

Copy and paste the qryCustomersByName query and name it qryCustomersByNameParameter. Open the qryCustomersByNameParameter in Design view and then close the Navigation Pane. Click the City Criteria text box, type [Enter the city:], and then press the Enter key, as shown in Figure 5-15. Save and run the query. Access displays a dialog box prompting the user for the name of the city, as shown in Figure 5-16.

New Perspectives on Microsoft Office Access 2007

35

Creating a Parameter Query

XP

New Perspectives on Microsoft Office Access 2007

36

Creating a Parameter Query

XP

New Perspectives on Microsoft Office Access 2007

37

Creating a Parameter Query

XP

New Perspectives on Microsoft Office Access 2007

38

Creating a More Flexible Parameter Query

XP

Switch the qryCustomersByName Parameter query to Design view. Click the City Criteria text box and then open the Zoom dialog box. Click to the left end of the expression in the Zoom dialog box, type Like, press the spacebar, go to the right end of the expression, and then type & *, as shown in Figure 5-18. Click the OK button, save your query design changes, and then run the query. Close the query after running it.
New Perspectives on Microsoft Office Access 2007 39

Creating a More Flexible Parameter Query

XP

New Perspectives on Microsoft Office Access 2007

40

Creating a Crosstab Query

XP

A crosstab query performs aggregate function calculations on the values of one database field and displays the results in a spreadsheet format Aggregate Function Definition
Avg Count First Last Max Min StDev Sum Var
New Perspectives on Microsoft Office Access 2007

Average of field values Number of nonnull values First field value Last field value Highest field value Lowest field value Standard deviation of field values Total of the field values Variance of the field values
41

Creating a Crosstab Query


In the Other group on the Create tab, click the Query Wizard button. In the New Query dialog box, click Crosstab Query Wizard, and then click the OK button. In theWizard dialog boxes, select the table or query (qryCustomersAndInvoices) on which to base the crosstab query, and then click the Next button. Move the City field to the Selected Fields list box to select it as the row heading field, and then click the Next button.

XP

New Perspectives on Microsoft Office Access 2007

42

Creating a Crosstab Query

XP

Select the InvoicePaid field as the column heading field, and then click the Next button. Select InvoiceAmt in the Fields list box, click Sum in the Functions list box, make sure that the Yes, include row sums check box is checked, and then click the Next button. Enter qryCustomersAndInvoicesCrosstab as the querys name, make sure that the option button for viewing the query is selected, and then click the Finish button. Access then saves the query and displays the recordset. Resize all the columns in the query recordset to their best fit, and then click the City field value in the first row (Battle Creek). See Figure 5-23 on the next slide.

New Perspectives on Microsoft Office Access 2007

43

Crosstab Query Recordset Figure 5-23

XP

New Perspectives on Microsoft Office Access 2007

44

XP Crosstab Query in Design View Figure 5-24

New Perspectives on Microsoft Office Access 2007

45

XP Changing the Crosstab Querys Column Headings

Click the Home tab on the Ribbon, click View, and then change to Design view, as shown in Figure 5-24. Click the InvoicePaid Field text box (in the second column), and then open the Zoom dialog box. Delete the highlighted expression, and then type IIf(InvoicePaid,Paid,Unpaid) in the Zoom dialog box.

New Perspectives on Microsoft Office Access 2007

46

XP Changing the Crosstab Querys Column Headings

Click the OK button, and then save and run the query. Access displays the crosstab query with Paid and Unpaid, as shown in the next slide. Close the query.

New Perspectives on Microsoft Office Access 2007

47

ChangedCrosstab Querys Column HeadingsXP

New Perspectives on Microsoft Office Access 2007

48

Creating a Find Duplicates Query

XP

A find duplicates query is a select query that finds duplicate records in a table or query. In the Other group on the Create tab, click the Query Wizard button. Click Find Duplicates Query Wizard, and then click the OK button. In the first Find Duplicates dialog box, click the Table: tblContract (if necessary) and then click OK. Move the StartDate field to the Selected Fields list box, and then click the Next button. Click the >> button to move the remaining fields to the Selected fields list box, and then click the Next button.
New Perspectives on Microsoft Office Access 2007 49

Creating a Find Duplicates Query

XP

Enter qryDuplicateContractStartDates as the name of the query in the final Find Duplicates dialog box, be sure the option button for viewing the results is selected, and then click the Finish button. Access saves the query and then displays 10 records for contracts with duplicate start dates, as shown in Figure 5-26. Close the query.

New Perspectives on Microsoft Office Access 2007

50

Creating a Find Duplicates Query

XP

New Perspectives on Microsoft Office Access 2007

51

Creating a Find Unmatched Query

XP

A find unmatched query is a select query that finds all records in a table or query that have no related records in a second table or query. In the Other group on the Create tab, click the Query Wizard button. Click Find Unmatched Query Wizard, and then click the OK button. Click the Queries button in the first Find Unmatched Query Wizard dialog box, scroll down the list of queries and then click the qryCustomersByName query to select it. In the next dialog box, click Table: tblContract in the list box (if necessary) and then click the Next button.
New Perspectives on Microsoft Office Access 2007 52

Creating a Find Unmatched Query

XP

The CustomerID field is the common field between the table and the query. Click CustomerID in the Fields in qryCustomersByName list box, click CustomerID in the Fields tblContract list box, and then click the button to join the two fields. The Matching fields box will then display CustomerID CustomerID to indicate the joining of the two fields. Click the Next button to open the next dialog box. In the next dialog box, click the >> button to move all fields to the Selected Fields list box, and then click the Next button to open the final dialog box.
New Perspectives on Microsoft Office Access 2007 53

Creating a Find Unmatched Query

XP

In the final dialog box, type qryInactiveCustomers as the querys name, be sure the option button for viewing the results is selected, and then click the Finish button. Access saves the query and then displays two records in the query recordset, as shown in Figure 5-28. Resize the columns, save the query again, and then close the query

New Perspectives on Microsoft Office Access 2007

54

Creating a Find Unmatched Query

XP

New Perspectives on Microsoft Office Access 2007

55

Creating a Top Values Query

XP

The Top Values property for a query lets you limit the number of records in the query results Create or open a select query with the necessary fields and sorting and selection criteria Open the Navigation Pane, open the qryLargeContractAmounts query in Datasheet view, and then close the Navigation Pane. Click the Home tab and then switch to Design view. In the Query Setup Group on the Query Tools Design tab, click the Return arrow (with the Screen Tip Top Values, and then click 25%, as shown in Figure 5-29.
New Perspectives on Microsoft Office Access 2007 56

Creating a Top Values Query

XP

New Perspectives on Microsoft Office Access 2007

57

Creating a Top Values Query

XP

Run the query. Access displays six records in the query recordset. These records represent the customers with the highest 25% contract amounts, as shown in Figure 5-30. Because this query is not needed, dont save it. Thus, close the query without saving it.

New Perspectives on Microsoft Office Access 2007

58

Creating a Top Values Query

XP

New Perspectives on Microsoft Office Access 2007

59

Creating a Lookup Field

XP

A lookup field lets the user select a value from a list of possible values You use a Lookup Wizard field in Access to create a lookup field in a table Figures 5-32 and 5-33

New Perspectives on Microsoft Office Access 2007

60

Creating a Lookup Field

XP

Click on Relationships in the Show/Hide group of the Database Tools tab and delete the relationship between the tblContract and tblCustomer. Open tblContract in Design view. Click the right side of the Data Type text box for the CustomerID field and then click Lookup Wizard. Make sure that the option for looking up values in a table or query is selected and then click the Next button to display the next dialog box.

New Perspectives on Microsoft Office Access 2007

61

Creating a Lookup Field

XP

Click the Queries option button in the View section to display the list of queries, scroll to and click Query: qryCustomersByName, and then click the Next button. In the next dialog box, click Customer (if necessary) and move it to the Selected Fields list box, click CustomerID (if necessary) and move it to the Selected Fields list box, and then click the Next button. Click the arrow at the right end of the 1 text box, click Customer, and then click the Next button. Resize the two columns to best fit and then click the Next button.
New Perspectives on Microsoft Office Access 2007 62

Creating a Lookup Field

XP

Click CustomerID in the Available Fields list box and then click the Next button to open the final dialog box. Accept CustomerID as the table name and then click the Finish button. Save the tblContract table and switch to Datasheet view. For ContractNum 3030, click Lilys Boutique in the CustomerID column, and then click the arrow at the right-side to display the list of Customer and CustomerID field values from the qryCustomersByName query.
New Perspectives on Microsoft Office Access 2007 63

Creating a Lookup Field

XP

Scroll up the list box and then click Ingram, Michael to display in the datasheet and to store a CustomerID field value of 11042 in the table, as shown in the next slide. Save and close the tblContract table.

New Perspectives on Microsoft Office Access 2007

64

Created Lookup Field

XP

New Perspectives on Microsoft Office Access 2007

65

Using the Input Mask Wizard

XP

A literal display character is a special character that automatically appears in specific positions of a field value; users dont need to type literal display characters Input mask To use the Input Mask Wizard for the Phone field. Open tblCustomer in Design view, click the Phone Field Name text box to make that row the current row and to display its Field Properties options. Click the Input Mask text box in the Field Properties pane. A Build button appears to the right of the Input Mask text box.
New Perspectives on Microsoft Office Access 2007 66

Using the Input Mask Wizard

XP

Click the Build button at the right side of the Input Mast text box. The first Input Mask Wizard dialog box opens. If necessary, click Phone Number in the Input Mask list box to select it, and then click the Next button. In the next dialog box, accept all wizard defaults and then click the Next button to open the final dialog box. In the final dialog box, make sure that the option without the symbols in the mask is selected and then click the Finish button. Access displays the tblCustomer table in Design view, as shown in Figure 5-36. Change the Phone field size from 14 to 10.
New Perspectives on Microsoft Office Access 2007 67

Using the Input Mask Wizard

XP

New Perspectives on Microsoft Office Access 2007

68

Defining Data Validation Rules

XP

To prevent a user from entering an incorrect value in the Zip field, you can create a field validation rule The Validation Rule property value specifies the valid values that users can enter in a field The Validation Text property value will be displayed in a dialog box if the user enters an invalid value A table validation rule compares one field value in a table record to another field value in the same record to verify their relative accuracy

New Perspectives on Microsoft Office Access 2007

69

Defining Data Validation Rules

XP

Switch to Design view for the tblCustomer table, and then click the Zip Field Name text box to make that row the current row. Since the Zip field is a number field, you will specify a range of valid values in the Validation Rule text box. In the Field Properties pane, click the Validation Rule text box, type >=48000 And <=49999, and then press the Tab key. Type Michigan zip codes must be between 48000 and 49999 in the Validation Text text box, as shown in Figure 5-41
New Perspectives on Microsoft Office Access 2007 70

XP Defining Data Validation Rules Figure 5-41

New Perspectives on Microsoft Office Access 2007

71

Defining Data Validation Rules

XP

Save the table. Switch to Datasheet view, and then scroll the table to the right until the Zip field is visible. Double-click the 49341 in the first rows Zip field text box, type 54321, and then press the Tab key. A dialog box opens containing the message Michigan zip codes must be between 48000 and 49999, which is the Validation Text property setting you created, as shown in the next slide. Click the OK button, and then press the Esc key. The first rows Zip field value again has its original value, 49341. Close the tblCustomer table.
New Perspectives on Microsoft Office Access 2007 72

Defining Data Validation Rules

XP

New Perspectives on Microsoft Office Access 2007

73

Defining Table Validation Rules

XP

Open the tblContract table in Design view, and then, in the Show/Hide group of the Database Tools Design tab, click the Property Sheet button to open the property sheet for the table. In the property sheet, click the Validation Rule text box, type [SigningDate]<[StartDate], and then press the Tab key. Type The signing date must be earlier than the start date in the Validation text box, as shown in Figure 5-42.

New Perspectives on Microsoft Office Access 2007

74

XP Defining Table Validation Rules Figure 5-42

New Perspectives on Microsoft Office Access 2007

75

Defining Table Validation Rules

XP

Close the property sheet, save the table, and switch to Datasheet view. Click the Signing Date column value in the first records, click the date picker icon to the right of the date, click 27 in the calendar control to change the date to 2/27/2010, press the Tab key two times to complete the changes to the record. A dialog box opens containing the message The signing date must be earlier than the start date, as shown in the next slide. Unlike field validation rule violations, which Access detects immediately after you finish your entry and advance to another field, Access detects table validation rule violations when you finish all changes the current record and advance to another record.
New Perspectives on Microsoft Office Access 2007 76

Defining Table Validation Rules

XP

New Perspectives on Microsoft Office Access 2007

77

Defining Table Validation Rules

XP

Click the OK button, and then press the Esc key to undo your change to the Signing Date column value. Close the tblContract table.

New Perspectives on Microsoft Office Access 2007

78

Working with Memo Fields

XP

You use a Memo field for long comments and explanations. Text fields are limited to 255 characters, but Memo fields can hold up to 65,535 characters. Open the tblCustomer table in Datasheet view. Click the Company column selector, press and hold down the Shift key, click the Last Name column selector, and then release the Shift key to select the Company, First Name, and Last Name columns. In the Records group on the Home tab, click the More button, and then click Freeze. The three columns shift to the left and are now the three leftmost columns in the datasheet. Scroll to the right until the entire Comments column can be seen, as shown in Figure 5-43.
New Perspectives on Microsoft Office Access 2007 79

Working with Memo Fields Figure 5-43

XP

New Perspectives on Microsoft Office Access 2007

80

Working with Memo Fields

XP

The Comments field values are partially hidden because the column is not wide enough. Thus, you will view the Comments field value in the Zoom dialog box. Click the Comments text box for the first record, hold down the Shift key, press the F2 key, and then release the Shift key. A Zoom dialog box displays the entire Comments field value for the first record, as shown in the next slide. Click the OK button to close the Zoom dialog box.

New Perspectives on Microsoft Office Access 2007

81

Viewing the Property Settings of the Memo Field

XP

Save the Customer table, switch to Design view, click the Comments Field Name text box to make that row the current row, and then scroll to the bottom of the list of properties in the Field Properties pane. Click the Text Form text box in the Field Properties pane, and then click its arrow. The list of available text formats appears in the list box, as shown in Figure 5-44. The Text Format property was set to Rich Text, which lets your format the datasheet field contents using the options in the Font group of the Home tab. The default Text Format property setting for a Memo field is plain text, which doesnt allow formatting
New Perspectives on Microsoft Office Access 2007 82

Viewing the Property Settings of the Memo Field Figure 5-44

XP

New Perspectives on Microsoft Office Access 2007

83

Viewing the Property Settings of the Memo Field

XP

Click the arrow on the Text Format text box to close the list, scroll down the Field Properties pane to the bottom of the list of properties, and then click the Append Only text box, as shown in the next slide. The Append Only property, which appears at the bottom of the list of properties, tracks the changes that you make to a Memo field. The default setting of No lets you edit the Memo field value in a normal way. Setting this property to Yes also lets you edit the Memo field value in a normal way, but additionally, causes Access to keep a historical record of all versions of the Memo field value. You can view each version of the field value, along with a date and time stamp of when each version change occurred. The review of the Memo field is now finished, thus close the tblCustomer table.
New Perspectives on Microsoft Office Access 2007 84

Viewing the Property Settings of the Memo Field

XP

New Perspectives on Microsoft Office Access 2007

85

Closing the Panaroma Database

XP

Click the Office Button, click on Manage, and then click Compact and Repair Database. Click the Office Button again and Close the Panorama database.

New Perspectives on Microsoft Office Access 2007

86

You might also like