Top Banner
Access 2010 Intermediate Skills
59
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PDF 2010 Microsoft Access Intermediate Skills Manual

Access 2010 Intermediate Skills

Page 2: PDF 2010 Microsoft Access Intermediate Skills Manual

(C) 2013, BJC HealthCare (St Louis, Missouri). All Rights Reserved.

Revised June 5, 2013.

Page 3: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

2

TABLE OF CONTENTS

OBJECTIVES ................................................................................................................................................................................. 3 UNDERSTANDING RELATIONSHIPS ...................................................................................................................................... 4

WHAT IS A RELATIONSHIP? ............................................................................................................................................................. 4 CREATING RELATIONSHIPS .............................................................................................................................................................. 7 EDITING TABLE RELATIONSHIPS USING JOIN PROPERTIES .................................................................................................................... 11 EDITING RELATIONSHIPS USING REFERENTIAL INTEGRITY .................................................................................................................... 14

CONTROLLING DATA ENTRY .............................................................................................................................................. 16 THE IMPORTANCE OF FIELD PROPERTIES .......................................................................................................................................... 16 SETTING UP DEFAULT VALUES AND REQUIRED FIELDS ........................................................................................................................ 19 DEVELOPING INPUT MASKS .......................................................................................................................................................... 22 DEFINING VALIDATION RULES........................................................................................................................................................ 27 CONTROLLING DATA ENTRY: LOOKUPS ............................................................................................................................................ 32 PROPERTY SHEETS ....................................................................................................................................................................... 38

USING EXPRESSIONS IN OBJECTS ....................................................................................................................................... 39 WHAT IS AN EXPRESSION? ............................................................................................................................................................ 39 USING EXPRESSIONS IN QUERIES .................................................................................................................................................... 40 USING EXPRESSIONS IN FORMS ...................................................................................................................................................... 43 USING EXPRESSIONS IN REPORTS ................................................................................................................................................... 45

OTHER TYPES OF QUERIES ................................................................................................................................................. 49 QUERY OPTIONS ......................................................................................................................................................................... 49 UPDATE QUERIES ........................................................................................................................................................................ 49 MAKE TABLE QUERIES ................................................................................................................................................................. 51 APPEND QUERIES........................................................................................................................................................................ 53 DELETE QUERIES ......................................................................................................................................................................... 55 PARAMETER QUERIES .................................................................................................................................................................. 57

Page 4: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

3

OBJECTIVES Upon completion of this course, participants will:

Describe relationships, join types, and referential integrity

Create relationships at the query and database levels

Change the output of a query by modifying the join type between the tables

Control data entry by enforcing referential integrity between tables

Describe the impact of the cascade options within referential integrity

Manage the properties of different field types

Set default values for fields

Mark fields as required for data entry

Create an standard and custom input mask to format the entered data

Create a validation rule for a field

Create a custom message for a validation rule

Create a data lookup based on data entered into a list

Create a data lookup that references another table

Create and use expressions in forms, queries, and reports

Create custom calculations

Combine data from separate fields

Create, modify, and run an Update query

Create and run a Make Table query

Create and run an Append query

Create and run a Delete query

Create and run a Parameter query

Page 5: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

4

UNDERSTANDING RELATIONSHIPS

WHAT IS A RELATIONSHIP? Newcomers to the database world have a hard time seeing the difference between an Excel spreadsheet and a database table. They see the table grid structure and recognize that databases allow you better organize and query the data in new ways, but many don’t notice the power of the relationship function that gives this software its true power.

Relationships allow you describe the connections between different database tables. These connections allow you to perform powerful cross-table queries called Joins.

RELATIONSHIP TYPES There are three types of relationships in Access. These are named according to the number of table rows that may be involved in the relationship.

ONE-TO-ONE RELATIONSHIP These relationships occur when each entry in the first table has one, and only one counterpart or match in the second table. A One-to-One relationship is created if both the related fields are primary keys or have unique indexes. An index helps find and sort records faster. One-to-One relationships are rare because most of the time a more efficient way is to just store both fields in the same table.

An example of a One-to-One relationship can be found in our class file between the Employee Data and Employee Salary. One record that includes the employee basic name and address info in the Employee Data table matches one record that contains the employee’s salary info in the Employee Salary table. The information is stored in separate tables for confidentiality reasons.

Page 6: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

5

ONE-TO-MANY RELATIONSHIP These relationships are the most common type of database relationship. They occur when each record in the first table corresponds to one or more records in the second table. A One-to-Many relationship is created if only one of the related fields is a primary key or has a unique index.

An example of a One-to-Many relationship can be found with our class file between the Patient List and the Visits table. A patient name record found in the Patient List table can be matches several times with visit information on the Visits table. This means that the one patient can visit many times!

MANY-TO-MANY RELATIONSHIP These relationships occur when each record in the first table matches one or more records in the second table and each record in the second table also matches one or more records in the first table. Access cannot resolve many-to-many relationships. The application will produce an error immediately, as it cannot determine in which order to match the records.

An example of a Many-to-Many relationship can be found with our class file between the Patient and the Doctor List tables. A patient can see several doctors in the Doctor table and a doctor can see multiple patients in the Patient table. This means there is no easy way to join the two tables because there are too many matches for Access to try to resolve.

The Primary key in the first table matches up with the primary key in the second table.

The Primary key in the first table matches more than one field in the second table.

Doctors can see more than one patient.

Patients can see more than one doctor.

Page 7: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

6

SOLVING A MANY TO MANY RELATIONSHIPS Tables that need to be joined but can’t because of a Many-to-Many relationship can exist in theory but can’t exist physically. There is no way to connect the fields properly. The solution to joining these tables so that data can be retrieved is creating a Junction table. The Junction or Joining table can solve the problem by containing the primary key from each of the tables and combine them to make up a unique primary key. This Junction table then provides the bridge that provides a one-to-many relationship on each side.

An example of a Junction table can be found with our class file between the Patient List, Doctor List and Visits table. The combination of the Doctor ID, Patient ID, and Visit Date field provides a unique record. Now that we joined these tables we can create queries that pull fields from all three tables. Dr last name, patient Last name, gender, visit date and length.

One-to-Many One-to-Many

Two primary keys combine to make a unique record.

Page 8: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

7

CREATING RELATIONSHIPS It is best to create the relationships before beginning to add data to your tables. Consider drawing your relationships on paper before trying to create the links in your database. When you create a relationship between two tables, Access will determine what type of relationship exists between the tables.

QUERY LEVEL VS DATABASE LEVEL RELATIONSHIPS You can create a relationship between tables at the query level or at the database level. When you create a relationship at the query level, the relationship exists for that query only. When you create a relationship at the database level, the relationship extends into all database objects you create including forms, reports, queries, etc.

When you create a relationship, it’s not necessary for the linking fields to share the same names but they must be the same data type. The exception to this is when the primary field has the AutoNumber data type. You can match an AutoNumber field with a Number field as long as the field size property for both of the fields is the same.

Example of Relationship Created in Database Level

Example of Relationship Created in Query Level

Page 9: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

8

Linking Two Tables at the Query Level

Open the Query in Design View. 1. Drag the linking (common) field from the first table to the same data type field on the second table. 2.

Save the Query. 3.

Instructions Comments

1. Open the Access Intermediate Class database.

2. Double-click the Employee Payroll Query. Note how many records return when the query is run.

3. Click the Home tab.

4. Under the Views group, click Design View. Note that no link exists between the Employee Data table and the Employee Salary table.

5. Drag the ID field from the Employee Data table onto the ID field in the Employee Salary table.

Your mouse will turn into a rectangle. Make sure your mouse is positioned on the correct field before releasing it.

6. Run the Query. Note the number of records is greatly reduced. Access is no longer matching each record in the Employee Data table with every record in the Employee Salary table.

RELATIONSHIP TOOLS WITHIN THE RELATIONSHIP WINDOW You can display a window that shows what relationships are set up within your database. This is found under the Database Tools tab. The purpose of the Relationship Window is to show you how the fields between different tables are linked. When you click on the Relationships button, a separate window opens. The Relationship Tools tab also appears. This tab offers ways to edit the details of a selected relationship, adjust the layout, or print a Relationship report. You can also add more tables to the window or adjust which relationships or tables are being viewed.

Opens the Edit Relationships window to control all the options with the joined tables.

Creates a printable map of joined tables within the database.

Clears the Relationship display window.

Adds new tables to the display.

Controls which relationships display in the window.

Closes the Relationship window.

Drag and drop one field on top of the other to join.

Page 10: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

9

Opening the Relationship Window

1. Click the Database Tools tab.

2. Under the Show/Hide group, select the Relationships button.

3. Click Close when finished.

note

Relationships are automatically saved as you click the Create button. If you are asked to save, you are saving the layout (the position of the tables on the screen) and not the relationships you have created.

Linking Two Tables at the Database Level (within the Relationships Window)

Open the database. 1. Click the Database tools tab. 2. Under the Show/Hide group, click the Relationships button. 3.

Under the Relationships group, click Show Table. 4.

Select the table(s) you want to join and click Add. (Use the Ctrl key to select more than one table.) 5. Click Close. 6. Arrange the tables within the Relationships window. 7.

STEPS

NOTE!

STEPS

Page 11: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

10

Drag the linking (common) field from one table to the same data type field in the other table. 8. Verify the information in the Edit Relationships dialog box. 9.

Click Create. 10.

Instructions Comments

1. Click the Database Tools tab.

2. Under the Show/Hide group, click Relationships. Opens the Relationship window.

3. Under the Relationships group, click Show Table. Allows you to choose which tables you want to display in the window.

4. Select Employee Data table and click Add.

5. Select Employee Salary table and click Add.

6. Click Close. Closes the Show Table window.

7. Position your mouse on the border of the Employee Data table.

8. Resize the table to view it in its entirety. Resizing the table makes it easier to see all the field names.

9. Repeat steps 8 & 9 to resize the Employee Salary table.

10. Drag the ID field from the Employee Data table onto the ID field in the Employee Salary table.

Make sure you have the correct fields.

11. Click Create. Joins the tables and displays the join in the Relationship window.

12. Click Close. Closes the Relationship dialog box.

13. Close the Relationships window.

14. Click Yes to save the layout of the tables. Saves the position and size of the displayed tables.

15. Click the Create tab.

16. Under the Query group, click the Query Design button.

17. Add the Employee Data table and Employee Salary table. Note that the link between the tables.

18. Close without saving.

Notice that the field names don’t have to be the same – just the data type.

Page 12: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

11

EDITING TABLE RELATIONSHIPS USING JOIN PROPERTIES When two tables are joined in a relationship or query, the join tells Access how the data in the table is related and the type of join will dictate which records the query performs an action on. It is important to understand the nature of join properties and how join types greatly impact data output.

JOIN TYPES There are three join types that Access supports. The primary table is determined by the way you create the links (from left to right or from right to left).

EQUAL JOIN An equal join, which is the default join type, will return only those records where there is a match between both tables.

LEFT OUTER JOIN A left outer join will return all records of the table designated to be the primary (or left) table, and matching records, if any, from the linked table.

RIGHT OUTER JOIN A right outer join will return all records of the table to be the primary (or right) table, and matching records, if any, from the linked table.

JOIN PROPERTIES You can control the output of your queries by changing the properties of the links between the tables. This is called Join Properties. Most often, you will want to do this at the query level.

note

You can right-click on the join line and choose Join Properties from the short-cut menu.

Patients Visits

NOTE!

Left Outer Equal Right Outer

The default show only matched fields.

Page 13: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

12

Changing the Join Type in a Query

1. Open the query in Design View, if necessary. Double-click on the join line you want to modify.

2. Select a Join Type: 1, 2, or 3.

3. Click OK.

4. Run the query.

Instructions Comments

1. In the Navigation Pane, find the Tables category.

2. Open the Patient List table. There are 50 patient records.

3. Close the table.

4. Open the Visits table. There are 40 visit records.

5. Close the table.

6. In the Navigation Pane, find the Queries category.

7. Open the Patients and Visits query. How many records display?

8. Switch to Design View.

9. Double-click on the join line between the Patient List and Visits tables.

The Join Properties dialog box will appear. Note that the default option is to return only those records where there is a match between the tables.

10. Select the Include All Records from ‘Patient List’ and only those records from ‘Visits’ where the joined fields are equal option.

11. Click OK. Note that the join line now shows which table is the ‘one’ and which table is the ‘many’ in the relationship.

12. Run the query. Note the number of records has increased as all patients are now listed.

13. Save and close the query.

14. Close the database.

STEPS

Page 14: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

13

Changing the Join Type in the Relationships Window

1. Click the Database Tools tab.

2. Under the Relationships group, click the Relationships command.

3. Double-click on the join line you want to modify.

4. Click the Join Type command button.

5. Select the desired join option.

6. Click OK.

7. Close the Relationships window.

note

When you make a change in the Relationships window, the new setting will be applied to all subsequent queries created that use those tables.

NOTE!

STEPS

STEPS

Page 15: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

14

EDITING RELATIONSHIPS USING REFERENTIAL INTEGRITY Referential integrity is a system of rules that Microsoft Access uses to ensure that relationships between records in linked tables are valid. Referential integrity makes sure that you don't accidentally add, delete, or change records that violate the rules you set. For instance, you may set referential integrity on a relationship between tables such as Patients and Visits to ensure that you do not record a visit for a patient that does not exist in the Patients table. Referential integrity is set at the database level using the Relationships window.

Enforcing Referential Integrity

1. Open the Relationships window.

2. Add the tables and create the appropriate links, if necessary.

3. Double-click on the join line for the tables for which you want to establish referential integrity.

4. Click the Enforce Referential Integrity check box.

5. Click the Cascade Update and Delete options, if required.

6. Click OK.

7. Close the Relationships window.

Instructions Comments

1. Open the Employee Data table. Notice how many employees there are.

2. Close the table.

3. Open the Employee Salary table.

4. Add a new record with the following information: ID: 999 DOH: 7/5/2012 Position Lead Nurse Rate: 30 Manager: Collins

Note that the customer 9999 does not exist in the Customers table.

5. Click elsewhere in the table to save the record. The record is allowed to be saved.

6. Delete the record.

7. Close the table.

8. Click the Database Tools tab.

9. Under the Show/Hide group, select the Relationships button.

10. Double-click on the join line between the Employee Data and Employee Salary table.

The Edit Relationships window will open.

STEPS

Page 16: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

15

Instructions Comments

11. Click the Enforce Referential Integrity checkbox.

12. Click OK.

13. Close the Relationships window.

14. Reopen the Employee Salary table.

15. Add a record using the same information in step 4.

16. Click elsewhere in the table to attempt to save the record.

A message stating that the record cannot be saved will appear.

17. Click OK to close the error message. The record cannot be saved as the customer does not exist in the Customers table.

18. Press ESC to cancel the record.

19. Close the table.

20. Close the database.

CASCADE UPDATE/DELETE RELATED FIELDS The Cascade Update option allows you to change a record in the primary table (the one side of the one-to-many) and have Access automatically update all matching records in related tables. The Cascade Delete option allows you to delete a record in the primary table and have Access delete all matching records in the related table. Use caution when activating either option!

Activating the Cascade Update and Cascade Delete Options

1. Open the Relationships window.

2. Add the tables and create the appropriate links, if necessary.

3. Double-click on the join line for the tables for which you want to initiate the Cascade Update or Cascade Delete options.

4. Activate the Enforce Referential Integrity checkbox. The Cascade options will become available.

5. Select the desired option.

6. Click OK.

7. Close the Relationships window.

STEPS

Page 17: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

16

CONTROLLING DATA ENTRY

THE IMPORTANCE OF FIELD PROPERTIES The fields in your database have settings that determine the type of data they can store, how the data is displayed, and what you can do with the data. The field’s data type controls what can be entered into the field. For instance, text data cannot be entered into a field assigned the Number data type. The data type also determines the actions you can perform on a field and how much space the data uses.

Each field also has properties that define its characteristics and behavior. Field properties control the details of information, including character or number length, default values, validation rules, captions, and input masks that control how data gets entered into the field. These properties make it easier to enter and manage data within the field.

CONTROLLING THE SIZE OF TEXT FIELDS You can control the size of a Text field by setting its Field Size property. A Text field can store from 1 to 255 characters, although the default field size is 50. Be sure you select a field size that will accommodate your data needs and apply the field size before any data exists for the field. If you reduce the field size for a field that has data in it, you will truncate data.

Changing the Field Size Property for a Text Field

1. Open the table in Design View.

2. Click in the field to select it. The field properties area at the bottom will update to display the available properties for the data type.

3. Edit the Field Size value.

4. Save the table.

note

A Text field can store from 1 to 255 characters but a memo field can store up to 65,535 characters.

NOTE!

STEPS

Page 18: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

17

note

Making a field larger will have no effect on existing data.

Instructions Comments

1. Open the Gourmet Lunch for Data Properties database.

2. Open the Employees table. There are 20 customer records.

3. Switch to the Design View.

4. Select the State field. Note that the Field Size property is set to 50.

5. In the Field Properties area, click in the Field Size line.

6. Change the value to 2. This will accommodate a two letter abbreviation for each state.

7. Close and save the table. A message will appear to warn you that some data may be lost. This message only appears when the field size is shortened and data exists in the field.

8. Click OK to confirm the new size.

9. Switch to the Datasheet View.

10. Click in the State column for the first record.

11. Try to type more than two characters. Access will ignore any characters past the two allowed for the field.

12. Close the table.

NOTE!

Page 19: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

18

CONTROLLING THE SIZE OF NUMBER AND CURRENCY FIELDS For Number and Currency fields, the Field Size property is particularly important, because it determines the range of values the field can store. For example, a one-bit Number field can store only integers ranging from 0 to 255. The Field Size property also determines how much disk space each Number field value requires.

When you enter numeric data into your table, you need to know how Access treats the information, based on the field’s properties. Number properties should be assigned before data is entered into the field to avoid decimal data being lost.

Number Field Size

Description Decimal info

allowed? Disk space

in bytes

Byte Stores numbers from 0 to 255 No 1

Integer Stores numbers from –32,768 to 32,767 No 2

Long Integer Stores numbers from –2,147,483,648 to 2,147,483,647 (Default)

No 4

Single Stores numbers from –3.402823E38 to –1.401298E–45 for negative values and from 1.401298E–45 to 3.402823E38 for positive values

Yes 8

Double Stores numbers from –1.79769313486231E308 to –4.94065645841247E–324 for negative values and from 4.94065645841247E–324 to 1.79769313486231E308 for positive values

Yes 12

Decimal Stores numbers from –10^28–1 through 10^28–1 Yes 16

Changing the Field Size Property for a Number Field

1. Open the table in Design View.

2. Click in the field to select it. The field properties area at the bottom will update to show the available properties for the data type.

3. From the Field Size drop down list, select the size based on the number of decimal places you need stored.

4. Save the table.

STEPS

Page 20: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

19

Instructions Comments

1. Open the Entrees table.

2. Click in the Cost field for the first record. The current price is $10.00

3. Change the price to 9.75

4. Move off the record. The price changes to $10.00.

5. Switch to the Design View.

6. Click in the Cost field. The field’s data type is Number. The Field Size is set to Long Integer.

7. Change the Field Size property to Single. This setting will allow decimal info to be stored.

8. Change the Format to Currency.

9. Save the table.

10. Switch to the Datasheet View.

11. Change the Cost value in the first record to 9.75.

12. Move off the record. The price remains at $9.75 because decimal information is allowed to be stored in the field.

13. Close the table.

SETTING UP DEFAULT VALUES AND REQUIRED FIELDS There may be times when you find you are typing the same values into a field more often than not. For instance, you might be tracking patient information and most of the patients you are working with live in the state of Missouri. Rather than continue to type the state value in every time, you can set a default value for the field and change the value when there is an exception.

SETTING UP THE DEFAULT VALUE The Default Value property allows you to specify a value that will be automatically entered when you add a new record. You can accept the default value or type a new value over it.

Adding a Default Value for a Text Field

1. Open the table in Design View.

2. Select the appropriate field.

3. In the Field Properties area at the bottom of the screen, click in the Default Value line.

4. Enter a value.

5. Save the table.

note

The default value for the field will display in the new record line at the bottom of the table.

NOTE!

STEPS

Page 21: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

20

Instructions Comments

1. Open the Lunch Orders table.

2. Switch to the Design View.

3. Select the Order Date field.

4. In the Field Properties area, click in the Default Value line.

5. Type =NOW() This means every Order Date field entry will be preloaded with today’s date.

6. Save the table.

7. Switch to the Datasheet View. Note that today’s date is automatically filled in at the bottom of the table within the new record row.

DESIGNATING REQUIRED FIELDS By default, only the primary key field in the table is required for data entry because, by definition, it is not allowed to have a null value or remain blank. However, there are many instances when data is required for other fields. For instance, patient name fields should not be blank. In a table tracking patient information, the primary key is assigned to a field that will be unique to each patient, whether it is a patient’s social security number or another unique identifier. However, it is possible that two patients could have the same name. This is the reason, in fact, that the primary key should not be assigned to the name fields. In your patient information table, even though the name fields are not set as primary, you do want to have as complete a set of data as possible. The Required property allows you to specify that data must be entered into a field.

Set a required field before entering data into the table. If you are applying this setting to a field that contains data, Access will warn you that the field’s data integrity rules have changed. Access will test the new rule against the existing data and will warn you if you have records which violate the rule.

Setting a Field to be Required for Data Entry

1. Open the table in Design View.

2. Select the appropriate field.

3. In the Field Properties area at the bottom of the screen, click in the Required line.

4. Select Yes from the drop-down list.

5. Save the table.

6. Click Yes if presented with a message that data integrity rules have changed. Clicking Yes will cause Access to check each record to see if the rule is valid.

7. Click Yes to continue testing.

STEPS

Page 22: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

21

note

If there is an issue with the data, Access will present another message. At this point, you should cancel the action, and go back and address the issues in your data.

Instructions Comments

1. Open the Lunch Orders table.

2. Switch to the Design View.

3. Select the Main field.

4. In the Field Properties area, click in the Required line.

5. Select Yes from the drop-down list. The field record will not be saved unless this field is filled.

6. Adjust the following fields to be required as well: Side Sweet Drink

7. Save the table.

8. Click Yes to test the changes to the data integrity rules.

It runs a data integrity check.

9. Switch to the Datasheet View.

10. Add a New Order but skip the Main field.

Use the tab key to quickly pass by the field.

11. Click within another record. Because Access attempted to save the record and the Main field was blank, an error message appears.

12. Click OK to close the error message.

13. Press ESC to remove the record.

14. Close the Lunch Orders table.

NOTE!

Page 23: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

22

DEVELOPING INPUT MASKS An input mask is a data entry template for a field. Input masks are often used for data such as social security numbers, zip codes, or telephone numbers. With an input mask you can control the type and number of characters that can be entered into the field. For instance, within an input mask applied to a zip code field, you can specify whether letters are allowed, whether the extended zip code (+4) is required or optional, and whether or not a hyphen is stored with the field. At the same time, you can assign the Text data type to the field and limit the number of characters stored within the database to optimize large sets of data. You can also use literal display characters such as hyphens, underscores, or parentheses to give your users information about what you expect to be entered in the field. Access gives you the ability to choose from a list of commonly used masks or to create the mask from scratch.

THE STRUCTURE OF AN INPUT MASK

00000\-9999;0;_

There are three sections to an input mask structure:

The first section is the series of characters that makes up the mask. Use F1 within the Input Mask property to see a complete set of characters.

The second section is used to define whether literal characters (such as parentheses and hyphens) used within the mask are stored with the value.

The third section is used to identify the placeholder character used to format the field for the data entry user. The default character used is the underscore (_).

USING THE PRE-DEFINED INPUT MASK Common masks such as phone number or zip code formats can be applied quickly using the Input Mask Wizard. The Input Mask Wizard offers a list of pre-defined masks to choose as well as a list of placeholder characters. The Wizard gives you the choice to store any additional symbols such as the dash in a phone number, with the data or not.

Applying a Pre-Defined Input Mask

1. Open the table in Design View.

2. Select the appropriate field.

3. In the Field Properties area, click in the Input Mask line.

4. Click the Build button.

STEPS

Series of characters Literal characters

Placeholder character Each section is separated by a semi-colon (;).

Build button

Page 24: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

23

5. Select a mask from the column on the left.

6. Click Next.

7. Select a different placeholder character to display when the user clicks in the field, if desired.

8. Click Next.

9. Choose to store the literal characters in the mask (hyphens, etc.) in the field or to store the data only.

10. Click Next.

11. Click Finish.

12. Save the table.

Page 25: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

24

note

Make sure your field size is large enough to store any literal characters you choose to store with the data.

Instructions Comments

1. Open the Employees table.

2. Switch to the Design View.

3. Click within the Phone field. Notice its data type is just text.

4. In the Field Properties area, click in the Input Mask line.

Displays the phone field properties.

5. Click the Build button. Opens the Input Mask Wizard.

6. Click the Phone Number mask.

7. Click in the Try It Line to see how the mask would appear to data entry users.

Type a few letters to see how it reacts.

8. Click Next.

9. Click Next to accept the underscore character as the placeholder displayed to the users.

10. Click the ‘With the symbols in the mask’ option. Storing the literal characters make it easier to read the phone number but it also takes up more room within the database file.

11. Click Next.

12. Click Finish. Note the syntax used for the mask.

13. Save the table.

14. Click the Datasheet View. Notice how the phone numbers are now stored.

note

Setting the InputMask property to the word "Password" creates a password-entry control. Any character typed in the control is stored as the character but is displayed as an asterisk (*). You use the Password input mask to

prevent displaying the typed characters on the screen.

NOTE!

NOTE!

Page 26: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

25

CREATING A CUSTOM INPUT MASK When you create a custom input mask, you need to use special characters to represent what type of data, such as a number or character, you want to display. When you type data in a field for which you've defined an input mask, the data is always entered in Overtype mode. If you use the BACKSPACE key to delete a character, the character is replaced by a blank space

You can define an input mask by using the following characters.

Character Description

Digit (0 to 9, entry required, plus [+] and minus [-] signs not allowed)

9 Digit or space (entry not required, plus and minus signs not allowed)

# Digit or space (entry not required; spaces are displayed as blanks while in Edit mode, but

blanks are removed when data is saved; plus and minus signs allowed)

L Letter (A to Z, entry required)

? Letter (A to Z, entry optional)

A Letter or digit (entry required)

a Letter or digit (entry optional)

& Any character or a space (entry required)

C Any character or a space (entry optional)

. , : ; - / Decimal placeholder and thousand, date, and time separators.

< Causes all characters to be converted to lowercase.

> Causes all characters to be converted to uppercase.

! Causes the input mask to display from right to left, rather than from left to right. You can

include the exclamation point anywhere in the input mask.

\ Causes the character that follows to be displayed as the literal character (for example, \A is

displayed as just A).

Input mask Sample values

The following table

shows some useful

input masks and the

type of values you

can enter in them.

(000) 000-0000 (206) 555-0248

(999) 999-9999 (206) 555-0248

( ) 555-0248

(000) AAA-AAAA (206) 555-TELE

#999 -20 or 2000

>L????L?000L0 GREENGR339M3

MAY R 452B7

>L0L 0L0 T2F 8M4

00000-9999 98115- or 98115-3007

>L<?????????????? Maria

SSN 000-00-0000 SSN 555-55-5555

>LL00000-0000 DB51392-0493

Page 27: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

26

Creating an Input Mask from Scratch

1. Open the table in Design View.

2. Select the appropriate field.

3. In the Field Properties area, click in the Input Mask line.

4. Type the appropriate syntax for each section of the mask. Separate the sections with a semi-colon.

Instructions Comments

1. In the Employee tables Datasheet view, click within the first record’s E ID field.

2. Type 003456 in the E ID field. Accepts any letter or number combination.

3. Press Tab. The revision is allowed.

4. Click Undo to undo the ID change. The original order number is restored.

5. Switch to the Design View. E ID field is the first field.

6. In the Field Properties area, click in the Input Mask line.

7. Press F1. Context-sensitive help on the Input Masks will display.

8. Close the Help screen.

9. Type >LLL” - “000000000;1;X in the Input Mask line.

The >LLL segment requires 3 upper case letters then a dash and the employee number.

10. Save the table.

11. Switch to the Datasheet View.

12. Add a new record. Note the input mask displays X’s as placeholders.

13. Type 00. Will not accept numbers.

14. Type BJC. Will not accept a partial set of information.

15. Type BJC 009873457.

16. Press Tab to move to the next field. Accepts the data set.

17. Press Esc to cancel the record.

? : How could you change the mask to automatically include BJC without typing it?

STEPS

Page 28: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

27

DEFINING VALIDATION RULES Validation rules allow you to restrict or control the data that can be entered into a field. Access will verify that the data entered into the field is valid as the user leaves the record. If the data entered does not comply with the rule, a message will appear and the data cannot be saved. When determining a refill quantity for a prescription, you might develop a validation rule that ensures that the quantity entered for the refill does not exceed the original quantity.

CREATING VALIDATION RULES IN FIELD PROPERTIES Validation rules are created in the Design View’s Field Properties section for a selected field. You can use standard operators such as >,<,=, and, or, etc. Validation rules can be entered into the Validation Rules Property or you can use the Expression Builder if you need to add formulas within the rule.

Creating a Validation Rule by Typing

1. Open the table in Design View.

2. Click in the desired field.

3. In the Field Properties area, click in the Validation Rule line.

4. Type the rule you want use. (For example: >100 And <1000 or “F” OR “M”)

5. Save the table.

Instructions Comments

1. Open the Employees table.

2. Switch to the Design View.

3. Select the Gender field. Notice it already has data.

4. Under the Field Properties, click within the Validation Rule row.

Type the rule in the cell.

5. Type “F” Or “M” Don’t forget to place quotes around the text.

6. Switch to Datasheet View and save the changes.

7. Click Yes to test the existing data. Allows you to test to see if the existing data fits the new rule.

8. Click Yes to continue to test and keep the rule.

9. Click within the first records Gender field.

10. Type Female and press TAB. Access displays a message that the value is prohibited because of the validation rule.

11. Retype it as F and press TAB. Accepts the data.

12. Close the table.

STEPS

Page 29: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

28

UNDERSTANDING THE STRUCTURE OF THE EXPRESSION BUILDER You can use the Expression Builder to create a validation rule for a field. The Expression Builder is also used to help build formulas, which are called expressions in Access. The Expression Builder allows you to write expressions in forms and reports, tables, and in queries. It builds a list of fields and controls in your database and displays the built-in functions. You can create an expression by typing into the Expression box or you can select from the list of existing expressions. Examples of prebuilt functions include page numbers and the current date.

The Expression Builder contains the following elements:

The Expression Box Operator buttons Expression elements

Folders

Categories

Values and Functions

Categories

Values and functions

Definition of the selected value

Expression Box

Folders

Page 30: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

29

Creating a Validation Rule

1. Open the table in Design View.

2. Click in the desired field.

3. In the Field Properties area, click in the Validation Rule line.

4. Click the Build button.

5. Enter the appropriate validation expression in the Expression Builder.

6. Click OK.

7. Save the table.

EXPLAINING INVALID DATA The default message that appears when a user enters invalid data can be confusing. You may want to consider replacing this message with one that explains to the user what type of data is valid for the field.

Here’s an example of an error message generated by Access:

STEPS

Click to see a list of built-in functions

Page 31: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

30

Instructions Comments

1. Open the Lunch Orders table. You will create a validation rule that ensures that the order date entered today or in the future.

2. Switch to the Design View.

3. Click in the OrderDate field.

4. In the Field Properties area, click in the Validation Rule line.

The Build button will appear.

5. Click the Build button. The Expression Builder window will open.

6. In the Expression Box, type >=.

7. Double-click on the Functions folder in the first column of the Expression Elements area.

The Functions folder will expand.

8. Select the Built-In Functions folder. A list of function categories will appear in the middle column.

9. Select the Date/Time category. A list of date/time functions will appear in the third section.

10. Double-click on the Now function. The Now function returns the current date and time.

11. Click OK. The Expression Builder will close.

12. Save the table.

13. Click Yes to perform the data integrity check.

14. Switch to Datasheet View.

15. Create a new record but use a date in the past.

16. Press Tab to complete the record. A message will appear informing you that the entry into the OrderDate field is not valid.

17. Click OK. The message closes.

18. Press ESC twice to cancel the new record.

Page 32: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

31

Adding Explanatory Text to a Validation Rule Message

1. Open the table in Design View.

2. Click in the desired field.

3. In the Field Properties area, click in the Validation Text line.

4. Enter meaningful text for the error.

5. Save the table.

Instructions Comments

1. Switch to the Design View.

2. Select the Order Date field.

3. In the Field properties area, click in the Validation Text line.

4. Type “Please enter an order date today or in the future.”

This message will appear if the data entered is incorrect.

5. Save the table.

6. Switch to the Datasheet View.

7. Create a new record but use a date in the past.

8. Press Tab to complete the record. The custom error message with appear.

9. Click OK to close the message.

10. Change the Order Date to a future date.

11. Press Tab to complete the record. The validation rule is satisfied so the record is saved.

12. Close the table.

13. Close the database.

STEPS

Page 33: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

32

CONTROLLING DATA ENTRY: LOOKUPS When you provide users a list of choices for a field entry, you eliminate the possibility that incorrect data can be entered. In Access this can be accomplished by developing a lookup table.

DEFINING A LOOKUP: ENTERING YOUR OWN LOOKUP VALUES The lookup list can either be built into the field properties as a static list or the list can provide current data from a table. When your list is static and rarely changes, adding the list to the field is appropriate.

Storing the Lookup within the Table

1. Open the table in Design View.

2. Click in the desired field.

3. Change the field type to Lookup Wizard.

4. Click the “I will type in the values that I want.” option.

5. Click Next.

6. Type the number of columns you want in your lookup list.

7. Click in the first row in Col1.

8. Type the information you want in the list.

9. Press Tab to add additional items in the list. Repeat as needed.

STEPS

Page 34: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

33

10. Click Next.

11. If you are listing more than one column, specify which column stores the identifier.

12. Click Next.

13. Type a label for the column. This will show in the datasheet view.

14. Click Finish.

15. Save the table.

Page 35: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

34

Instructions Comments

1. Open the Employees table.

2. Switch to the Design View.

3. Add a new text field named HSO. In this example, you will create and attach a list of vendors to the Inventory table.

4. Change the data type to Lookup Wizard. The Wizard will launch.

5. Select the “I will type in the values I want” option. Use this option when your list is static.

6. Click Next.

7. Enter a 1 for the number of columns.

8. Press Tab. The Wizard will expand the number of columns displayed.

9. Enter the following information in the columns using TAB after each one: SLCH BJH MBMC CH BJHWC ALTON BOONE PROGRESS WEST

Expand the column to view all the text entered.

10. Click Next.

11. Accept HSO as the name of the field.

12. Click Finish.

13. Save the table.

14. Switch to the Datasheet View.

15. In the first record, click in the HSO field. Note a drop-down list appears.

16. From the drop-down list, select BJH.

17. Close the table.

Page 36: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

35

DEFINING A LOOKUP: USING A CURRENT TABLE’S VALUES When your data is subject to change, you may want to consider keeping your lookup information in its own table. The table is easier to find and edit than the lookup stored within the field. The table must exist before you can connect it. In essence, this action will create a relationship between the two tables.

Pointing the Lookup to an Existing Table

1. Open the table in Design View.

2. Click in the desired field.

3. Change the field type to Lookup Wizard.

4. Click the “I want the lookup column to lookup…” option.

5. Click Next.

6. Select the appropriate table.

7. Click Next.

8. Add the appropriate fields from the Available Fields list to the Selected Fields list using the arrow button.

9. Click Next.

STEPS

Page 37: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

36

10. Adjust the width of the column to display the data, if necessary.

11. Turn on or off the viewing of the key column, as desired.

12. Click Next.

13. Enter a label for your column.

14. Click Finish.

15. Save the table.

DELETING THE RELATIONSHIP TO BUILD A LOOKUP If the field that you want to create a lookup for is in a table that is joined to another table, you will need to delete the relationship, then you can create the lookup. You can break the relationship in the Relationship window.

Deleting a Relationship between Two Tables

1. Click the Database tools tab.

2. Under the Relationships group, select Relationships.

3. If no tables are displayed, click Show Table command and use the CTRL key to select the tables you

want to display.

4. Click OK.

5. Right-click on the table join you want to delete.

6. Select Delete from the shortcut menu.

7. Click Close.

STEPS

Double-click on the column border to auto-fit the column to the data.

Page 38: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

37

Instructions Comments

1. Open the Lunch Orders table.

2. In the first record, click the Employee field. Note the data is simply a number.

3. Switch to Design View.

4. Click in the Employee field.

5. Change the data type to Lookup Wizard. Opens the Lookup Wizard.

6. Accept the option to look up the values in a table or query.

This option allows you to either enter the list now or use an existing field in a table as the list source.

7. Click Next.

8. Select the Employees table. Choose from a list of all available tables.

9. Click Next.

10. Add the Employee Last Name field.

11. Click the Selection Arrow. Moves the field to the selected field list.

12. Add the Employee First Name field.

13. Click the Selection Arrow. Moves the field to the selected field list.

14. Sort by Employee Last Name.

15. Click Next.

16. Adjust how wide you want to display the column and accept the default option to hide the key column.

17. Click Next.

18. Select Employee Last Name. Choose which field you want stored in the table.

19. Accept the Employee Last Name and click Finish.

20. Click Yes to save the table.

21. Switch to Datasheet View.

22. In the first record, click the Employee Name drop down list.

Note the names are sorted alphabetically.

23. Select an Employee.

24. Close the table.

25. Close the database.

Page 39: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

38

PROPERTY SHEETS In Access, use properties to determine the characteristics of tables, queries, fields, forms, and reports. You can view properties in a separate display called a Property Sheet.

WHAT IS A PROPERTY SHEET? The Property Sheet is a tabbed window that displays on the right side of the Access display. It contains five tabs; Format, Data, Event, Other, and All. The Property Sheet changes depending on the Access object that is displayed. If a form or report is displayed then Access allows you to choose to view the properties for the entire object or the property of a selected element within the object.

FORMAT SHEET TABS The Format tab contains all of the commands related to the formatting and design of the object. The Data tab contains all of the commands related to where the data on the form is being pulled from and how a user can interact with that data. The Event tab contains all the commands related to running macros. The Other tab contains all of the commands that don’t fit on the Format, Data, or Event tabs such as the Menu Bar display and Printing information.

Viewing a Property Sheet for Tables and Queries

1. Open the Access object.

2. Click the Home tab.

3. Within the View group, click the View drop-down list.

4. Choose Design View.

5. Choose the Query, Table, Form or Report Design tab.

6. For Queries and Tables within the Show/Hide group, click Property Sheet.

7. For Forms and Reports, within the Tools group, click Property Sheet.

Use the keyboard shortcut F4 to open the Property Sheet quickly. TIME SAVER

STEPS

Change the object selected by clicking on the drop down list.

Page 40: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

39

USING EXPRESSIONS IN OBJECTS

WHAT IS AN EXPRESSION? Expressions are a fundamental part of many Microsoft Access operations. An expression is a combination of symbols,

identifiers, operators, and values — that produces a result. In Access, the word expression is synonymous with formula. Sometimes you need information that is not stored in a field in your database. For example, you might need to calculate the total due from a patient for multiple services or calculate the quantity of a particular drug dispensed within a 24-hour period. To accomplish these tasks, you use expressions.

COST W/TAX:[COST]*1.06

RULES WHEN USING EXPRESSIONS You can calculate in a query, a form, or a report object. Custom calculations can be created referencing particular table and field names but there are a few rules to remember:

Table and field names must be referred to exactly but case is not considered.

Table and field names must be enclosed by brackets.

Table and field names must be separated by exclamation point.

Field names in formulas must be spelled correctly but are not case sensitive.

TIME SAVER

Existing field

Operator

New Field Name

Example of an expression located in an unbound text box.

Page 41: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

40

USING EXPRESSIONS IN QUERIES Expressions are commonly used in queries. If you created a field in a table to hold a total due for a patient visit, you would be forced to manually calculate that total in an object. Changes to the fees for services would require you to recalculate that total. Instead, you can create a calculation in a query to total the fees. You can then use that calculation in the report invoice generated for the patient at the end of the visit. Access also includes the ability to use common functions to quickly find high level information, such as an average quantity for a drug dispensed by the pharmacy. These are called Aggregate functions.

USING AGGREGATE FUNCTIONS Access includes the ability to group and summarize records within a query. By grouping the records, you can choose from a variety of built-in calculations, including sum, average, and count.

Finding a Count of Items

1. Create a new query in Design View.

2. Add the appropriate tables to the query.

3. Add the appropriate fields to the query.

4. Under the Show/Hide group, click the Totals button.

5. Click in the Total line for the field you want to summarize.

6. Select a function from the drop-down list.

7. Run the query.

STEPS

Turn on the Totals feature and a Total criteria line will be added to the query design grid.

Page 42: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

41

note

To get the best results with aggregate functions, use the fewest number of fields possible.

Instructions Comments

1. Open the Gourmet Lunch for Expressions database.

2. Create a new query in Design View.

3. Add the Employees, Lunch Orders, and Entrees tables to the query.

Note that the tables are already related.

4. Add the Employee Last Name field from the Employees table to the query.

5. Add the Main field from the Lunch Orders table to the query.

6. Sort the Employee Last Name field in Ascending order.

7. Click the Totals button. A Total line is added to the query grid.

8. In the Total line for the Main field, choose Count from the drop-down menu.

9. Run the query. For each product in the Inventory table, you will see the number of customers who ordered it.

10. Resize the columns to view the entire set of data.

11. Save the table as Orders by Employee.

12. Close the query.

Q?: What is another way to get a count of how many orders per employee?

CREATING CUSTOM CALCULATIONS When you create a custom expression in a query, you create a field that can be used in forms or reports. This allows you to extend your data without storing manually calculated information in a table.

In the query grid, the calculated field consists of three parts: the name you'll give to the calculated field, a colon, and then the expression that generates the result of your calculated field.

COST W/TAX:[COST]*1.06

NOTE!

Existing Field (in brackets)

Operator

New Field Name

Page 43: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

42

Creating a Custom Calculation in a Query

1. Create a new query in Design View.

2. Click in a blank field cell in the query grid.

3. Type a name for the expression, followed by the colon (:). Type the expression for the calculation, referring to the rules outlined in the Using Expressions section.

4. Right click on the field cell.

5. Choose Properties from the short-cut menu or press F4.

6. Select an appropriate format.

7. Run the query.

Instructions Comments

1. Open the Orders by Dept query. Note that there are many items that have quantities ordered greater than one.

2. Click in the first blank Field cell.

3. Type Cost w/tax:[Cost]*1.06 Be sure to spell the fields correctly and use the bracket symbol not the brace.

4. Run the query. Note that the Cost w/tax field has a general number format.

5. Switch to the Design View.

6. Right-click on the Cost w/tax field. A short-cut menu will appear.

7. Select Properties.

8. Choose Currency from the Format drop-down menu.

The Format property is found on the General tab.

9. Run the query. Note that the Extended Price field has a general number format.

10. Save the query.

11. Close the query.

How Can I get a Total for the ID department?

STEPS

Page 44: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

43

USING EXPRESSIONS IN FORMS You can also add an expression directly to a form. For example, you might use a calculation to total fees from multiple fields. An unbound text box is used to hold the expression. The expression is entered into the text box’s Property Sheet under the Control Source. Expressions in forms look for like an Excel formula because they start with an equal sign.

CALCULATING IN A FORM BY ADDING A TEXT BOX You can add text boxes to any form or report in either Design or Layout View. When a text box is added, it will not only create a text box but also a label box. The text box will display the word “unbound” in the box. This means that this text box is not bound to any particular field within the database.

Text boxes can be bound to a field or contain an expression that includes reference to a field. Expressions in forms are added within the Control Source of an unbound text box. You can use the Expression Builder to create the expression and then it will display in the Property Control Source box.

Adding an Expression to a Form

1. Open the form in Design View.

2. Click the Form Design Tools Design tab.

3. Under the Controls group, click the Text box button.

4. Click and drag on the form where you want the text box to appear.

5. Select the unbound object.

6. Click the Form Design Tools Design tab.

7. Under the Tools group, select the Property Sheet.

8. On the All tab, type a name for the object in the Name line.

9. In the Control Source line, type the expression or use the Build button to open the Expression Builder.

10. In the Format line, choose an appropriate format using the drop-down list.

STEPS

Unbound text box with label

Expression can be typed in within the Control Source line.

Choose the value’s format

Page 45: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

44

11. Select the label.

12. Click the All tab.

13. Under Caption, type a name for the label.

14. Press Enter.

15. View the form to test your expression.

You can also right-click on an unbound object to access the Property Sheet.

ADJUSTING THE TEXT BOX AND LABEL BOX LOCATION AND SIZE The text box and corresponding label box are both attached by default. You can adjust the individual box size by using the selected box’s size handles. If you need to move both boxes to a new location, simply click and drag one of the boxes and the other will also move. Use the larger black handle on either the label or text box to move them individually.

Instructions Comments

1. Open the Gourmet CO Orders form. Note that the form contains a subform with order details.

2. Switch to Design View.

3. Click the Form Design Tools Design tab.

4. Under the Controls group, click the Text Box tool. The mouse pointer will change to include Text Box icon.

5. Click below the Cost field. Use the crosshair located at the top left of the mouse pointer to help you position the field. An unbound control will be added.

6. Move the unbound control to align it with the Cost field, if necessary.

7. Right-click on the unbound object.

8. Choose Properties from the short-cut menu.

9. Click in the Control Source line. The Control Source property can be found on the All or the Data tabs.

10. Type = [cost]*1.06.

11. Click in the Format line.

12. Select Currency from the drop-down list. You will need to scroll down.

13. Close the Properties window.

14. Switch to the Form View. Note the calculation.

15. Save the form.

16. Close the form.

TIME SAVER

The larger square handle is to move the label only

Page 46: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

45

USING EXPRESSIONS IN REPORTS You can use expressions from queries in reports or create expressions directly within the report. Examples of common report expressions include a grand total for an invoice or claim, merged text fields storing name and address information, page numbers and date/time information.

CREATING AN EXPRESSION IN A REPORT In order to place an expression in a report you will need to be in Design view. Just like with forms, you can type an expression in the Property Sheet of an unbound object within the Control Source.

Adding an Expression to a Report

1. Open the report in Design View.

2. Click the Report Design Tools Design tab.

3. Under the Controls group, select the Text Box tool.

4. Using the Text Box tool, add an unbound control to the report in the section in which you want the

expression to appear.

5. Right-click on the unbound object.

6. Choose Properties from the short-cut menu.

7. On the All tab, type a name for the object in the Name line.

8. In the Control Source line, type the expression or use the Build button to open the Expression Builder.

9. In the Format line, choose an appropriate format using the drop-down list.

10. Close the Properties window.

11. Update the label object with descriptive text.

12. View the report to test your expression.

You can use the keyboard combination Shift + F2 to open a Zoom window. This window will allow you to enlarge the font size, making it easier for you to create and edit the expression.

TIME SAVER

STEPS

Example of an expression located in the property sheet.

Example of an expression located in a report’s unbound text box.

Page 47: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

46

Instructions Comments

1. Open the Orders by Dept report.

2. Switch to the Design View. Notice how the report is divided into sections.

3. Click the Report Design Tools Design tab.

4. Under the Control group, click the Text Box tool.

5. Add the unbound control box to the Dept Footer section below the “Summary for” text box.

6. Under the Tools group, click the Property Sheet. The text box Property Sheet opens on the right.

7. Under the Property Sheet’s Data tab, click in the Control Source line.

8. Press Shift + F2 on the keyboard. The Zoom window will open.

9. Change the Font command button. Allows you to change the font size so it’s easier to see.

10. Change the font size to 12. This affects text within the Zoom window only.

11. Click OK.

12. Type=Sum([Cost]*1.06. This formula adds together all the field values in the group and then multiplies them by 1.06.

13. Press ENTER.

14. Select the label and type Sum with Tax.

15. Press ENTER.

16. Click OK.

17. Click in the Format line.

18. Select Currency from the drop-down list. Formats the value to display a dollar sign and two decimal places.

19. Close the Property Sheet.

20. Preview the report.

21. Save the report.

Page 48: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

47

CONCATENATING TEXT The word “concatenate” means to combine. Concatenating text is handy when you need to display fields together in a report such as a First and Last Name. Access uses the plus sign (+) as the concatenation symbol. All literal characters, such as spaces and punctuation, must be enclosed in quotation marks.

Merging Text Fields in a Report

1. Open the report.

2. Right-click on the report and select Design View.

3. Click the Report Design Tools Design tab.

4. Under the Controls group, select the Text Box tool.

5. Using the Text Box tool, add an unbound control to the report in the section in which you want the

expression to appear.

6. Right-click on the unbound object.

7. Choose Properties from the short-cut menu.

8. On the All tab, type a name for the object in the Name line.

9. In the Control Source line, type the expression or use the Build button to open the Expression Builder.

10. Type the next text or field name.

11. Under the Views group, click the Report View or Print Preview button.

STEPS

Concatenation symbol Literal characters are in quotes

Page 49: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

48

Instructions Comments

1. Open the Orders by Dept report. Note that the report opens in Print Preview.

2. Switch to Design View. You can right-click on the open report and choose Design View.

3. Under the Detail Section, delete the Employee Last Name and Employee First Name fields.

4. Click the Report Design Tools Design tab.

5. Under the Controls group, select the Text box tool.

6. Add the unbound control within the Detail section lined up with Employee First Name field in the Page Header.

Make it big enough to accommodate both first and last names.

7. Type =[Employee Last Name]+”,”+[ Employee First Name].

Don’t forget the brackets!

8. Switch to Report View. Displays a Print Preview of the report.

9. Switch to Design View.

10. Select the text label and press DELETE. Click the larger selection handle to delete just the label.

11. Under the Page Header section, delete the Employee First Name text box label.

12. Select the Employee Last Name text box label and remove the word Last.

Select the text box and then double-click on Last to quickly select it.

13. Press ENTER. Accepts the change.

14. Select the concatenated text box, and add a space after the comma.

In some cases spaces needed to be added when combining fields. Access doesn’t automatically add them.

15. Right-click on the report and select Design View. Offers a quick way to switch to Design view.

16. Make adjustments to the size and position of the bound control, if necessary.

17. Switch to Report View.

18. Close the report and save the changes.

Page 50: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

49

OTHER TYPES OF QUERIES

QUERY OPTIONS Most queries used in Access are Select queries; this type of query extracts information from the table or tables used within the query based on the criteria set. Although you can use a Select query to change data in a table, no other action takes place. There are other types of queries as well, including a Make Table query, an Update query, an Append query, Delete query and Parameter query. These queries take action on your data.

To create these special queries you need to start with a Select query. When the select query is in Design View, then the Query Tools tab appears which contains the Query Type group.

UPDATE QUERIES An Update query is used to make changes in the data of a table. An Update query might be used if fees are increased (or reduced) by 5% across a group of services. Rather than calculate the new fee manually and revise the records, an Update query can calculate the entire field’s data within a table. In order to run an Update query, you must use the Run button on the toolbar. No action takes place when you use the View button.

Running an Update Query

1. Create a new query in Design View.

2. Add the appropriate tables to the query.

3. Add the desired fields to the query. The field being updated must be added to the query.

4. Add criteria to the query specifying which records are affected.

5. Run the query to verify you are returning the records you want to affect.

6. Switch to the Design View.

7. Click the Query Tools Design tab.

8. Select Update Query. An Update To line will be added to the query grid.

STEPS

Run command is important tool with specific queries.

Pushes data to a new table.

Pushes data to an existing table.

Updates values Deletes specific data from a table

Page 51: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

50

9. Enter an expression in the field you want updated.

10. Click the Run button.

11. Click OK to confirm the action.

12. Close the query without saving.

note

Saving an update query can be dangerous! The query will run when someone double-clicks on it, which will cause the update to occur again.

Instructions Comments

1. Open the Entrees table. Notice the prices of the wraps.

2. Close the table.

3. Create a new query in Design View.

4. Add the Entrees table to the query.

5. Add the SW and Cost fields.

6. In the Main criteria line, type *wrap. Displays only descriptions that end in wrap.

7. Run the query. There are 4 items with the SW field equal to Wrap. Note the prices of each.

8. Switch to Design View.

9. Click the drop-down arrow to the right of the Query Type tool.

A list of other query types will display.

10. Select Update Query. An Update To line will be added.

11. In the Update To line for the Cost field, type [Cost]+1.50.

This will increase the price of each wrap by $1.50.

12. Click the Run button.

13. Click Yes to confirm the action.

14. Close the query without saving. If you saved and reran the query the prices would increase again.

15. Open the Entrees table. Note the new prices.

16. Close the table.

17. Close the database.

OH NO! - Why is the Cost increase more than just $1.50?

NOTE!

Page 52: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

51

MAKE TABLE QUERIES A Make Table query is often used to segment data into separate tables. For instance, you might use a Make Table query to archive old patient visit records in order to improve the performance of your Visits table. Then you can delete the old records from the visits table and be assured that they are stored elsewhere in the database.

Running a Make Table Query

1. Create a new query in Design View.

2. Add the appropriate tables to the query.

3. Add all fields you want in the new table.

4. Add criteria to the query specifying which records will be added to the new table.

5. Run the query to verify you are returning the records in the new table.

6. Switch to Design View.

7. Click the Query Tools Design tab.

8. Run the query.

9. Type a name for the new table.

10. If you want to the table to be stored in a new database, select the database.

11. Click OK.

12. Under the Results group, select Run.

13. Click Yes to confirm the action.

14. Close the query without saving.

STEPS

Page 53: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

52

Instructions Comments

1. Open the Employees table. Note how many inactive employees display.

2. Close the table.

3. Create a new query in Design View.

4. Add the Employees table to the query.

5. Add all the fields individually to the query.

6. In the Inactive criteria line, type Yes.

7. Run the query. How many inactive employees are there?

8. Switch to the Design View.

9. Click the Query Tools Design tab.

10. Under the Query Type group, select Make Table query.

11. Type Inactive Employees as the table name.

12. Under the Results group, click Run. Changing the View will not run a Make Table query.

13. Click OK. Closes the Make Table dialog box.

14. Click Yes. Confirms the action.

15. Click Yes. Confirms pasting rows into the new table.

16. Close the query without saving.

17. Open the Inactive Employees table. How many records display in the new table.

18. Close the table.

Page 54: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

53

APPEND QUERIES An Append query adds records to an existing table. You might receive a report on new patients from another department and would like to add these patients automatically to the existing Patients table. You must first make sure that all of the required data is present in the records you wish to append.

Running an Append Query

1. Create a new query in Design View.

2. Add the appropriate tables to the query.

3. Add all fields you need to append.

4. Add criteria to the query specifying which records will be added to the existing table.

5. Run the query to verify which records will append.

6. Switch to Design View.

7. Click the Query Tools Design tab.

8. Under Query Type, click Append.

9. Click the Table Name drop down list and select the table you want to append.

10. If you want to the table to be stored in another database, select the database.

11. Click OK.

12. Under the Results group, select Run.

13. Click Yes to confirm the action.

14. Close the query without saving.

STEPS

Page 55: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

54

Instructions Comments

1. Open the Employees table.

2. Select two records and mark them Inactive. Click in the Yes/No checkbox.

3. Close the table.

4. Create a new query in Design View.

5. Add the Employee table to the query.

6. Add all the fields individually to the query. To save time, select all the fields first and then drag and drop them into the design grid.

7. In the InActive criteria line, type Yes. Yes or True means checked.

8. Run the query. Check the results.

9. Switch to the Design View.

10. Under the Query Type group, select Append Query.

The Append window will open.

11. Select Inactive Employees from the Table Name drop-down list.

12. Click OK.

13. Under the Results group, select Run. Appends the table with the new data.

14. Click OK to confirm the append action.

15. Close the query without saving.

16. Open the Inactive Employees table. Note how many employees are now listed.

17. Close the database.

Page 56: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

55

DELETE QUERIES When you need to remove a small amount of records from a database it’s safest to simply open the table or form that displays the records and use the Delete command. But if you need to delete large amounts of data quickly, use a Delete query. Delete queries remove all the data in each field, including the key value that makes a record unique.

If you wish to use the Delete query to remove records from multiple tables rather than just a single database table, you will need to ensure that:

You have relationships defined between related tables in your Microsoft Access database

You have enforced the Referential Integrity for the relationships between your chosen tables.

You have opted to Cascade Delete Related Records for the relationship type

If you are using the Delete query to remove records from multiple tables that are related in a One-To-Many relationship without having defined the option to Cascade Delete Related records, Microsoft Access will only delete the records from one table at a time. If this is the case, you must delete the records from the many side of the relationship first (to ensure against orphaning records), and then delete the records from the one side of the relationship.

note

Make sure that you have a backup of your database before you delete your data.

Running a Delete Query

1. Create a new query in Design View.

2. Add the appropriate tables to the query.

3. Add all fields you want to delete.

4. Add criteria to the query specifying which records will be deleted.

5. Run the query to verify which records will be deleted.

6. Switch to Design View.

7. Click the Query Tools Design tab. Under Query Type group, click Delete.

8. Click Yes to approve the delete.

9. Close the query without saving the changes.

STEPS

NOTE!

Page 57: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

56

Instructions Comments

1. Create a new query in Design View.

2. Add the Employees table to the query.

3. Add all the fields individually to the query.

4. In the Inactive Employee criteria line, type Yes. Yes or True means checked.

5. Run the query. The results display only the Inactive Employees.

6. Switch to the Design View.

7. Under the Query Tools Query Type tab, select the Delete Query.

8. Click Yes. Approves the record deletion.

9. Close the Query and don’t save it.

Page 58: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

57

PARAMETER QUERIES Sometimes we need to use the same basic query on a monthly basis but one variable needs to be adjusted each time. Instead of recreating a new query each time or changing the design of an existing one, you can build a Parameter query. This useful query asks for your input to set criteria before the query actually runs.

Instead of typing a value or expression into the criteria cell in Design view like a normal query, you type your question in the criteria cell and enclose it in square brackets ([ ]).

The text you type will appear as a prompt on a dialog box, so you might want it to be in the form of a question to the user. In this example the user will be prompted to type the name of a department when they run the query. The text the user types will be used as the criteria for that particular field. The dialog box looks like this…

Creating and Running a Parameter Query

Create a Select Query in Design View. 1. Add any non-variable criteria to the appropriate fields in the Criteria row. 2. Locate the field(s) you would like to make variable criteria. 3. Type the phrase you would like to appear in the prompt that will pop up every time you run your 4.

query. Enclose the phrase in brackets [].

Under the Results group, click Run. 5.

A dialog box will appear with the prompt you specified. 6.

Enter your criteria and click OK. 7.

STEPS

Adding brackets to the value or expression will set it as a parameter message.

When the message appears, type in your criteria.

Page 59: PDF 2010 Microsoft Access Intermediate Skills Manual

Microsoft Access 2010 Intermediate Skills

58

TIPS FOR WRITING PARAMETER QUERIES

Ideally, the prompt you create for your query should make it clear what type of information the search term should be, and what format it should be entered in. For example, to guarantee that people enter a search for a date in the format we use in our database, we could write the following in the Criteria: row of the Date field: [What date? (mm/dd/yy)] .

The simplest parameter query will give you an exact match criteria, meaning that the query will search for the exact text you enter in the prompt. However, you can turn any type of criteria into variable criteria. Simply type your prompt text in brackets in the part of the criteria where you would normally put a search term. For example, in a normal query, we could find orders that were placed between two dates by using the criteria Between x AND y, and replacing the x and y with the first and second dates, respectively. To turn this into a parameter criteria, we would simply replace the x and y with the text we want to appear in the prompt. Our variable criteria might look like this: Between [Enter the start date:] AND [Enter the ending date:]. These prompts would appear:

Instructions Comments

1. Open the Orders by Date query.

2. Within the Order Date field criteria line, type [What Date?]

3. Press Enter.

4. Switch to Datasheet View. Runs the query with the parameters.

5. Type 5/15/2013 and click OK.

6. Switch to Design View.

7. Within the Order Date field criteria line, remove the current parameter. Replace with the following: Between [Enter Start Date:] and [Enter End Date:]

Adding the Between and And operators creates multiple parameter criteria boxes.

8. Press Enter.

9. Switch to Datasheet View.

10. Enter a Start Date and click OK.

11. Enter an End Date and click OK.