Professional Documents
Culture Documents
Tutorial Acces 2010
Tutorial Acces 2010
Tutorial Acces 2010
Access 2010
Tutorial
An Introduction to Access 2010
Contents Page
Page | 1
Introduction
This tutorial is an introduction to using Microsoft Access 2010. 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:
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. Don’t use spaces in field names because
different database programmes treat these differently and it can also cause problems with creating
queries later.
Page | 2
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.
Give the database a suitable name BasicSalesSystem and choose a folder to save it to by clicking the
folder icon.
Click Create
Creating a Back up
Creating Tables
Select the Create Tab and Click the Table Design button
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
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
Page | 6
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.
Page | 7
In the Field Properties Box, change the Field Size to ‘50’, and Required to ‘Yes’
Now add the rest of the Attributes with the following properties
Save and name the table Customer (can save by closing the table or by using the save button)
Page | 8
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.
Page | 10
10
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.
The Primary/Foreign key link allows any of the information in the primary key’s 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.
11
12
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
13
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.
14
Solution
15
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 don’t have to be the same (eg may have InventoryID and
InventoryCode). As long as the data type and size are correct, this won’t 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.
Page | 16
16
Adding Data
Data can be added using the Datasheet View mode or by using a form (covered later). 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
properties changed will only be accepted if all of the data in that particular fi eld is valid for
the new field property entered.
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
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.
Make sure the data is added to the Customer, Inventory and Employee tables before adding
Page | 17
17
data to the Sale table (ie add data to the Sale table last)
Page | 18
18
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
Page | 19
19
20
Customer Table
Inventory Table
Employee Table
Sale Table
Page | 21
21
If the value included in one of the above fields isn’t 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.
22
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.
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
Steps
5. Run Query
6. 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.
Page | 23
23
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
24
Result
Design shows the query using the GUI view we started the query
with.
Change the query to Design View mode and select Ascending for CustomerLastName
Page | 25
25
Close the query and give it a suitable name when prompted (eg qCustomerPhoneList).
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)
Page | 26
26
Select CustomerFirstName, CustomerLastName from the Customer table and the SaleDate from the
Sale table. Sort by the sale date.
Result
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.
Page | 27
27
Adjust the query by showing only Bill Smith’s sales by including ‘Bill’ in Customer FirstName and
‘Smith’ in CustomerLastName.
Result
Change the query to show only those sales which occurred in February 2012
This can be done by typing >= 1/2/12 And <= 29/2/12 in the SaleDate criteria. When the query is run,
Access adds the # around each date.
Result
Page | 28
28
Create a query which shows the Employee Name, Date, Quantity, and Inventory Name for sales for
each Employee. Sort by Employee Last Name
Result
Adjust the query to show only the sales which Gail Smith has made.
Page | 29
29
Result
Result
30
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.
TotalSalePrice: Sale.SaleQuantity*Sale.SaleUnitPrice
31
Result
Once the query has been run, the calculation will show [ ] around the table and attribute names:
Create a simple query using all of the fields from the Customer table and save it with a suitable name
eg qCustomerDetails:
Page | 32
32
Result
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.
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
Page | 33
33
only use the Report Wizard and making modifications to this. Reports can be based on tables or on
queries.
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.
CustomerFirstName
CustomerLastName
CustomerPhoneNumber
Click Next
Click Next
Page | 34
34
CustomerFirstName
Click Next
Orientation (portrait)
Click Finish
35
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.
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 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.
Page | 36
36
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.
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 (on the Format Tab).
Result
37
Click inside the textbox, delete the word ‘Customer’ and put a space between ‘Last’ and
‘Name’
Do not change any of the text in the details section. If you do, the report will no
longer display the required information in the report. The text in the Detail section refers to the
field names in the database.
38
The customer phone list report can also be created using the query created previously.
39
Using the Report Wizard, select the query qEmployeeSaleDetails and select all of the fields.
This time, the report is going to group all of the sales by employee
Click Next
Page | 40
40
Click Next
Sort by SaleDate
Page | 41
41
Click OK
Click Next
Click Next
Page | 42
42
Click Finish
Extracts from the report after changing the report to landscape mode
Now, the report is not the nicest. Switch to the Design View mode and modify the report format to
look like the following:
It is possible to add new Page Headers and to modify the data displayed in the
Page | 43
43
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
Page | 44
44
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.
Page | 45
45
The easiest way to create a data entry form is to use the Form button
46
Any related records in the Sale table are also shown, making it easy to add, edit or delete these
records.
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.
Page | 47
47
Steps
1. Copy the Sale table and rename it (eg Sale2) – choose the structure and data option
5. Change the SaleID to a read only field because this is an auto number field (see below)
The source is based on the Customer table, and the first 6 columns which are the
48
NB If you use a form to update a table, the table needs to be closed for the update to be shown.