You are on page 1of 26

Microsoft Access 2007

Lesson 1: Creating and Editing a Database

Databases are collections of information that are organized so that you can easily find the
information that you need. It is fairly easy to retrieve information from small databases, such as
a phone book, which is organized alphabetically by last name. On the other hand, large
databases, such the iTunes catalog of over 5 million songs (July 31, 2007), are too massive to be
navigated by hand. Large databases require a separate computer program, called a database
management system (DBMS), to help manage and retrieve information. You are using a DBMS
whenever you use iTunes – searching tracks according to artist, album or genre and creating
playlists, for example. These lessons will introduce you to a DBMS that is accessible and fairly
easy to use, Microsoft Access.

Downloading Files for Lessons


The files you need for this lesson are located at the URL below:

http://webs.wofford.edu/whisnantdm/Training/Access_2007/Files_For_Lessons/

You will see a list of files. Right-click on the first link and choose Save Target As . . . in the
menu. Save the file to your H: drive. Repeat for the other files on the list.

Database Concepts
Access is structured in terms of tables that have rows and columns and look a lot like an Excel
worksheet. Columns in the table, which are called fields, identify the types of data that are stored
in the table – Last Name, First Name, Middle Initial, Street, City, and State in the example
shown below. Each row in the table, which is called a record, is a set of information about a
particular item in the database – a particular person in the example shown below.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 1
The data elements of a database at the intersection of a row (record) and a column (field) are
called values. For example, the value of the First Name field for the second record in the table
shown above is “Alfred.”

One of the fields, called the primary key, in a table uniquely identifies each of the records. In
the example shown above, the primary key is the ID. No two rows in a table can have the same
value in a primary key field.

Access is a relational database management system (RDBMS) that generally uses more than
one table to store information. Each table holds data that are logically inter-related. For example,
a college student database might have one table of personal information about students and
another showing the classes they are taking.

These two tables have a relationship created by the matching StudentID numbers. For example,
by matching the two StudentID numbers we can see that Bat Masterson took Psychology 104,
Humanities 100, Math 200, and Religion 202 in the 200409 term.

If a database contains a large number of fields, a one-table database will be very large and
unwieldy. Grouping data in multiple tables usually allows it to be maintained and manipulated
more efficiently.

NOTE: You will find these lessons interspersed with Questions and
Practice Problems. These are integral parts of the lessons and should be
done at the time you read them rather than later.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 2
In Class Question: Suppose you were storing the data on page 2 in one table, like the
one below, rather than two.

Would this lead to more work entering data and an increased chance of making
errors? (Hint: Think about Alfred E. Neumann, for whom class data is shown in both
the 200409 and 200502 terms.) What kind of duplication will be required when you
enter a record for one term and then for another? How would you store data for a
student taking five classes in a semester?

Searching in several smaller tables also is more efficient because it is easy to create queries that
can pull information from multiple tables.

A Database Example
Let’s assume that your hobby is collecting old 45 rpm records from the classic rock era of the
50s and 60s. You decide to create a database to help you manage your collection. We will create
an Access database to store the following information about your collection:

Label No. Artist A-side B-side Condition Price


Swan 4152 Beatles She Loves You I’ll Get You Excellent $150.00
Swan 4152 Beatles She Loves You I’ll Get You Fair $ 75.00
Swan 4152 Beatles She Loves You I’ll Get You Poor $ 10.00
Capitol 5555 Beatles We Can Work It Day Tripper Good $ 50.00
Out
Soma 1137 Fendermen Muleskinner Torture Good $8.50
Blues
Soma 1137 Fendermen Muleskinner Torture Fair $5.00
Blues
Decca 29791 Haley, Bill and See You Later The Paper Excellent $12.00
the Comets Alligator Boy
Mercury 71032 Platters I’m Sorry He’s Mine Fair $15.00
Volt 157 Redding, Otis (Sittin’ On) The Sweet Lorene Good $6.50
Dock of the Bay

Notice the duplication in several fields in this table (for example, in the first three Beatles
records). To remove the duplications, we will construct the database with more than one table. In
this case we will use two tables, one to store information about each 45 record and one to store
information about the copies of that record that you have... Recall that each table must have a
primary key field. We will add unique IDs as primary keys in the two tables. ID45 will identify a
45 rpm record that was issued. IDSpecific will identify a specific copy you own. Instead of
storing a word for the condition, we will use one letter (e.g., “E” for “Excellent”) to specify the
condition, which will make data entry easier.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 3
tblIssue
ID45 Label No. Artist A-side B-side
1 Swan 4152 Beatles She Loves You I’ll Get You
2 Capitol 5555 Beatles We Can Work It Out Day Tripper
3 Soma 1137 Fendermen Muleskinner Blues Torture
4 Decca 29791 Haley, Bill and the Comets See You Later Alligator The Paper Boy
5 Mercury 71032 Platters I’m Sorry He’s Mine
6 Volt 157 Redding, Otis (Sittin’ On) The Dock of the Bay Sweet Lorene

tblValue
IDSpecific ID45 Condition Price
1 1 E $150.00
2 1 F $ 75.00
3 1 P $ 10.00
4 2 G $ 50.00
5 3 G $8.50
6 3 F $5.00
7 4 E $12.00
8 5 F $15.00
9 6 G $6.50

In Class Question: Each row in the tblValue table is associated with a row in the
tblIssue table. What field in each table establishes the relationship between the two
tables?

Opening Access
Having figured out the tables we need, we are ready to create our database. Open Microsoft
Office Access.

When Access starts you will


see a screen that looks
something like this one.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 4
Creating a New Database
Choose Blank database from the New File menu at the right.

In the Blank Database section that appears on the right side of the
screen, click on the Folder icon to choose where you want to save
the database.

Navigate to your H: drive and choose


the name “Records” for your database.

Click on OK.

You should see the File Name Records.accdb, with H:\ below it.
Click on Create.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 5
Creating Tables in Design View
First we will create a table with the field names shown below.

ID45 Label No. Artist A-side B-side

Click on the Design View button.

Name the table tblIssue.

You now should see the Design View of the table – the view we use when we want to work on
the structure of the database, as opposed to the data stored in the database. The Design View
allows you to enter names of the fields in the table and to specify their data types. A data type
indicates the kind of data to be stored—for example, numbers, text, or dates. It is best to select
the type that most closely matches the kinds of values you are storing in a field.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 6
When you design a table, the first thing you do is to specify the Field Names and the Data
Types – the type of data that is allowable for each field. A few common data types are listed
below:

Data Type Allowable Field Values Number of Characters


Text (the default data type) Letters and numbers Up to 255
Number Numbers Up to 64,000
AutoNumber Numbers in sequence that are
automatically created by Access
Currency Numbers
Date and time Numbers

The first Field Name we will enter is ID45, the field we


will use as the primary key to uniquely identify each
record. Enter “ID45” as the first field name. Its Data
Type should be AutoNumber.

The new field probably will be designated as the primary key for the
table – see if there is a small key icon to the left of the field name.

If the ID45 field has not been designated as the primary key, right-
click on the Field Name. In the menu that pops up, select Primary
Key.

Enter “Label” as the second field name. The default


Data Type is Text, which is appropriate for this field.

You will see a list of Field Properties at the bottom of


the Design View window. The default Field Size for a
Text field is 255, which is much bigger than a record
label name is ever going to be. Change the Field Size to
20.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 7
Enter the other Field Names as
shown at the right. Use 100 as the
lengths for Artist, A_Side and
B_Side.

When you have entered them, click


on the Save icon to save the table.

In Class Practice: Creating as Second Table

Select the Create tab and click on Table.

• Name the table “tblValue”


• Add the four fields shown on page 4 for the table
labeled “tblValue”
o Choose the appropriate one of these fields to
be the primary key
• Assign a Data Type appropriate for each field. The Data Type for the “Price” field should
be Currency.

Adding Data to a Table


Now that the tables have been created we
need to populate them with data. We will
start with tblIssue. Select the “tblIssue” tab
and choose Datasheet View from the View
menu.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 8
This will display the Datasheet View of the table – the view in which you add data to the table.
This view is reminiscent of a spreadsheet. Each column is labeled by a field name.

The Home tab should be selected. If it is not, click on the Home tab to select it.

Enter the data in the table below into the database. Don’t type anything in the ID45 field, which
is autonumbered. Every time you make an entry of a new Label, the next number will
automatically be added to the corresponding ID45 cell. When you have entered the data for one
cell, pressing the Tab key will move to the next cell on the right.

ID45 Label No. Artist A-side B-side


1 Swan 4152 Beatles She Loves You I’ll Get You
2 Capitol 5555 Beatles We Can Work It Out Day Tripper
3 Soma 1137 Fendermen Muleskinner Blues Torture
4 Decca 29791 Haley, Bill and the Comets See You Later Alligator The Paper Boy
5 Mercury 71032 Platters I’m Sorry He’s Mine
6 Volt 157 Redding, Otis (Sittin’ On) The Dock of the Bay Sweet Lorene

When you have entered the


data, you may find that the
column widths do not match
the data in the fields. For
example, the Label column is
consistently wider than the
Label names that have been
entered. On the other hand the
columns for the Artists’ names
and song titles are too narrow.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 9
Using the same methods as for spreadsheet columns, either drag the column borders or double-
click on them to change the column widths to match the data.

Click on the Office Button and Save the file.

Select the tblValue tab and move to Datasheet View. If you


are asked whether you want to save the table, choose “Yes.”

Add the data shown at the right to the table. The contents of the
IDSpecific field should be autonumbered and cannot be entered.

Enter “E” for Excellent, “G” for Good, and so forth. Do not
type the dollar sign for the monetary values – because the Data
Type for this field is Currency, the dollar sign should be
displayed automatically,

Save the file.

Right-click on the tblValue tab and close the table.

Editing Data
You can change the values stored in the database at any time. Suppose you decide you want the
name of the Otis Redding A-side to be simply “The Dock of the Bay,” rather than “(Sittin’ On)
The Dock of the Bay.”

In the tblIssue table, click on the cell containing “(Sittin’ On) The Dock of the Bay.” Delete the
characters you do not want.

Save the table when you are finished.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 10
Adding a Field
Even if you have planned carefully in advance, it is not unusual to find you need to add a field
once you actually start using the database. In our example, after using the Records database for a
while, you find that you need a field to show that date on which you purchased each specific
recording. We will add this field to the tblValue table.

Open tblValue by double-clicking on the table


name in the list of tables.

In the Design View, enter a field with name


“DatePurchased” and Date/Time type. Note that
there is no space between the two words in
“DatePurchased.”

Save the table.

Move to the Datasheet View. This view will


display the contents of the table, including the
new DatePurchased field, which currently
contains no data.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 11
Setting Field Properties
Before entering data in
the new field, we will set
a few Field Properties.

Return to the Design


View and select the
Condition field name.

Enter the Description


and Field Properties
shown here. We will
describe the function of
these entries below.

Save the table. When you


do, you will be notified
that a field size has
changed and some data
will be lost. This is
because we reduced the field size of the Condition field. Don’t worry about it because the change
will have no effect on the data in the field.

Field Description
The Field Description gives
the user information about
what kind of data may be
entered in that field.

In the Datasheet view, when


you are entering data, the
Field Description is displayed
at the bottom of the Access
window, as shown in the
illustration at the right.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 12
Field Properties: Format Property
The Format property specifies the way that data is displayed. It does not change the way that
the data actually is stored in the database – just how it is displayed. A few format examples are
given in the table below:

Format Symbol Data Display Example


> All characters displayed as uppercase If a field is formatted with “>”, data entered
as “e” would be displayed as “E”
< All characters displayed as lowercase If a field is formatted with “<”, data entered
as “G” would be displayed as “g”
@ Every “@” requires a text character or If a field is formatted with “(@@@) @@@-
space to be entered. @@@@, data entered as 8641234567 would
be displayed as (864) 123-4567. This is
useful for phone numbers, for instance.

Choosing the Format = “>” for the Condition field means that all entries in that field will be
displayed as uppercase. Then you don’t have to worry so much about whether you enter “E” or
‘e’ as the condition.

For some data types, you can choose a format property


from a list. For example, move to the DatePurchased field,
which has a Date/Time Data Type.

If you click on the white rectangle next to Format, you will


see an icon appear.

Clicking on this icon will display a list of date formats from


which to choose. For our example, choose Medium Date,
which will display the dates like “19-Jun-07.”

Go into Datasheet View and enter dates in the empty fields.


Notice that dates always are displayed in Medium Date
format (e.g., 19-Jun-07) even if you enter the date in
another format (e.g., 6/19/07).

Save the file and return to the Design View.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 13
Field Properties: Validation Rule and Text
Databases often include Validation Rules that limit the input that is possible in a field. When
you make an entry in a field, Access checks your entry against the validation rule (if any) that
has been set up for that field. If the entry agrees with the rule, then everything is OK. If the entry
violates the validation rule, an error message will be displayed showing the validation text.

For example, the only entries possible in the


Conditions field are E, G, F, or P. The validation rule
shown at the right limits entries to these four letters.

In Class Practice: In Design View, enter the validation rule shown above
• Go to the Datasheet View of the table and try to enter “B” for “Bad” as a
Condition.
• Does “B” satisfy the Validation Rule set up for this field?
• What role does the validation text play when you enter “B”?

Moving a Field
After using the database for a while, you may decide to change the position of one of the
columns or to remove it entirely from the table. Suppose, for example, that you want to move the
DatePurchased column of tblValue two columns to the left so that it is between the ID45 and
Condition columns.

Move to the Datasheet View and


select the Home tab. Then click on
the Field Label (DatePurchased) so
that the entire column is selected.

“Grab” the column by clicking on


the Field Label and holding the left
mouse key down. Drag the column
to the left until a solid line is
displayed where you want to drop
the column.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 14
Drop the column in the new
position by releasing the mouse
button.

Deleting a Field
If you find that you never use a field in the database, you may decide to delete it. This only
should be done after giving the deletion careful thought, because deleting a field deletes this
information from every record in the database. Always back up your database before deleting
a field in case you want to restore it later. You cannot use Edit, Undo to undo the deletion of a
field.

Let’s delete the


DatePurchased field from
tblValue. Move to the
Datasheet View and select
the Home tab.

Select the entire column by


clicking on the Field Label.

Click on Delete icon.

Access will ask you if you really


want to do something this drastic.
Click on Yes to delete the field.

Save the database.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 15
Deleting a Record
At some point, you probably will want to remove one or more records from the database – if, for
example, you sell one of the 45s in your collection. Once a record has been deleted it cannot be
restored without re-entering the data. Always back up your database before deleting a record
in case you want to restore it later.

Open tblIssue in the Datasheet view. Select the Home tab.

Suppose that you have sold


the Platters record and
want to remove it from the
database.

Click on the far left of this


record to select the entire
record. Then click on the
Delete icon.

You will be asked to confirm that you really want to do


this.

Click on Yes.

Note: as an alternative to deleting a record for a 45 you no longer have, you could add a field
that designates the date a record is sold. If a sold-date value is present in a row, then that
record has been sold. If a sold-date value is null (not present), then the 45 has not been sold
yet. This design lets you keep a history of your collection. Consider such alternatives when you
design a database.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 16
Print Preview and Printing a Page

Open tblIssue in Datasheet View. To preview


what the printed version of the table will look
like, click on the Office Button and then hold the
cursor over Print for a second or two. When a
Print menu appears, click on Print Preview.

You probably will see


something like the
picture at the right.

Note that the entire table


does not fit on the page
because the orientation
of the page is set to
Portrait. We need to
change the page layout
to Landscape.

In the Page Layout


section, select
Landscape orientation.

This will orient the page


so that the entire table
will be displayed on one
page.

Although we will not do it in this lesson, you could now choose Print on the Office Button to
print out a hardcopy of the table.

Save and Close the Records database.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 17
More About Field Properties
Click on the Office Button and 0pen the database named “ExampleDB” that you downloaded
into your H: drive.

You will see


that this
database has
only one table,
tblEmployees.

Open this table.

Change to Design View and set the text Field Sizes to those shown below:

• LastName 25
• FirstName 25
• MI 1
• Street 25
• City 25
• State 2

Save the database.

Because you have decreased some of the field sizes, you will be warned that data might be lost.
The field sizes above are more than sufficient to hold the data, so do not be concerned about this
warning.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 18
Field Properties: Default Value
In this table, we see that most of the
states are SC. Choose the Default
Value field property for State and
enter “SC”. When a default value,
such as “SC,” is set this value will
automatically be entered when a new
record is created. It can be changed
if need be.

Also set State’s Format property to


“>”.

Finally enter the Description “Enter


the two-letter abbreviation” for the
State field.

In Class Question: What does setting the format property to “>” do?

Field Properties: Input Mask


An Input Mask lets you designate the way data may be entered for a particular field, thereby
reducing the probability of data input errors. The Input Mask property is different from the
Format property.

• The Input Mask property limits the way that data can be entered and hence defines the
way that data actually exists in a table.
• The Format property limits the way that data is displayed – not how it is stored.

Let’s use an input mask to designate


how phone numbers may exist in
the table. In Design View, select the
Home Phone field and click on the
Input Mask property.

You will see a small button with


three dots displayed at the right of
the Input Mask row. Click on this
button.

You will be asked if you want to


save the table first. Click on Yes.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 19
In the Input Mask Wizard window, select the Phone
Number input mask. The appearance that this mask
will give to the data is shown in the Data Look
column. Using this input mask, phone numbers will
look like (206) 555-1212.

Click on Next.

In the following window, you will be asked if you


want to change anything. Let’s not change anything.
Click on Next again.

You next will be asked if you want the stored data to


include the symbols in the mask. This is useful in the case
of phone numbers, so let’s do it. Select the top option.

Click on Next and then Finish.

The expression for this Input Mask now is displayed.

If you Save the database and look back at the Datasheet View, you will see that the phone
numbers now are stored in the format specified by the Input Mask.

Enter a new record or two in the table to see how the Input Mask works.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 20
Field Properties: Captions
It is useful to give fields names that do not include spaces. Although Access allows field names
with spaces, not all DBMS do. It also is easier to write SQL statements if you don’t include
spaces. On the other hand, people are accustomed to seeing spaces between words, so we would
like to label columns in tables with spaces included. We can do this by defining captions for
fields.

Define a caption for fields that need spaces between


words. For example the field “LastName” has no
spaces between the two words. We can choose its
caption to be two words: “Last Name.”

If you look at the Datasheet View, you will see the


captions displayed at the top of the columns rather
than the actual field names.

In Class Review: Creating a Table


Create a new table in the example database named tblDepartments, which contains two fields.
The first field, the primary key, will contain numbers. Name this field “DeptID.” Note that the
Dept ID field is a Number field and is not autonumbered.

The second field, named “DeptName,” will contain text. Make the entries shown below in the
table. Save the table.

DeptID DeptName
100 Purchasing
101 Finance
102 Shipping
103 Sales
104 Personnel
105 Marketing
106 Security
107 Engineering
108 Communications
109 Information Technology

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 21
Importing Records into a Database
When creating a database, it may be that some of the data you need already exists – in an Excel
spreadsheet, for example. If this is the case, you probably will want to import the existing data
into your database to avoid entering it again by hand.

In our example, information about salaries and whether employees are exempt from receiving
overtime pay is stored in an Excel spreadsheet named Salaries, which you downloaded into your
H: drive. We want to create a new table in the ExampleDB database from the Excel spreadsheet.

Close all tables in ExampleDB.

Select the External Data tab and


click on the Excel button.

Select “Import the


source data into a new
table in the current
database.”

Click on Browse. Find


the spreadsheet file
named Salaries located
on your H: drive.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 22
The spreadsheet has three worksheet
tabs, so Access will ask you which
one you want to use. We will use
the first sheet, which is the only one
that contains data.

Click on Next.

You will be asked if you want to use the


Excel column headings as Field Names
in the table you create. In this example,
we do.

Click on Next.

We will use the Employee ID field as a


Primary Key, so we do not want it to
have any duplicates. Choose Yes (No
Duplicates) in the Indexed drop-down
box.

Click on Next.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 23
Choose Employee ID as the Primary
Key for the table.

Click on Next.

Finally, enter the name of the table – let’s call it


tblSalaries.

Click on Finish.

You will be asked if you want


to save the import steps.
Generally you will not.

Do NOT check the “Save


import steps” box and click on
Close.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 24
You now will see tblSalaries
included in the list of tables
in the database.

Finally, add captions to the Field Properties of tblDepartments and tblSalaries as needed.

Save the database.

Exercises (To be done outside of class.)


1. What is the difference between the Datasheet and Design Views in Access? What do you
use the Design View for? What about the Datasheet View? Where is the icon on the Access
toolbar that toggles you back and forth between these two views?

2. What is a primary key in a table? Why is it necessary?

3. How is a RDBMS different from a DBMS?

4. What is the purpose of the Autonumber format?

5. What is the difference between a field description and a validation rule? How is each used
in Access?

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 25
6. Look at the table of data below:

City State Office Phone Staff


Chicago IL Cicero (773) 613-4228 Bill Jones
Chicago IL Cicero (773) 613-4228 Jill Kirby
Chicago IL Cicero (773) 613-4228 Jon Gaff
Chicago IL Oak Park (773) 888-1943 Mary Arthur
Chicago IL Oak Park (773) 888-1943 Virginia Dawes
Chicago IL Oak Park (773) 888-1943 Alfred Prufrock
Chicago IL Berwyn (773) 762-3333 Tillie Mays
Chicago IL Berwyn (773) 762-3333 John Illkie
Chicago IL Berwyn (773) 762-3333 Kirby Puck
Chicago IL Addison (773) 784-3276 Jonica Kinsey
Chicago IL Addison (773) 784-3276 Bubba Ames
Chicago IL Addison (773) 784-3276 Chris Armstrong
Atlanta GA Doraville (404) 749-1885 Otto Engine
Atlanta GA Doraville (404) 749-1885 Frank Hucks
Atlanta GA Doraville (404) 749-1885 Linda Dyson
Atlanta GA Roswell (404) 559-1144 Juanita Masters
Atlanta GA Roswell (404) 559-1144 Aaron James
Atlanta GA Roswell (404) 559-1144 David Demster
Atlanta GA Downtown (404) 622-6434 R. K. Lings
Atlanta GA Downtown (404) 622-6434 Reba Newling
Atlanta GA Downtown (404) 622-6434 Watts Yterby

How many Access tables would you set up to store this data efficiently? Decide on the fields
you would use in each table. What would be the primary key in each table? What would be the
relationships that link the tables?

7. You enter “Elmer Fudd” in a field that has the “<” Format. In what form will this name be
displayed on the screen? In what form will it actually exist in the database?

8. You enter “Elmer Fudd” in a field that has the “<” Input Mask. In what form will this
name be displayed on the screen? In what form will it actually exist in the database?

9. Define a format mask for a field that contains a nine-digit phone number so that the value
displays as 999.999.9999.

Access 2007 Lesson 01: Creating and Editing a Database


10/29/2009 26

You might also like