Professional Documents
Culture Documents
The University of Minnesota is an equal opportunity educator and employer. This publication/material is available in alternative formats upon request. Direct requests to oca@umn.edu. Printed on recycled and recyclable paper with at least 10 percent postconsumer material.
10.05.2011
Table of Contents
LESSON 1 I NT R O D UC T I O N T O DATABASES
Database Design Vocabulary Relationships Design Process
1 1
1 2 3
LESSON 4 C R E A T I NG R E L A T I O NS H I P S
Print the design Removing Tables from the Diagram Deleting or Editing Relationships
31 31
35 36 36
37 37
38
7 7
7 8 10 17 18
M OD I F Y I NG T A B L E S A N D R E L A T I O N S HI P S
Adding the New Foreign Key Field
LESSON 6 C R E A T I NG I NT E R F A C E S T O I NT E R A C T W I T H D A T A
Using Forms vs. Tables for data entry
41 41
41 42 49 55 64 71 73
LESSON 3
19
S E T T I NG F I E L D P R O P E R T I E S 19
Resizing text fields Input Masks Default values Format number fields Exercise 2: Setting field properties 20 21 24 26 28
Creating the Member Status type Entry Form Exercise 3: Creating Entry Forms Using Multiple Items Form Option Handling One-to-Many Relationships with Subforms Exercise 5: Creating a Subform using Autoform Linking the Main form and Sub Form Navigating Records and Entering Test Data
Creating Forms using the wizard Exercise 6: Creating the CD entry form using the Wizard
74 79
LESSON 9 R E P OR T S
Creating a Report Using the Auto Report option
123 123
123
LESSON 6 CONTINUED
Adding to combo box lists on the fly Finding a Record on a Form Using a Combo Box to Find Records on the Form Creating the Member CD Form and Subform in Design View Date picker 84 86 89 93 101
Exercise 9: Creating a Report Using Auto Report 127 Creating a Report Using the Wizard Exercise 10: Creating a Report Using the Wizard 128 133
LESSON 10 F I N A L T O UC H E S
Menu
139 139
139 145
LESSON 7 E D I T I N G A F OR M
Modifying a Form
103 103
103
LESSON 8 Q UE R I E S
Creating a Query using the Query Wizard
107 107
107
Exercise 7: Creating a Query Using the Wizard 114 Adding Criteria to a Query Creating a Query in Design View Exercise 8: Creating a Query in Design View End of Exercise 116 118 121 122
ii
LESSON 1: INTRODUCTION
TO
DATABASES
APPENDIX A U S I N G T H E L A B E L R E P OR T WIZARD
149 149
APPENDIX B C R E A T I NG A Q UE R Y U S I N G A GG R E G A T E F U N C T I O N S
153 153
APPENDIX C
157
E X P OR T I N G Q U E R Y R E S UL T S 157
161
WIZARD
161
APPENDIX E E X P OR T I N G A R E P O R T
165 165
iii
1
Lesson
Less on 1
Introduction to Databases
Database Design Vocabulary
Database
Field: One piece of information you want to track in your database
Example:
Table
TITLE The Best of Van Morrison Positive Friction Natural Sketches of Spain Kind of Blue Rice, Rice, Hillman and Pedersen Man of Constant Sorrow Sketches of Spain Man of Constant Sorrow I Wonder Where You Are Tonight A Day Without Rain In Pieces American Idiot GENRE Rock Americana Brazilian Jazz Jazz Bluegrass Bluegrass Jazz Bluegrass Bluegrass New Age Country Pop
Fields
PERFORMER Van Morrison Donna the Buffalo Celso Fonseca Miles Davis Miles Davis Rice, Rice, Hillman and Pedersen Ralph Stanley Miles Davis Ralph Stanley Del McCoury Enya Garth Brooks Green Day
YEAR 1990 2000 2003 1991 1991 1999 2001 1991 2001 2002 2004 2002 2004
OWNER Amy Jones Amy Jones Amy Jones Amy Jones Amy Jones Amy Jones Amy Jones Andy Jackson Andy Jackson Andy Jackson John Smith John Smith John Smith
Records
LESSON 1: INTRODUCTION
TO
DATABASES
Best Practice
When you expect multiple values or repeating data in a table, you should consider splitting the table into two or more related tables. For example, in the flat/Excel file shown above, Miles Davis is listed several times, as are some of the Genres and Owners.
Relationships
Types of Relationships
One-to-Many: The most common type. For every record in Table A, there are multiple
records in Table B Example: One Member may have many Addresses (work, home, etc.)
Many-to-Many: For every record in Table A, there are multiple records in Table B, and
vice versa. In this case, a third tablecalled a Join Tableneeds to be created, which will contain only unique values. Example: One Member may have many CDs, and the same CD (title) may be owned by many Members
LESSON 1: INTRODUCTION
TO
DATABASES
Best Practice
The Join Table name should be a combination of the two many-to-many tables (e.g., tblMemberCD). The Primary Key can be the Join Table name or the first initial of each table name followed by ID (e.g., MemberCDID).
One-to-One: The rarest type, it is used for security and organization (i.e., avoiding empty
Keys
Key fields are used to build the relationships between data in different tables.
Primary Key: Auto-numbered, unique ID for internal tracking and for matching
The Primary Key should be the table name plus ID (e.g., MemberID) and should be the same name in the table where it is used as a Foreign Key. (There are exceptions. In this course, the Foreign Keys LoanedToID and MemberID in tblMemberCD table both go back to the Primary Key MemberID in tblMember.) The Primary Key should always be the first field in each table, followed by any foreign key(s). Primary Keys should never be actual datanot even something unique like a Social Security number or Student ID. These values are obtained from an outside source and, while seemingly unique and reliable, could produce data entry errors.
Design Process
These preliminary steps are done on paper.
LESSON 1: INTRODUCTION
TO
DATABASES
Statement of Purpose
each member and track the loaning of CDs between members. Classify all CDs by Genre, and list performer, year released, and value for each CD. Provide reports, such as member and CD listings. Best Practices
Use Prefixes in form or object names to denote different object types (e.g., tbl for tables). Avoid using spaces or special characters in object and field names. For single, repeating values requiring consistent data entry and searching, use a Lookup Table or Value List.
Value List: Works well for single, static, short lists of information that is
add additional values at a later date. You can choose a level of restriction (e.g., Genre).
Determine Fields
Best Practice
Avoid multiple yes/no fields or fields that will be consistently left empty. Consider breaking out into a separate table or creating a Lookup Table of values to choose from. Keep items such as names (first name and last name) in separate fields. It is far easier to join those fields when needed than it is to separate a first name, last name, suffix, etc., to use in a mail merge or similar function. This also keeps them consistently in the same place. If you only have one field, you have little or no control over the order of entry.
4
UNIVERSITY TECHNOLOGY TRAINING CENTER
LESSON 1: INTRODUCTION
TO
DATABASES
Consider an active/inactive status field instead of deleting records and losing history (such as Member Status).
Define Relationships
Used for viewing, adding, and updating data Used to ask Access to return a selected portion of your data Used to format the data for printing
2
Lesson
Less on 2
Go to Start Programs Microsoft Office Microsoft Office Access 2007 (or Microsoft Access 2010)
In Access 2007, you may have to click Blank Database in the upper left corner first. In the window pane on the right hand side, type the name you want to give the database (in our case: CDClub).
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
1
2 4
Click on the browse folder icon. Browse to and select your Desktop Access 2010 Basics folder
4. Then click Create. Access will open to a new table in datasheet view.
In Access 2007:
1. You will see a Security Warning just under the tools Ribbon. Click Options. 2. If you are absolutely positive the database is safe, choose Enable this content. If
you have any doubt, choose Help protect me from unknown content. This will turn off Macros that could potentially do harm to your database or computer.
3. Click OK
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
In Access 2010:
1. You will see a Security Warning just under the tools Ribbon. 2. If you are absolutely positive the database is safe, choose Enable this content. If
you have any doubt, simply close the warning with the small x on the right. This will turn off Macros that could potentially do harm to your database or computer. For more information about creating trusted locations for frequently used databases, and other security issues, search Access help for Security.
Access 2007 has the default for text fields set at 255 instead of the old default of 50. That is much larger than needed for most fields and will eat up too much space. We will set this to a more reasonable 35. You only have to set this once, and it will apply for all future fields you create. It will not change older fields. Select Object Designers from the left hand pane. Change Default text field size: to 35.
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
Access 2007 introduced a new view called Layout View. This view is a cross between Design View and Form View. It lets you make changes to the design while seeing data in the fields at the same time. This view can be confusing for new Access users, so we will disable it during class. Turning this view off or on only applies to the database you are currently working in. If you would like more information about the Layout View and when to use it, search Access Help for Layout View. Select Current Database from the left hand pane. Uncheck Enable Layout View for this Database
Creating Tables
The first step in making a new Access database is to create all the tables we will need in our database. Access gives us an initial table to work with in datasheet view. It includes a primary key simply called ID. You want to give the primary key a more meaningful name so it is obvious
10
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
which table that ID belongs to. We will follow our best practice of naming the primary key as the table name followed by ID (with no spaces in the field name). Views in Access
For all objects we work with in this class, we will be switching back and forth between Design View of the object and the end user view or result (Data Sheet, Form, query results, or report results). The method of changing the view is identical.
2. Access will ask you to save the table first. Type in the table name tblMember.
3. Click OK
11
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
tblMember. The top section of the table Design View screen has three columns for entry
Navigation Pane
12
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
default data type. The default width is now 255, which is the maximum.
Memo: Alphanumeric Text field that allows for 65,535 characters. Use when you need a
the calendar. Date formats are determined by the Regional Settings on each PC. If those settings have been changed, you may get unexpected results.
Yes/No: Displays as a checkbox and stores one value (yes or no, true or false). Access
etc.). For more information on Data Types, search Access Help for Data Type in quotes (this restricts the search to that specific phrase, rather than searching for Data and Type separately).
Descriptions
The description is used to describe what a field is used for, and is entirely optional. This information will show up on a form as a screen tip if you hover over the field so it can also be used as tips for data entry.
13
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
by picking from the list, or you can just start typing the word and it will find and display the closest matching data type for you.
2. Once you have entered all the fields and their corresponding field types, close
and save the table design. You will be asked to confirm design changes on a table, form etc.
3. Click Yes 4. To create another table click on the Create tab on the Ribbon
14
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
15
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
Note:
Fields such as Zip and phone are created as text fields instead of number fields in order to apply input masks for controlling the data entry format (also in case you need to enter foreign Zip codes.)
8. Repeat for tblCD
Note: If you use Value instead of CDValue you would get the following message:
16
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
End of Exercise
17
LESSON 2: BUILDING
A
DATABASE
IN
ACCESS
The Join Table name should be a combination of the two many-to-many tables. The Primary Key can be the Join Table name or first initial of each table name followed by ID.
Note
When working with an existing database, you may or may not see some form of object-naming conventions and proper field/table names. Changing the names of tables and fields may affect many areas of your database, from relationships to forms and reports to possible VBA code behind the scenes. Use extreme caution when changing names if you are not sure where an object is used and how to correct errors that may surface after such a change.
18
3
Lesson
Less on 3
Set all the format properties and options at the table-level so they propagate through the forms, queries, and reports.
19
If you are working with a table that has data in it already, you may cut off existing data in that field if it is longer than the new field size you select. Access will display a warning message before it applies the change so you have a chance to cancel.
1. Open tblMemberAddress in Design View by right clicking on it and choosing Design View 2. Select the State field 3. Click on the Field Size value in the General tab 4. Change the value to 2
20
Zip:
Phone:
Field size of 10
Input Masks
Input Masks are used for consistent data entry and formatting of dates, phone numbers, and zip codes. Added characters (/ - ( ) , etc.) are for display onlythey are not saved as part of the data value. The field must be text, not numeric. If the Input Mask is added later, it will only show formatting correctly if it consists of straight numbers with no preexisting formatting. If you are working with existing data that has characters in the field (such as / or -), remove the extra charactersmanually or by using a Search/Replaceand the data will then correctly display with the chosen input mask.
1. Select the Phone field in tblMemberAddress 2. Click in the Input Mask field in the General tab.
21
The
3. Click on the ellipses button to display the Input Mask Wizard 4. You will be asked to save changes made to the table design
5. Click Yes 6. The input mask wizard will appear. Choose the Phone Number option (first one
in the list)
22
7. In the Try It section, type in a phone number and see how it works.
Note:
With all Input Masks, if you tab into the field, the cursor will start at the beginning. If you click in the field, it will start wherever you click, and you have to move to the beginning of the field in order to type the data correctly into the Input Masks format.
8. When you are done testing the Input Mask, click Finish. This is what the phone
23
Default values
Default values can save data entry time for a field where most of the records entered will have the same value (i.e., most of the CD Club members will be from Minnesota). The value entered can be easily changed for the exceptions that occur. A default value will only be present in new records. Existing records will not be affected. In order for our DateCreated field in each table to work, we need to add a command into the default value. Default values apply only to new records. So every time there is a new record, we want to put in todays date automatically.
1. Select the State field in tblMemberAddress
24
3. When you tab off the field it will put double quotes around the value.
4. Choose the Country field 5. In its default value type USA 6. Select the DateCreated field 7. In the Default Value field, type Date()
Note:
Be sure to include both parentheses! This will date stamp the creation of a new record when it is added to the table. If you wanted to be more precise, you could use the command Now(), which includes both date and time.
25
-32768 to 32767 (rounds up or down, default number type) Key auto number fields)
Long Integer: -2,147,483,648 to 2,147,483,647 (rounds up or down, default for Primary Single: Double:
-3.4x1038 to 3.4x1038 (up to 7 decimal places, will not round) -1.797x10308 to 1.797x10308 (up to 15 decimal places, will not round)
26
left.
5. Click on the CDValue field to select it 6. Change the DataType to Currency
7. Switch back to Datasheet View and type in 14.99. It will now display as $14.99
27
Add the Date() command into the default value for DateCreated Add a Short Date Input Mask to the Birthday field
2. In tblAddressType
Add the Date() command into the default value for DateCreated
3. In tblMemberAddress
4. In tblGenre
Add the Date() command into the default value for DateCreated
5. In tblPerformer
Set Performer to a length of 100 Add the Date() command into the default value for DateCreated
6. In tblCD
Set Title to a length of 100 Add the Date() command into the default value for DateCreated
7. In tblMemberCD
Add a Short Date Input Mask to the DateOut field Add the Date() command into the default value for DateCreated
28
End of Exercise
29
4
Lesson
Less on 4
Creating Relationships
In Access, you create the relationships between tableswhich youve previously defined on papervisually through the Relationship Window. Setting up the relationships here tells Access how the tables within this database are linked to each other. If you set up the relationships this way, the links between tables will carry through anytime you use the tables in a form, query, or report.
1. Click on the Database Tools Tab 2. Click on Relationships
31
3. The Show Table dialog box will appear. Select all tables in the database at once
by holding down your shift key and click on the first table in the list, then the last table.
Note:
If the Show Table dialog box is not open, you can right click in a blank section of the relationships window and choose Show Table
5. Once you have all the tables showing in the window, you can rearrange them to
look like your paper diagram by simply dragging them around the window.
32
Create the lines to connector relatethe Primary Keys to their respective Foreign Keys, as on our paper diagram.
6. Click on the Primary Key in one table and drag it over to the Foreign Key in the
related table. When you release the mouse you will see the Edit Relationships dialog box.
Important Notes!
These fields must be of the same data type to be related. If not, you will get an error message and will have to return to the table design and make the necessary corrections. Only one relationship is allowed between any two tables.
33
On the Edit Relationships window, there is an option that should be checked for all tables: Enforce Referential Integrity, which prevents a record from being deleted in one table when there are related records in the other table. This option aids in data integrity by not allowing deletion of data creating orphan records. For example, you wouldnt want someone to delete a CD from the database that multiple people own and may be out on loan.
Note:
In Access 2003, setting Referential Integrity also acted as a required field setting. Setting Referential Integrity between Genre and CD, for example, will prevent you from adding a CD record without selecting a Genre as well. If you have data in the tables already, Access will not allow you to set Referential Integrity until all records meet this requirement. This was not the case before 2003 and has been changed back in 2007 so that it does not create the required field property.
8. Click Create 9. Repeat for all tables in the database
34
2.
35
4. After you have printed the report close the report preview window using the X in
36
5
Lesson
Less on 5
37
RELATIONSHIPS
Add the Date() command into the default value for DateCreated
Open tblMember in design view by right clicking on the table name in the navigation pane
38
RELATIONSHIPS
Updated Relationships
39
6
Lesson
Less on 6
Example of a table with multiple foreign keys after data is entered through a form.
41
INTERACT
WITH
DATA
We will examine 4 methods for creating forms in Access 2007 along with the ability to update the values in a lookup table on the fly when doing data entry on a form.
Types of Forms
Single/Columnar Form: Displays one record at a time. Multiple Item/Continuous/Tabular Form: Displays as many records as will
fit in the detail section of the form. As you type in one record, a new record is started below. The multiple items form is most often used for the lookup tables to create an easy form to maintain the values in those tables.
This method of form creation automatically selects all fields from the table and displays them on the form in Form View.
42
INTERACT
WITH
DATA
4. At this point the basic form is ready for data entry. Enter the following values:
Active Inactive
Note how the icon to the left changes to a Pencil symbol and a new record is created below as soon as you start typing in the Member Status field. The primary key ID is also assigned at this point. The Pencil means this record is currently being edited. Once you leave that record, it is automatically saved, and it moves to the next record.
5. Make note of what MemberStatusID number gets assigned to Active, we will
When you open the form in design view initially it may show all the fields and labels with an orange border. This means they are currently grouped together. To ungroup them, click anywhere in the form outside of the grouping.
43
INTERACT
WITH
DATA
7. Edit the form title in the form header by clicking once to select the field, then
clicking again on the text of the field to go into edit mode. Remove tbl and put a space between Member and Status
8. Remove the MemberStatusID field. This field does not need to be shown on the
Access automatically repositions the other fields after you remove a field. 9. Put spaces in the field names to separate the individual words in the labels that are
44
INTERACT
WITH
DATA
Important Note!
You can edit the label (the transparent box), but you cannot edit the text box (the white box). The text box is the actual field name that the data entered will be saved into. If you change or delete the content in the white box, Access will not know where to save the data entered on the form. If you do accidentally change the field name, Access 2003 and later will mark the field and give you suggestions to fix the error. For example, if you change the MemberStatus field to Member Status, the field will show a green triangle in the upper left corner.
Click on the triangle to produce a yellow exclamation sign. Click on it to see the Access tips for fixing the problem.
The highlighted fields are the applicable errors. Listed below are possible solutions. In this case, the solution would be to Edit the Controls Control Source
45
INTERACT
WITH
DATA
Property and choose the correct field (or simply remove the space so it is a valid field name).
The Enabled and Locked properties can be used together or independently depending on the desired outcome.
Enabled=No
Locked = Yes will lock the field but not grey it out. The field is selectable and
can be copied. It can cause confusion on the end users part because it acts like one should be able to type in the field
Enabled=No AND Locked = Yes
46
INTERACT
WITH
DATA
3. Return to form view using the View toggle to see the changes made. 4. Close and Save the form as frmMemberStatus
47
INTERACT
WITH
DATA
48
INTERACT
WITH
DATA
them to show up in combo box used to create a CD record. This form is used to add and edit the values in tblPerformer.
1.
49
INTERACT
WITH
DATA
4. Switch to Design View using the View Toggle on the Home tab 5. Edit the Label in the header of the form to remove the tbl 6. Delete the PerformerID field 7. Put spaces in the DateCreated label 8. Lock down the DateCreated field using the field properties (set Enabled to No
9. Save and name the form as frmPerformer 10. Switch to Form View and enter test data:
50
INTERACT
WITH
DATA
51
INTERACT
WITH
DATA
Part 2:
Address Type entry form: We need to enter the Address Types ahead of time in order
for them to show up in combo box used to create an Address record. This form is used to add and edit the values in tblAddressType.
1. In the Object Navigation pane, click on tblAddressType 2. From the ribbon menu choose Create 3. From the Forms section choose Multiple Items
4. Switch to Design View using the View Toggle on the Home ribbon menu 5. Edit the Label in the header of the form to remove the tbl and put a space
52
INTERACT
WITH
DATA
8. Lock down the DateCreated field using the field properties (set Enabled to No
9. Save and name the form frmAddressT ype 10. Switch to Form View and enter test data:
End of Exercise
53
INTERACT
WITH
DATA
54
INTERACT
WITH
DATA
Member Form
The member form is the main form that shows one member record at a time.
1. In the object navigation pane select tblMember 2. Choose the Create tab
55
INTERACT
WITH
DATA
Newly created member form 4. Go to the Home tab and Switch to Design View
56
INTERACT
WITH
DATA
5. Click outside the field grouping to ungroup them 6. Edit the top label in the header to read Members instead of tblMember 7. Edit the field labels to add spaces between the words
We wont delete the key field in this case since the MemberID could be referenced and searched by within the database (like an account number)
8.
Put spaces between First, Middle, and Last and Name Put a space between Date and Created
Replace MemberStatusID with a combo box that looks up the values from tblMemberStatus Select the MemberStatusID field and click delete From the Design Tab click on the Combo Box object to select it (it will place an orange border around the selected object).
Move your mouse to the location on the form that you want to place the object, in this case - below the Date Created field. (Your mouse icon will change to the icon for the object you have selected).
57
INTERACT
WITH
DATA
The combo box wizard will appear, leave the default option, click Next
Move MemberStatusID and MemberStatus fields into the selected fields area by double clicking or using the > button, click next.
58
INTERACT
WITH
DATA
Note: When creating a combo box the Primary Key of the lookup table should always be the first field and the text field the user will actually see and pick from should be the second field. Select MemberStatus as the field to sort by, click next
The next screen is a preview of what the drop down box contents will be. Here you can make the column(s) wider or narrower as needed. Note that the Primary key (the first field we selected) is automatically hidden from the user.
59
INTERACT
WITH
DATA
This step is critical, we must tell Access where to store the selection we make. The combo box stores the value of the primary key which needs to be saved to the corresponding foreign key in the table the form is based on. Use the dropdown to select the matching foreign key field MemberStatusID, click next.
Type in the text you want for the label part of the combo box: Member Status, click Finish
60
INTERACT
WITH
DATA
9.
Move the newly created combo box below the Birthday field. Access will automatically move the DateCreated field down to accommodate for it and reformat the field to match the others. need to shorten the fields to make room to add a subform. Group all of the fields in the detail section
10. We
Click anywhere above the MemberID Field (not the label, we dont want the labels included in this grouping)
Drag your mouse down and to the right until all fields are within the box it has created.
61
INTERACT
WITH
DATA
11. Let
go of the mouse and all the fields will be grouped and showing the orange border
Make sure that the labels to the left of the fields are not included in the grouping (not outlined in orange) 12. Hover over
the right side and get the double arrow, then drag the group to the left to shorten all the fields at once.
13. Release
14. Lock
down the DateCreated field by setting enabled to no and locked to yes in the fields properties
62
INTERACT
WITH
DATA
63
INTERACT
WITH
DATA
1. In the object navigation pane select tblMemberAddress 2. Choose the Create tab 3. Under the Forms section choose Form
64
INTERACT
WITH
DATA
4. Go to the Home ribbon menu and Switch to Design View 5. Click outside the field grouping to ungroup them 6. Edit the top label in the header to read Addresses instead of tblMemberAddress 7. Delete the MemberAddressID and MemberID fields 8. Replace the AddressTypeID with a combo box
Delete the AddressTypeID field Select the Combo Box option from the design tab and place it under date created on the form Select tblAddressType Select AddressTypeID and AddressType
65
INTERACT
WITH
DATA
Type Address Type for the label text Move the newly created combo box above the Address1 field.
Put spaces between Address and the 1, 2 or 3 Put a space between Date and Created
10. Group
Click anywhere above the AddressTypeID Field (not the label, we dont want the labels included in this grouping)
Drag your mouse down and to the right until all fields are within the box it has created.
11. Let
go of the mouse and all the fields will be grouped and showing the orange border
Make sure that the labels to the left of the fields are not included in the grouping (not outlined in orange)
66
INTERACT
WITH
DATA
the right side and get the double arrow, then drag the group to the left to shorten all the fields at once.
67
INTERACT
WITH
DATA
13. Because this is a subform, we want it to be compact. We also need to shrink the
size of the form itself (the area with the grid behind it). You may have to scroll to the right to see the edge of the form.
14. Hover over the right edge of the form and get the double arrow with the vertical
line.
15. Drag the side of the form up to the right edge of the fields and release.
Oops! It didnt work. The box for the addresses label is very large and needs to be shrunk as well.
68
INTERACT
WITH
DATA
16. The
easiest way to do that is to right click on the Addresses label in the header and choose Size To Fit
69
INTERACT
WITH
DATA
18. Lock
down the DateCreated field by setting enabled to no and locked to yes in the fields properties
19. Close and Save the form as subfrmMemberAddress (use the subfrm
Note:
You should not enter an address in the subform as it is right now. You need to have it linked to the main form first to know what member the address belongs to. We will do that in the next step together.
End of Exercise
70
INTERACT
WITH
DATA
This step is similar to setting the relationship between two tables. We need to tell Access which primary key/foreign key fields link the two forms. This method also allows Access to maintain the Primary and Foreign Key values that are used to match the addresses with their respective members.
1. Open frmMember in Design View 2. In the Design tab in the Controls section click on Subform/Subreport
3. Click in the detail area to the right of MemberID 4. From the subform wizard click on Use an Existing Form
71
INTERACT
WITH
DATA
6. In the Define Link form, choose the Show tblMemberAddress for each record in tblMember using MemberID option Click Next
If the show statement is cut off in the selection window you can see the full statement right below.
72
INTERACT
WITH
DATA
7. Leave the name subfrmMemberAddress for the form name Click Finish
73
INTERACT
WITH
DATA
New Record When you have a subform you will have a second set of navigation buttons to move through the subform records.
home address
3. Click on the Next button in the subforms navigation bar 4. Choose Work or School as the address type and enter another address for
yourself
5. Click on the Next button of the main Members form and repeat for another
member. Enter a friend, relative, or made up member with two different addresses
This form will be used to enter values into the Genre Lookup Table for use when entering CDs.
1. On the Create ribbon menu choose More Forms Form Wizard
74
INTERACT
WITH
DATA
3. Select the fields that you want to see on the form by using the > button.
75
INTERACT
WITH
DATA
4. After the 2 fields are under the Selected Fields area, click Next 5. Choose the Tabular option (multiple items/continuous type of form), click Next
76
INTERACT
WITH
DATA
6. To make this form match our previously created forms choose the Access 2007
8. From the Home menu, switch to Design View 9. Remove frm from the frmGenre label 10. Widen the Genre field and label 11. Lock
down the DateCreated field by setting enabled to no and locked to yes in the fields properties
12. Save the design changes and switch back to Form view
77
INTERACT
WITH
DATA
Modify Formatting
1. Enter test data:
Note the Alternating lines created with this style of creating a form.
2. If we want to turn that off to match the style of the other forms we have created,
78
INTERACT
WITH
DATA
3. Hover over Alternate Fill/Back Color and select None to remove it.
79
INTERACT
WITH
DATA
3. Select the fields that you want to see on the form by using the > button.
4. After the desired fields are under the Selected Fields area, click Next 5. Choose the (default) Columnar option (Single form), click Next
80
INTERACT
WITH
DATA
6. To make this form match our previously created forms choose the Access 2007
81
INTERACT
WITH
DATA
8.
9. Remove frm from the frmCD label 10. Remove the PerformerID field and replace with a combo box
Select PerformerID and Performer from tblPerformer Sort by Performer Save selection to the PerformerID field Label as Performer
11. Remove the GenreID field and replace with a combo box
Select GenreID and Genre from tblGenre Sort by Genre Store the selection in GenreID Label as Genre
12. Space out the words in CDValue, YearReleased and DateCreated labels 13. Lock
down the DateCreated field by setting enabled to no and locked to yes in the fields properties
82
INTERACT
WITH
DATA
14. If you want to have the fields in a different order (more logical for data entry),
simply drag them up or down in design view. Move the title field to the top, followed by Performer, Genre, Year Released, Value and Date Created
all fields and the form itself so it is more compact but still has room in the fields for the data.
End of Exercise
83
INTERACT
WITH
DATA
What happens if the Genre or Performer does not exist in the Combo Box?
Answer:
Starting with Access 2007 you will be prompted about adding the new value to the list
Click Yes and the form you specified in the field properties will open up and you can go to a
84
INTERACT
WITH
DATA
new record and add the missing value. The value will be immediately available for use in the main form once you close the edit form.
2. When you get to the first missing Performer (Del McCoury), hit esc a couple times
6. Close the properties window 7. Repeat for the GenreID field, choosing frmGenre 8. Continue with the data entry 9. Close and save the form
85
INTERACT
WITH
DATA
Quick Search
1. Open frmCD in Form View 2. Click in the search box in the bottom navigation bar 3. Start typing in a word that appears in any field on any record in the form. As you
type the word the first record that has a match will be displayed. Ex. Where, or blue. Hit enter to move to the next matching record if there are more.
86
INTERACT
WITH
DATA
Filtering by Selection
1. Locate a record that has a Bluegrass CD 2. Place your cursor in the Genre field 3. On the Home tab, in the Sort and Filter section, click on Selection 4. Options will be displayed that will help you choose how to filter. Choose the Equals Bluegrass option
5. The form will show that it is Filtered and how many records it found that matches
The form now only shows the 2 records that have a bluegrass Genre type.
6. To remove the filter and see all records again click on Filtered and it will change
back to Unfiltered
87
INTERACT
WITH
DATA
A Popup will display all the options for that field 3. Clear all check boxes by unchecking the Select All option 4. Select only Bluegrass and Jazz
5. Click OK
The records showing will be anything with Bluegrass OR Jazz genre types.
6. To remove the filter, click on Filtered in the navigation area at the bottom of the
88
INTERACT
WITH
DATA
7.
8. From the Design menu Controls section click on the Combo box icon
9. Click in the form header section next to the CD label (leave enough room for the
89
INTERACT
WITH
DATA
11. Add the CDID, Title, and Year Released fields to the Selected fields
90
INTERACT
WITH
DATA
14. Use the Font Size and Bold options on the toolbar to make the Find CD label Bold
and 14 point
15. Expand the width of the Combo Box and the label
16. Switch to Form View and test the combo (lookup) box 17. The list currently is not sorted, to make this function work better we need to sort
by the CD Title (the field you would be typing a value for) Switch back to Design View Right click on the combo box and choose Properties On the Data Tab click in the Row Source field Click on the Ellipsis button at the end of the field
91
INTERACT
WITH
DATA
Choose Ascending in the sort row under Title Close and Save the Query
18. Switch to form view and search for a CD by typing in the start of a Title or drop the
92
INTERACT
WITH
DATA
From the Create tab, in the forms section choose Form Design
93
INTERACT
WITH
DATA
2.
3.
On the Data Tab, use the drop down for Record Source to choose tblMember (the table this form needs to be based on)
4. 5.
Close the Property Sheet From the Tools section choose Add Existing Fields
94
INTERACT
WITH
DATA
The field list is divided into 3 sections: Fields from the table you chose as the record source, fields from tables related directly to that table and the rest of the tables in the database. You can change the view to only show fields from the table you selected by clicking on at the bottom of the field list.
95
INTERACT
WITH
DATA
If you select records from a table other than your record source it will change to the record source to a query joining the appropriate tables. This may produce undesired results. You can return to seeing all tables by clicking on Show all tables at the bottom of the field list.
6.
7.
8.
96
INTERACT
WITH
DATA
9. Close and Save the form as frmMemberCD (you will come back to it later
to add a subform)
From the Create menu ribbon, in the forms section choose Form
Design
2.
Base the combo box on tblCD Select the fields CDID, Title and YearReleased (in that order) Sort by Title Save the selection to the field CDID Label as CD Widen the field
97
INTERACT
WITH
DATA
4. Create a combo box for the Loaned To field to the right of the CD field
Base the combo box on tblMember Select the fields MemberID, LastName and FirstName (in that order) Sort by LastName Save the selection to the field LoanedToID Label as Loaned To
Drag the DateOut field from the field list to the right of the Loaned To field
Click on the Property Sheet in the tools section Make sure the drop down says Form, if it is anything else change it to Form
Switch to the Format Tab Use the dropdown to change the default view from Single Form to
Continuous Forms
98
INTERACT
WITH
DATA
7. Close the properties window 8. Shrink the form up from the bottom so it is just bigger than the fields (or it will
3. In the subform Wizard, choose the existing form subfrmMemberCD 4. Link the form and subform by the common key field MemberID 5. Finish the subform wizard 6. Back at the form design view, delete the label above the subform 7. Expand the border of the subform downward to make room to show multiple
records
99
INTERACT
WITH
DATA
8. Save the design changes and switch to form view 9. Use the MemberCD form to loan a CD to another member in the club,
including filling in the Date Out field. (If there are no other members listed in the Loaned to dropdown, add another member through the Member form.) Did you notice?
We chose two fields for the Loaned To Combo Box, but only one showed up after you chose the member. When you drop down the item, you will see all the fields, but only the first field after the Primary Key ID will display once selected. If you would rather see the first name instead of the last, recreate the Combo Box, choosing the Primary Key first and then FirstName followed by LastName.
100
INTERACT
WITH
DATA
Date picker
The date picker is a new feature to Access 2007, it is a popup calendar you can use to enter a date into a field instead of manually typing in the numbers. The icon will show on forms and reports next to a date field currently being edited. It also has a handy Today button at the bottom to easily select the current date.
The Date Picker will not be available if there is an input mask on the date field. That is the case with our Date Out field. If we want to use the calendar to select a date for this field we have to go back to the table and remove the Input mask from the field.
1. Open tblMemberCD in design view 2. Select the DateOut field 3. On the General tab, clear out the value in the Input Mask 4. Close and save the table design changes 5. Try it out on the form
101
7
Lesson
Less on 7
Editing a Form
The Member CD form and subform we created in design view do not match the style of the forms we created using the automated tools. We can edit that form and assign a format to it and add a label in the header.
Modifying a Form
1. Open frmMemberCD in Design View 2. Go to the Arrange tab 3. In the Show/Hide section choose Form Header/Footer
103
LESSON 7: EDITING
A
FORM
5. Choose the Access 2007 format that we used in the prior exercises (if you hover
over the thumbnail it will give you the format name, it is the second one in the first row)
6.
7.
104
LESSON 7: EDITING
A
FORM
8. 9.
Drop the label in the header and type in Member CD List Change the font and size to the following:
10. Right
We should also lock out both the first name and last name fields from being edited. Members should only be added through the Member Form.
11. Select 12. Click
both fields at the same time by holding down your shift key. on Property sheet on the Design tab on the Data Tab of the Property sheet
If you set both Enabled to No and Locked to Yes, you will not be able to search on that field
14. Close and Save the design changes
105
LESSON 7: EDITING
A
FORM
106
8
Lesson
Less on 8
Queries
From this point use the CDClubForms database. After data has been entered using the forms, you can use queries to ask questions of your database. You can view and analyze fields from one table or join multiple tables together. A common reason for creating a Query is simply to join tables together to view related content. Queries are generally the basis of reports as well. Use the query to pull the fields and narrow down to the records desired, the report just formats the data for printing.
107
LESSON 8: QUERIES
4. Add Title and YearReleased fields to the selected fields area 5. Switch to tblGenre
6. Add the Genre field to the selected fields 7. Switch to tblPerformer 8. Add the Performer field to the selected fields 9. Switch to tblMember
108
LESSON 8: QUERIES
10. Add the FirstName and LastName fields to the selected fields
OOPS!
In order for Access to know how these tables are related, and to pull fields from several tables in this query wizard, the tables have to be set up in the relationship design. In this version of the database, one relationship has been left out.
12. Click OK and it will show the relationships window 13. Close the show table dialog box 14. Recreate the missing relationship between tblGenre and tblCD
109
LESSON 8: QUERIES
15. Close the relationship window and start over at Step 1 of this Exercise 16. Leave the default of Detail and click Next
110
LESSON 8: QUERIES
Be descriptive in the query and report names, you may end up with several that are similar and you will want to be able to easily identify the purpose of the query or report just by looking at the name of it.
111
LESSON 8: QUERIES
tblMemberCD is included in the query even though we did not select any fields from that table. We cannot make a connection directly from tblCD to tblMember. When we set up the relationships, we told Access that tblMemberCD is needed to join the two tables, so Access automatically added tblMemberCD into the query design.
2. Add a new field to the query to combine the Last and First Name fields into one
field called Owner. This field exists only within this query In the next blank column, type Owner :
The , *space+ between the fields leaves a comma and a space between the values so they do not run together)
Once you tab off the field, Access will add [ ] around each field name.
If you are working in a database that has spaces in the field names, you must put in the brackets manually, or Access will see each word as a field
3. Delete the original Name fields by hovering above the FirstName column until
you get a downward pointing arrow. Drag across to the Lastname column so both are highlighted. Then hit the Delete key
112
LESSON 8: QUERIES
4. View the query results. Note that the new column name is the name we gave the
field.
Queries are dynamic, meaning as the data changes, the query results will change. If you query for a list of all the CDs in the club database today, and then again in a month, any CDs added, edited, or removed in the next month will change the query results. If Prince changes his name, for example, it will be reflected in the new results.
6. Open the Performer form 7. Find the record for Prince 8. Change the value from Prince to Artist Formerly Known as Prince 9. Double click on qryCDListWithOwners to run the query
Note the name change is reflected in multiple records even though you only changed it once
Before
After
113
LESSON 8: QUERIES
3. Switch to tblGenre and add the Genre field 4. Switch to tblPerformer and add the Performer field Click Next 5. Leave the default of Detail Click Next 6. Name it qryCDMasterList 7. Click Finish and the Query will open showing the results
114
LESSON 8: QUERIES
3. Switch back to the query results view. Now the CDs will be in order by Title
End of Exercise
115
LESSON 8: QUERIES
Access will add quotation marks around all text fields Criteria must be spelled exactly as it appears in the data
Only CDs with the Bluegrass genre will appear 8. Return to Design View
116
LESSON 8: QUERIES
9. Add criteria for Year Released as >=2000 (greater than or equal to 2000) to
Greater Than Less Than Greater Than or Equal To Less Than or Equal To Equal To (understood, does not have to be specified in criteria) Not Equal To
117
LESSON 8: QUERIES
2. From the Show Table dialog box add the following tables: tblMember tblMemberCD tblCD tblPerformer
4. Add the following fields by dragging them into the query design grid
From tblMember:
FirstName LastName
From tblCD:
Title
From tblPerformer:
118
LESSON 8: QUERIES
Performer
From tblMemberCD:
LoanedToID DateOut
The results include ALL CDs. Add criteria to check if there is a value in DateOut in order to return only those CDs out on loan 6. Return to Design View 7. In the DateOut Criteria row type is not null. Null is equivalent to blank or
empty.
8. View the
119
LESSON 8: QUERIES
in the query.
3. Link MemberID in tblMember_1 to LoanedToID in tblMemberCD
4. Delete LoanedToID from the query design grid 5. Replace it with LastName from tblMember_1 6. Add Lent To: in front of the LastName field you just added to differentiate it
120
LESSON 8: QUERIES
On the Create tab, under the Other section click on Query Design
2. From the show table dialog box add tblMember, tblMemberAddress, and tblAddress Type 3. Add the Name fields from tblMember 4. Add AddressType from tblAddressType 5. From tblMemberAddress add the fields from Address1 thru Email
121
LESSON 8: QUERIES
Notice that you have more than one record for some Members. Even though they are only in the database once, they have more than one Address in the table. When you join the tables, it finds all the matches and displays them. 8.
End of Exercise
122
9
Lesson
Less on 9
Reports
Reports have the same dynamic properties that queries do. Reports are used to format data for printing. You can also use reports to create labels. Most reports are based on queries that pull information from multiple tables. More than one report can be generated from a single query (different sorting and layout options can be used and not every field from the query has to be pulled for the report).
123
LESSON 9: REPORTS
Touch Up Report
1. Switch to Design View 2. Edit the title to read CD Master List
3. The records should be sorted by Title. From the Grouping and Totals section of
124
LESSON 9: REPORTS
125
LESSON 9: REPORTS
Printing a report
1. Open rptCDMasterList by double clicking on it 2. Click on the Office button
3. Hover over Print and choose Print Preview to view the report first and make
4. If you are ready to print click on Print, otherwise click on Close Print Preview
126
LESSON 9: REPORTS
Touch Up Report
1. Switch to Design View and 2. Edit the title to read CDs On Loan 3. Put spaces in the field labels as needed 4. Make the Name fields smaller to make room for the rest of the fields. 5. Close and Save the report as rptCDOnLoan
127
LESSON 9: REPORTS
End of Exercise
128
LESSON 9: REPORTS
4. Click Next 5. View data by tblGenre (what we want to group by) Click Next
129
LESSON 9: REPORTS
130
LESSON 9: REPORTS
9. Choose the report style Access 2007 (to keep consistent style with the rest of
11. The report displays in Print Preview mode, Click on Close Print Preview to
go to design view
131
LESSON 9: REPORTS
14. Select
the Performer field and make it wider (dont go beyond the right border of the report)
15. View
the report
132
LESSON 9: REPORTS
133
LESSON 9: REPORTS
4. Click Next 5. View data by tblPerformer (what we want to group by) Click Next
134
LESSON 9: REPORTS
135
LESSON 9: REPORTS
9. Choose the report style Access 2007 (to keep consistent style with the rest of
10. Name the report rptCDMasterbyPerformer Click Finish 11. The report displays in Print Preview mode, Click on Close Print Preview to go
to design view
136
LESSON 9: REPORTS
13. Select
the Performer field and make it wider (dont let the fields go beyond the right border of the report)
14. View
the report
15. Close
End of Exercise
137
10
Lesson
Less on 10
Final Touches
Menu
To further enforce not entering data into the tables and to make the database as easy to navigate as possible, you can use the Switchboard wizard to create a menu system.
Creating a Switchboard
1. On the Database tools tab, under Database tools, choose Switchboard Manager 2. The first time you run it you will be prompted to create a new switchboard
139
4. Click Edit
5. Click New
6. Add the Text for the first Item: Enter Members 7. Choose Open Form in Add Mode in the Command line (this will only allow
9. Click OK 10. Click New to add the next menu item, this one will allow the user to view existing
members
140
141
13. Create the items for the three reports. Menu items for Reports are created the
same way, except you choose Open Report for the Command
If you try to add another item at this point you will get a message. Access does limit each switchboard to 8 items, but you can have multiple switchboards, one for forms, one for reports, etc.
14. Click OK, thats all we are going to add to the switchboard for now 15. Click Close on the 2 Switchboard screens
142
3.
143
5. You will be prompted to close and reopen the database to see the results of the
144
The database will be closed and reopened when it has completed the process.
145
Under the Office button choose Manage Then choose Back Up Database
3.
You will be prompted for a location and name, click Save when you are ready to run the backup.
146
147
A
Appendi x A
Appendix
4. Click Next
149
APPENDIX A: USING
THE
5. Choose a Font and Font Size (this can always be adjusted later)
6. Click Next 7. Add fields to the Prototype Label area by selecting them from the Available Fields section.
Note:
More than one field can be placed on a line. To add to another line, click in that line, hitting Enter will take you to the next step in the wizard. Add spaces where appropriate and a comma between City and State.
150
APPENDIX A: USING
THE
151
B
Appendi x B
Appendix
This will display a new line on the query grid labeled Total. The default value for each field is Group By. Group By is used to combine all like values of that field
153
APPENDIX B: CREATING
A
6. We want to group by Genre so leave the Group By value in the Genre field
If we leave Group By in the Value field as well and Preview the query, we would see the query shows each unique Value within each Genre, thats not what we are looking for
7. Change the Group By under the CDValue field to Sum using the drop down list
8. Right click on the CDValue column in the Query grid and choose Properties
154
APPENDIX B: CREATING
A
The new temporary name for the CDValue column is SumOfCDValue. We are now seeing a sum of the values of all CDs within each Genre
11. Return to Design View and add the Title field from tblCD 12. Change Group By to Count in the Total row under Title
155
APPENDIX B: CREATING
A
We now have a count of how many CD Titles exist under each Genre
14. Return to Design View and add the CDValue field three more times 15. Change one of each of the newly added CDValue fields to Avg, Min, and Max
Hint: you can leave the properties window open and click on another field to set its properties.
17. Preview the query results
1 8.
156
C
Appendi x C
Appendix
appendix)
2. When the query results are on the screen, from the External Data tab, choose Excel
3. You will be prompted for the file location and format 4. Choose to Export with formatting and to Open the destination file
5. Click ok and an Excel document will open showing the query data
157
You can also choose to save as Text, html, xml and other options.
7. When you return to Access you will see a screen asking if you want to save your
export steps
158
If this is something you will do on a regular basis and you want to automate it, click on the box. You will be given more options:
159
D
Appendi x D
Appendix
161
APPENDIX D: CREATING
A
THE
WIZARD
3. Add the following fields, in order LastName FirstName AddressType Address1 Address2 City State Zip
4. Click Next 5. View data by tblMember , click Next 6. No additional grouping, click Next 7. Sort by AddressType, click Next 8. Set layout as Stepped and Landscape, click Next
9. Choose the report style Access 2007, click Next 10. Name the report rptMemberAddress , click Finish
162
APPENDIX D: CREATING
A
THE
WIZARD
11. Close
Print Preview
12. Edit the report title and add spaces in the field labels, switch between design and
report view as needed Change the Title at the top of the report to Member Address List Edit the Names, Address Type, and Address labels (not field names) to add spaces between the words Shrink or expand fields as needed so data is not getting cut off (as much as possible)(dont let the fields go beyond the right border)
163
Appendix
E
Appendi x E
Exporting a Report
Reports in Access can easily be exported to Word for use as an email attachment.
1. In the Reports objects navigation pane, double click on rptCDMasterbyPerformer 2. Go to Print Preview using the Office button 3. In the Data section click on Word
You will be prompted for the file name and location to save the document. The report results will be exported to Word as an *.rtf (rich text format) file.
165
APPENDIX E: EXPORTING
A
REPORT
4. Click the option to open the destination file and Word will launch and display the
document. Notice that some formatting will not show up (e.g., lines and other graphical elements.) because the rtf format can only display text.
166