Sei sulla pagina 1di 22

Microsoft Office

Access 2013

Tutorial Part 1
An Introduction to Access 2013
Creating a database

Author Sandra Dyke


https://sites.google.com/site/sandrascourses/

This work is licensed under the Creative Commons Attribution-


NonCommercial-ShareAlike 4.0 International License. To view a copy of
this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/
Contents Page

Introduction ........................................................................................................................... 2
Lets Get Started ................................................................................................................... 3
The Navigation Pane............................................................................................................. 4
Compact and Repairing files ................................................................................................. 5
Back Up Database ................................................................................................................ 6
Creating Tables..................................................................................................................... 7
Creating Relationships and Enforcing Referential Integrity .................................................. 13
Adding Data ........................................................................................................................ 18

Sandra Dyke, 2015 1 Access 2013 Tutorial


Introduction
This tutorial is an introduction to using Microsoft Access 2013. In this tutorial a database
with 4 tables will be created and data is added to these tables. Later tutorials will cover
creating queries 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 Attribute Data Type Size Required


Customer CustomerID AutoNumber Y
CustomerFirstName Short Text 50 Y
CustomerLastName Short Text 50 Y
CustomerAddress Short Text 50 Y
CustomerSuburb Short Text 50 N
CustomerCity Short Text 50 Y
CustomerPostCode Short Text 4 N
CustomerPhoneNumbe Short Text 15 N
r

Inventory InventoryID AutoNumber Y


InventoryName Short Text 50 Y
InventoryDescription Short Text 255 N

Employee EmployeeID AutoNumber Y


EmployeeFirstName Short Text 50 Y
EmployeeLastName Short Text 50 Y
EmployeeExtension Short Text 4 N

Sale SaleID AutoNumber Y


CustomerID Number Long Integer Y
InventoryID Number Long Integer Y
EmployeeID Number Long Integer Y
SaleDate DateTime Y
SaleQuantity Number Integer Y
SaleUnitPrice Currency 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, 2015 2 Access 2013 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.

Close this table without saving by clicking the X

Sandra Dyke, 2015 3 Access 2013 Tutorial


The Navigation Pane
This shows all of the objects created for the database
including tables, queries, forms and reports

Choosing the Object Type option displays the objects under


headings of table, queries, forms and reports. This is my
preferred option because I find it easier to find what I am
looking for particular once the database has more objects

The navigation pane can be collapsed and expanded by


clicking the Shutter Bar Open/Close Button

Sandra Dyke, 2015 4 Access 2013 Tutorial


Compact and Repairing files
Database files can become very large very quickly.
This is because everything is saved as you work including changes to queries, forms and
reports. For this reason compact and repair your database before quitting Access, if Access
starts running slow, an object (table, query, form or report) doesnt open properly, or if
Access displays an ,out of memory, message.

Go to File Compact & Repair

Sandra Dyke, 2015 5 Access 2013 Tutorial


Back Up Database
To create a back up of a database go to:
File Save As Save Database As Backup Database

Sandra Dyke, 2015 6 Access 2013 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

Sandra Dyke, 2015 7 Access 2013 Tutorial


If you clicked Table instead of Table
Design change the view from Data
sheet view to Date Design view using
the View option under the file tab or
the icons at the bottom right of the
screen

You will need to name your table to


continue call it Customer

The first table to be created is for Customer

Sandra Dyke, 2015 8 Access 2013 Tutorial


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

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

Sandra Dyke, 2015 9 Access 2013 Tutorial


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

In the Field Properties Box, change the Field Size to 50, and Required to Yes
By changing the field size to 50, the length of a name is limited to 50 characters. This
improves the efficiency of the database so extra space isnt used when it isnt needed to.
By changing the required property to Yes improves accuracy of the database because this
field is now requires a first name to be entered into the database.

Now add the rest of the Attributes with the following properties

Table Attribute Data Type Size Required


Customer CustomerID AutoNumber Y
CustomerFirstName Short Text 50 Y
CustomerLastName Short Text 50 Y
CustomerAddress Short Text 50 Y
CustomerSuburb Short Text 50 N
CustomerCity Short Text 50 Y
CustomerPostCode Short Text 4 N
CustomerPhoneNumber Short Text 15 N

Sandra Dyke, 2015 10 Access 2013 Tutorial


Why Post Code and Phone Number are not number fields
While the post code and phone numbers are numbers, the short text data type is used
because:
o sometimes we need to include a 0 at the start of the number a number field will
remove this
o a number has a limit a maximum value of 65536 (being 2 ^ 16) and this is too small
for a phone number think of the length of your mobile number or your landline
number with the area code in front of it

Save and name the table Customer if you havent already named it. This can be achieved
by closing the table, right clicking on the table name tab, or by using the save button

Then name your table

Now there is a table called Customer

Sandra Dyke, 2015 11 Access 2013 Tutorial


Now create tables for the rest of the tables using the information at the start of this
document. Give each field name a suitable caption.

Inventory (primary key InventoryID)

Employee (primary key EmployeeID)

Sale (primary key SaleID)

Creating Joint Primary Keys


Sometimes there is more than 1 field used for a primary key.
Control click to select all of the fields required for the primary key (or click the first in the list
and shift click the last in the list all the fields are consecutive and then click the primary key
button. The key symbol should appear against all of the fields which are part of the primary
key.

Sandra Dyke, 2015 12 Access 2013 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.

Primary Key Foreign Key


Table Attribute Table Attribute
Customer CustomerID Sale CustomerID
Inventory InventoryID Sale InventoryID
Employee EmployeeID Sale 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.

First check that all tables have been closed it is not possible to add a relationship to a table
which is still open and Access will tell you so!

Click on Database Tools Relationships

Sandra Dyke, 2015 13 Access 2013 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:

Sandra Dyke, 2015 14 Access 2013 Tutorial


Click and Drag CustomerID from the Customer table to the CustomerID in the Sale table

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, 2015 15 Access 2013 Tutorial


Close the relationship diagram, saving it when requested to.

Problems you may encounter


1. Line doesnt show the 1 and many symbols. It is also thinner than the line you
expect.

Solution

Right click on the link


Select Edit Relationship
Click Enforce Referential Integrity option

Sandra Dyke, 2015 16 Access 2013 Tutorial


2. The following error message appears when OK is clicked

This means there is a mismatch between the data size or type for the fields selected.

Solutions

Option 1: Incorrect attribute showing


Check the Table and Related Table fields
have the create attribute showing. If not,
use the drop down arrows to select the
correct attribute. The drop down arrow
appears when the textbox is selected

Option 2: Incorrect data type or size for either the primary key or the foreign key
Check the data type and size of the fields

Note 1: Name of attributes dont have to be the same (eg may have InventoryID and
InventoryCode). As long as the data type and size are correct, this wont cause a
problem

Note 2: The data type and size must be the same for both fields eg number and long
integer, text and size 6. The only exception is where the primary key is an
Autonumber the foreign key field must be of type Number and Long Integer.

Sandra Dyke, 2015 17 Access 2013 Tutorial


Adding Data
Data can be added using the Datasheet View mode or by using a form (covered in a later
tutorial).
Data can also be imported from a spread sheet or copied from a spread sheet. If importing
or copying from a spread sheet, it may be best to add the field properties after the data has
been imported. Either way, once the data is imported and the field properties are set, only
valid data can be entered. If field properties are set up before importing/coping the data, if
there is invalid data in any of the fields imported/copied, none of the data will be
imported/copied across. If the field properties are set up after the import/copying, the field
properties changed will only be accepted if all of the data in that particular field is valid for
the new field property entered.

Open the Customer table in View mode (Datasheet View)

If a field has a caption it will show in the field name line, otherwise the field name will be the
name given to the field itself eg

First Name is the caption for CustomerFirstName field, but CustomerLastName is the field
name for the customer last name as there is no caption provided.

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.

When you have entered the complete record (line) and move to the next line, Access
automatically saves the entry to the database.

If you make a mistake and delete a record, the autonumber will skip the number for the
deleted record which can mean a gap in the numbering of the ID field. If this happens to
you, dont worry about it but do remember this has happened because other screen shots
activities in later tutorial may have different details because of this.

Do the same thing for the other tables.

Make sure the data is added to the Customer, Inventory and Employee tables before adding
data to the Sale table (ie add data to the Sale table last)

Sandra Dyke, 2015 18 Access 2013 Tutorial


Table Attribute
Customer
CustomerID CustomerFirstName CustomerLastName CustomerAddress CustomerSuburb CustomerCity CustomerPostCode CustomerPhoneNumber
1 Bill Smith 123 Dove Street Hillside Nelson 7055 03123456
2 Bob Brown 43 Sweet Road Riverside Wellington 6986 0274567898
3 Jane Green PO Box 780 Auckland 1436 04573953
4 Sue Cross PO Box 8249 Nelson 7073 03850345
5 Fred Brown 9 Castle Grove Picton 7125 0277684930

Inventory
InventoryID InventoryName InventoryDescription
1 Hammer
2 Screwdriver Phillips
3 Saw
4 Spanner
5 Pliers

Employee
EmployeeID EmployeeFirstName EmployeeLastName EmployeeExtension
1 Joy Smith 123
2 Jack Trigger 124
3 Gary Wood 125
4 Gail Smith
5 Lee Drake 126

Sale
SaleID CustomerID InventoryID EmployeeID SaleDate SaleQuantity SaleUnitPrice
1 1 1 2 12/1/15 3 15.99
2 1 3 5 6/2/15 1 17.99
3 2 2 1 14/2/15 4 6.95
4 4 4 4 28/2/15 1 9.99
5 3 4 2 12/3/15 5 9.99

Sandra Dyke, 2015 19 Access 2013 Tutorial


Customer Table

Inventory Table

Employee Table

Sale Table

Sandra Dyke, 2015 20 Access 2013 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.

Eg for SaleID 5 change the InventoryID to 6 and then either save the table or select a
different record. The Inventory table does not contain an inventory item with an ID of 6 so the
record cannot be changed. Two possibilities exist here either ID 6 is incorrect or ID 6
needs to be added to the Inventory table. If the Inventory table needs to be updated, the
record in the Sale table needs to have correct data added to it before the Inventory table can
be corrected.

What is sufficient test data?


Where a database is being created by adding test data (or in the case of an assignment the
instructions are to include suitable data), the amount of data needs to be sufficient to test
any queries which are to be created later. For example, the data above includes the same
customer with more than 1 sale (customer 1 has a sale ID of 1 and 2). Likewise, the same
InventoryID appears in 2 different sales, as does the same EmployeeID.

Sandra Dyke, 2015 21 Access 2013 Tutorial

Potrebbero piacerti anche