Вы находитесь на странице: 1из 45

Microsoft Office

Access 2007
Tutorial

Sandra Dyke, 2011

Access 2007 Tutorial

Contents Page
Introduction ........................................................................................................................... 1
Lets Get Started ................................................................................................................... 2
Creating Tables..................................................................................................................... 3
Creating Relationships and Enforcing Referential Integrity .................................................... 8
Adding Data ........................................................................................................................ 12
Creating Queries ................................................................................................................. 16
Reports ............................................................................................................................... 25
Forms ................................................................................................................................. 35
Creating a Switchboard ....................................................................................................... 39
General Design Considerations .......................................................................................... 43
Useful Resources................................................................................................................ 43

Sandra Dyke, 2011

Access 2007 Tutorial

Introduction
This tutorial is an introduction to using Microsoft Access 2007. In this tutorial a database
with 4 tables will be created, data added to these tables, queries created to obtain
information from the database, and reports to display information from the database.
The database being constructed is for a basic sales system and contains the following tables
and attributes with the stated data types and sizes:
Table
Customer

Inventory

Employee

Sale

Attribute
CustomerID
CustomerFirstName
CustomerLastName
CustomerAddress
CustomerSuburb
CustomerCity
CustomerPostCode
CustomerPhoneNumber

Data Type
AutoNumber
Text
Text
Text
Text
Text
Text
Text

Size
50
50
50
50
50
4
15

Required
Y
Y
Y
Y
N
Y
N
N

InventoryID
InventoryName
InventoryDescription

AutoNumber
Text
Text

50
255

Y
Y
N

EmployeeID
EmployeeFirstName
EmployeeLastName
EmployeeExtension

AutoNumber
Text
Text
Text

50
50
4

Y
Y
Y
N

SaleID
CustomerID
InventoryID
EmployeeID
SaleDate
SaleQuantity
UnitSalePrice

AutoNumber
Number
Number
Number
DateTime
Number
Currency

Y
LongInteger Y
LongInteger Y
LongInteger Y
Y
Integer
Y
Y

Notice that the names of each attribute are typed with no spaces between the words, but
each new word starts with a capital. This is called CamelCase. Dont use spaces in field
names because different database programmes treat these differently and it can also cause
problems with creating queries later.
Also notice that each attribute name has the table name in front of it. This is not always
done, but is useful when the same field name may be used for different tables. For example,
this database also has an Employee table where the employee name is included. By
including the table name in front of the field name, it is easier to determine which field is
being referred to when using queries and reports.

Sandra Dyke, 2011

Access 2007 Tutorial

Lets Get Started


Open Microsoft Access and select Blank Database

Give the database a suitable name BasicSalesSystem and choose a folder to save it to by
clicking the folder icon.
Click Create

This creates the database and opens the default window.

Sandra Dyke, 2011

Access 2007 Tutorial

Creating Tables
Steps for creating a Table
1. Click Create Table Design
2. Enter Field names, data types, and descriptions (optional)
3. Enter properties for fields
4. Set the primary key(s)
5. Save the table design (and name the table)
Creating the Customer Table
Select the Create Tab and Click the Table Design button

This opens the Design view for the table

The first table to be created is for Customer


In the first Field Name type CustomerID, change the data type to AutoNumber, add a
description, and in the Field Properties section, change Indexed to Yes (no duplicates)
using the dropdown arrow

Sandra Dyke, 2011

Access 2007 Tutorial

The CustomerID is going to be the Primary Key, so with the CustomerID field selected, click
the Primary Key button on the ribbon. Notice the key which now appears next to
CustomerID

Move down to the next row, and type CustomerFirstName in the field name cell, choose Text
as the data type, add a suitable description.

In the Field Properties Box, change the Field Size to 50, and Required to Yes

Sandra Dyke, 2011

Access 2007 Tutorial

Now add the rest of the Attributes with the following properties
Table
Customer

Attribute
CustomerID
CustomerFirstName
CustomerLastName
CustomerAddress
CustomerSuburb
CustomerCity
CustomerPostCode
CustomerPhoneNumber

Data Type
AutoNumber
Text
Text
Text
Text
Text
Text
Text

Size
50
50
50
50
50
4
15

Required
Y
Y
Y
Y
N
Y
N
N

To save the table it needs to be closed.


Click the Close button

Sandra Dyke, 2011

Access 2007 Tutorial

Click Yes to save the table


Then name your table

Now there is a table called Customer

Now create tables for:


Inventory (primary key InventoryID)

Employee (primary key EmployeeID)

Sandra Dyke, 2011

Access 2007 Tutorial

Sale (primary key SaleID)


Sale date should be formatted as Medium Date in Field Properties

Sandra Dyke, 2011

Access 2007 Tutorial

Creating Relationships and Enforcing Referential Integrity


One of the most common types of database used for commercial purposes is the relational
database. This type of database stores data in related tables, and if well designed, reduces
the possibilities of data anomalies by storing a data item the minimum number of times
required. This usually means a data item is only stored in one table in the database but can
be accessed by other tables using the primary/foreign key link.
To create the relationships between the tables requires a link to be created between the
primary key in one table and the same data item in the other table.

Table
Customer
Inventory
Employee

Primary Key
Attribute
CustomerID
InventoryID
EmployeeID

Table
Sale
Sale
Sale

Foreign Key
Attribute
CustomerID
InventoryID
EmployeeID

The Primary/Foreign key link allows any of the information in the primary keys table to be
accessed via the link. For example, because the Sale table has the CustomerID in it, a
query run across the Sale and Customer tables will allow the customer name to be displayed
with the details of the sale. More about this later.
One of the advantages of the relational database design is to ensure that only valid data can
be entered for the foreign key data items. This is done by enforcing referential integrity
which means that before a value can be entered into the foreign key attribute, it must already
exist in the table containing the primary key. This means when entering data, care has to be
taken about the order the data is entered: First enter data in the primary key table and then
enter the data in the foreign key table. For this tutorial database, data will need to be added
to the Customer, Inventory and Employee tables before anything can be added to the Sale
table.
Click on Database Tools  Relationships

Sandra Dyke, 2011

Access 2007 Tutorial

Add all 4 tables in the Show Table dialogue box

Rearrange and resize the tables so all attributes are shown

To create the relationship, click and drag the primary key attribute to the appropriate attribute
in the related table:
Click and Drag CustomerID from the Customer table to the CustomerID in the Sale table

Sandra Dyke, 2011

Access 2007 Tutorial

This opens the Edit Relationships dialogue box.


Tick Enforce Referential Integrity
Notice that the Relationship Type is One-To-Many, This means one CustomerID could relate
to many Sale records.
Click Create
Notice the link which has now been created between the customer table and the Sale table.
The 1 at the Customer table end means 1 customer record could be related to many () sale
records.

Add the rest of the relationships.

Sandra Dyke, 2011

10

Access 2007 Tutorial

Close the relationship diagram, saving it when requested to.

Sandra Dyke, 2011

11

Access 2007 Tutorial

Adding Data
Data can be added using the Datasheet View mode or by using a form (covered later).
Open the Customer table in View mode (Datasheet View)

Click on CustomerFirstName and type Bill, tab to CustomerLastName and type Smith etc.
The CustomerID does not need to be typed because it is an AutoNumber and is
automatically included by Access.
Type in the rest of the customer records.
Do the same thing for the other tables.

Sandra Dyke, 2011

12

Access 2007 Tutorial

Table
Customer

Attribute

CustomerID

CustomerFirstName

CustomerLastName

CustomerAddress

1
2
3
4
5

Bill
Bob
Jane
Sue
Fred

Smith
Brown
Green
Cross
Brown

123 Dove Street Hillside


43 Sweet Road Riverside
PO Box 780
PO Box 8249
9 Castle Grove

CustomerSuburb

InventoryID

InventoryName

InventoryDescription

1
2
3
4
5

Hammer
Screwdriver
Saw
Spanner
Pliers

CustomerCity

CustomerPostCode

CustomerPhoneNumber

Nelson
Wellington
Auckland
Nelson
Picton

7055
6986
1436
7073
7125

03123456
0274567898
04573953
03850345
0277684930

Inventory
Phillips

Employee
EmployeeID

EmployeeFirstName

EmployeeLastName

EmployeeExtension

1
2
3
4
5

Joy
Jack
Gary
Gail
Lee

Smith
Trigger
Wood
Smith
Drake

123
124
125
126

Sale
SaleID

CustomerID

InventoryID

EmployeeID

SaleDate

SaleQuantity

UnitSalePrice

1
2
3
4
5

1
1
2
4
3

1
3
2
4
4

2
5
1
4
2

12/1/09
6/2/09
14/2/09
28/2/09
12/3/09

3
1
4
1
5

15.99
17.99
6.95
9.99
9.99

Sandra Dyke, 2011

13

Access 2007 Tutorial

Customer Table

Inventory Table

Employee Table

Sale Table

Sandra Dyke, 2011

14

Access 2007 Tutorial

Effect of enforcing referential integrity


Remember the discussion about referential integrity? What happens if an incorrect value is
included in the Sale table for CustomerID, InventoryID or EmployeeID?
If the value included in one of the above fields isnt in the appropriate primary table, Access
will not let the record be added or changed in the table until it is fixed.

Sandra Dyke, 2011

15

Access 2007 Tutorial

Creating Queries
Now that the data has been added to the tables, it can be used to produce results related to
a set of conditions given to it. This is known as queries. Queries can be from one table or
from a number of tables.
Queries
Select Create  Query Design

Steps
1.
2.
3.
4.
5.
6.

Select the required table(s)


Select the fields required
Include any conditions
Select display requirements
Run Query
Save Query with a suitable name by closing it, or clicking the Save Button at the top
of the Access screen

Important: When creating queries check that the results seem to be reasonable. When
there is only a small dataset, look at the data and determine what the results should be
before running the query and checking that the results obtained are the same.

Sandra Dyke, 2011

16

Access 2007 Tutorial

Query 1: Single Table Query


Create a Customer Phone List
This query will produce a list of customer first name, last name and phone number.
Select the Customer table from the Show Table Dialogue and click Add

Select the CustomerFirstName, CustomerLastName and CustomerPhoneNumber by using


the dropdown arrow in the Field row, or by dragging the attribute from the Customer table
shown.

Everything is going to be shown so nothing further is required.

Sandra Dyke, 2011

17

Access 2007 Tutorial

Run the query by clicking Run

Result

Lets sort this query by the customers last name.


Change the query to Design View mode and select Ascending for CustomerLastName

Run the Query again

Now the results are sorted by the customers last name


Close the query and give it a suitable name when prompted (eg qCustomerPhoneList).

Sandra Dyke, 2011

18

Access 2007 Tutorial

Query 2: Multi-table Query


Create a list of customer names and the dates they have purchased items.
This is a query across 2 tables: Customer and Sales
Click Create  Query Design
Add Customer and Sales
Notice that this time, the relationship between the tables is also shown (a customer may
have many sales, but a sale can only relate to one customer)

Select CustomerFirstName, CustomerLastName from the Customer table and the SaleDate
from the Sale table. Sort by the sale date.

Result

Save this query with a suitable name (eg qCustomerSaleDate).


Both queries so far have returned all of the records from the tables selected. It is possible to
select only some of the records by using the Criteria section.

Sandra Dyke, 2011

19

Access 2007 Tutorial

Adjust the query by showing only Bill Smiths sales by including Bill in Customer FirstName
and Smith in CustomerLastName.

Result

Change the query to show only those sales which occurred in February 2009
This can be done by typing >= 1/2/09 And <= 28/2/09 in the SaleDate criteria. When the
query is run, Access adds the # around each date.

Result

Sandra Dyke, 2011

20

Access 2007 Tutorial

Query 3: Multi-table Query


Create a query which shows the Employee Name, Date, Quantity, and Inventory Name for
sales for each Employee. Sort by Employee Last Name
This query is across 3 tables: Employee, Inventory and Sale.

Result

Save the query with a suitable name.


Adjust the query to show only the sales which Gail Smith has made.

Sandra Dyke, 2011

21

Access 2007 Tutorial

Result

Experiment with other conditions.


Query 4: Multi-table Query
Create a query which shows the customer name, address, Inventory Name, Date, Quantity,
Price, and Employee Name for each sale. Sort by customer last name.

Result

Save the query with a suitable name (qCustomerSale).


Experiment with different conditions.
Query 5: Adding Calculations
The price shown in the Sale table represents the unit price for the items sold.
It is possible to add the total price for the sale to the record by using a query. Values which
can be calculated from the other data in the table are generally not included within the table
due to the normalisation rules and to ensure an efficient database.

Sandra Dyke, 2011

22

Access 2007 Tutorial

Open query 4 (qCustomerSale) and rename it by going to Save As (qCustomerSaleTotal).

Format Column Name: Table.Attribute * Table.Attribute


In the first empty cell type TotalSalePrice: Sale.SaleQuantity*Sale.UnitSalePrice
Run the query
Result

Once the query has been run, the calculation will show [ ] around the table and attribute
names:

Sandra Dyke, 2011

23

Access 2007 Tutorial

Query 6: Joining 2 text fields together


A normalised database will usually have the customer name and address in more than 1
field eg CustomerFirstName,CustomerLastName, CustomerAddress, CustomerSuburb,
CustomerCity and CustomerPostCode. However, when these attributes are used, usually
the first and last name will need to be together as will the city and postcode. Because the
length of text contained in these fields could be different lengths, it is easier to join the fields
together as a new attribute and then use these new fields. Text fields can be joined together
using the & operator and spaces can be introduced by using (double quote space double
quote).
Create a simple query using all of the fields from the Customer table and save it with a
suitable name eg qCustomerDetails:

Switch into Design View


In the first empty cell type:
CustomerName:CustomerFirstName & & CustomerLastName

Run the Query


Result

Try combining city and postcode into 1 field.


To really see the effect of this joining text fields, create a report (complete the next section
first) which shows customer addresses using the separates CustomerFirstName,
CustomerLastName attributes as well as the joined attribute CustomerName and see the
differences.
This is a brief introduction to queries. There are lots of other types of queries which can be
created. This will be left for your own research activity.

Sandra Dyke, 2011

24

Access 2007 Tutorial

Reports
Reports are used to view information in the database. They can be created using a wizard
or manually and they can be modified to display the information in different ways. This
tutorial will only use the Report Wizard and making modifications to this. Reports can be
based on tables or on queries.
Select Create  Report Wizard

Follow the instructions in the wizard to select the table or query and the desired fields. The
fields will be shown in the report in the order in which they are selected.
Report 1: Customer Phone List
Select
Select
Select
Select

the Customer table


CustomerFirstName
CustomerLastName
CustomerPhoneNumber

Sandra Dyke, 2011

25

Access 2007 Tutorial

Ignore the grouping option at this stage


Sort the report first by CustomerLastName and then by CustomerFirstName

Select a report layout (this example will use the tabular layout)
Select a report style (this example will use the office style)
Give the report a suitable name (eg Customer Phone List)

Sandra Dyke, 2011

26

Access 2007 Tutorial

Preview the report.

When the preview is closed, the report is shown in the Design View Mode where alterations
can be made to the formatting of the report.
Lets have a look at the formatting of the report.
The heading looks okay, but it would be better if it was centred.
The column headings are the table attributes and are in CamelCase. They also include the
word Customer in each one. A better format for the column headings would be to delete the
word Customer from each heading and to put a space between the words.
In Design View, the report is broken into a number of sections. The report header and page
header details are just headings and can easily be adjusted. The Detail section is linked to
the database table and fields. These can also be modified by changing their placement and

Sandra Dyke, 2011

27

Access 2007 Tutorial

the size of the fields can be changed to display all of the data. The page footer shows the
date (=Now()) and the page number.

There are 2 ways to centre the report header. First the textbox containing the heading can
be moved and manually centred. Alternatively, the width of the textbox can be changed to
the width of the page and then the text can be centred.
Select the textbox

Click on the middle dot on the right of the textbox and drag it to the edge of the page

Click inside the textbox and then click the centre button.

Result

Adjusting the column headings

Sandra Dyke, 2011

28

Access 2007 Tutorial

Click inside the textbox, delete the word Customer and put a space between Last and
Name

Switch to View Mode and...

Sandra Dyke, 2011

29

Access 2007 Tutorial

Report 2: Using a query


The customer phone list report can also be created using the query created previously.
Instead of using a table, select the query.

Finish selecting options presented by the wizard and preview the report

Sandra Dyke, 2011

30

Access 2007 Tutorial

Report 3: Creating an Employee Sales Report


Using the Report Wizard, select the query qEmployeeSales and select all of the fields.
This time, the report is going to group all of the sales by employee

The report will be formatted as shown above.


Lets group within each employee by the Inventory Item.

Next, sort by SaleDate


Sandra Dyke, 2011

31

Access 2007 Tutorial

Lets sum by the quantity for each inventory item.


Click on Summary Options and select Sum

Extracts from the report after changing the report to landscape mode

Sandra Dyke, 2011

32

Access 2007 Tutorial

Now, the report is not the nicest. Switch to the Design View mode and modify the report
format to look like the following:

Sandra Dyke, 2011

33

Access 2007 Tutorial

It is possible to add new Page Headers and to modify the data displayed in the
InventoryName Footer and EmployeeFirstName Footer. The Property Sheet is useful to add
new headings so they have the same properties as the other headings.

Adjusted View

Sandra Dyke, 2011

34

Access 2007 Tutorial

Forms
A form can be used to view, add, edit and delete data. Forms can be formatted in a way
which is useful for the user.
The easiest way to create a data entry form is to use the Form button
Select the table required
Click Create  Form
Form 1: Creating a Customer Form
First select the Customer table
Then Click the Form button

This produces a form including all of the data entry fields.

Sandra Dyke, 2011

35

Access 2007 Tutorial

Any related records in the Sale table are also shown, making it easy to add, edit or delete
these records.
Each Customer record can be displayed and new records added.
Form 2: Sale entry
At the moment, the fSale form could be used; however this is not very useful because the
only data captured are ID numbers.

To improve this form, the Sale table could be modified to include lookup lists for customer,
inventory, and employee to help the user to capture the correct information.

Sandra Dyke, 2011

36

Access 2007 Tutorial

Steps
1.
2.
3.
4.
5.

Copy the Sale table and rename it (eg Sale2)


Change the CusotmerID field to a lookup list (see below)
Do the same for InventoryID and EmployeeID
Create a form to input the sale data (fSale2)
Change the SaleID to a read only field because this is an auto number field (see
below)

Change the CustomerID field to a lookup list:


Click on Lookup
Change the properties as shown below.

The source is based on the Customer table, and the first 6 columns which is the
CustomerID, CustomerFirstName, CustomerLastName, CustomerAddress, CustomerSuburb
and CustomerCity. Basing this on the table is not the best design option because if the table
structure changes (eg columns are added, removed, or reordered), the look up table will be
inaccurate. Creating a customer query to include the required information is better. Try this
yourself.
To disable the SaleID in fSale2:
Click on SaleID textbox
Open the Property Sheet if it isnt already open
Select the Data tab
Change Enabled to No

Sandra Dyke, 2011

37

Access 2007 Tutorial

Form with lookup list

Customer Lookup list

NB If you use a form to update a table, the table needs to be closed for the update to be
shown.

Sandra Dyke, 2011

38

Access 2007 Tutorial

Creating a Switchboard
Database Tools  Switchboard Manager

When asked whether a switchboard should be created, choose Yes

To use the default option, choose Edit

Change the switchboard name to something suitable (Sale System)


Click Close
Now click New to create new pages for the switchboard

Sandra Dyke, 2011

39

Access 2007 Tutorial

Type Forms

Click OK
Create a page for Reports
With Sales System highlighted, click Edit

Click New

Ensure Go to Switchboard is selected and click ok

Sandra Dyke, 2011

40

Access 2007 Tutorial

Next, select Forms in the switchboard box

Click Ok

Add items for Reports.


Add another item for Close Database (command Exit Application, Switchboard is left blank)

Sandra Dyke, 2011

41

Access 2007 Tutorial

Then Click Close to exit from the Edit Switchboard Page to return to Switchboard Manager
With Forms selected, click Edit, then click New

Do the same for the rest of the items which need to be added to the switchboard.
Also add a Return option for each button with the command = go to switchboard, and Form
= The Main Switchboard name (Sales System).
Once the Switchboard is completed, close out of it.
There are now 2 new objects in the database Switchboard Items table and Switchboard
form.

Sandra Dyke, 2011

42

Access 2007 Tutorial

Open the Switchboard select the Form version of the Switchboard

Try the different options to make sure it words properly.


To add new items or modify or delete current items, open the Switchboard Manager in the
Database Tools tab.

General Design Considerations


All of the forms and reports can have text, pictures and colour included by modifying the
design in the Design Mode and using the different options available on the Design tab.
For example, to add an image, there is an button in the Controls section, to change colour
etc, open the Property Sheet

Useful Resources
Microsoft Office Access 2007 Step by Step
Microsoft Office Access 2007 Bible
GCF Learn Free.org: Access 2007 Free Tutorial at
http://www.gcflearnfree.org/computer/topic.aspx?id=146
MS Access Tutorial at http://www.profsr.com/access/accless0.htm

Sandra Dyke, 2011

43

Access 2007 Tutorial

Вам также может понравиться