Professional Documents
Culture Documents
I MS A 2003: Ntroduction To Ccess
I MS A 2003: Ntroduction To Ccess
Introduction
The aim of this document is to introduce basic techniques for creating a simple database
using the Microsoft Access 2003 relational database management system (RDBMS).
You will undertake a case study to build a simple database consisting of tables,
relationships, forms and reports and queries. This will demonstrate the need to work out
your requirements before attempting to design a database.
You will use some of the Wizards that are provided with Microsoft Access 2003.
As we work we will introduce terminology commonly used in database design and in the
Windows 2000 environment
Prerequisites
No prior database or development experience is assumed, however Windows experience
is essential. You should be familiar with;
Keyboard layout
Use of mouse
Selecting items from menus
Using toolbars
Using dialogue boxes
Using On-line Help for Windows
Acknowledgements
These workshop notes are based on the Teachers Notes and Exercises supplied by
Software Made Simple Guide to Microsoft Office 2000. Copies of the Guide are
available to students and staff of Birkbeck either in paper form from ITS Help Desk,
MB 151 or from the College Intranet at
http://www.bbk.ac.uk/its/help/documentation/. Help Desk is open on weekdays
10.00am - 8.00pm in term time, and 10.00am - 6.00pm in vacations. References to
the SMS guides are indicated in brackets, e.g. (SMS 2.10)
Contents
1
2
Introduction
Some Theory
2.1
What is a Database?
2.2
Where does Microsoft Access 2003 fit in?
2.3
Steps in Designing and Building a Database
3
Case Study
3.1
About the Educational Institutions
3.2
About the orders
3.3
Analysis
1
1
1
2
2
2
3
3
3
4
5
3.4
Data Types
Creating a database
Working with tables
5.1
Creating Tables
5.2
Entering Data into a table
5.3
Sorting Data
5.4
More Practice on working with tables
Queries
6.1
What are Queries?
6.2
Query Wizard
6.3
Adding criteria to a query
6.4
Joining tables in a query
6.5
Calculated criteria
6.6
More practice at working with queries
Forms
7.1
What are forms?
7.2
Create a form with Form Wizard
7.3
Entering Data with a form
7.4
Modify the form design
7.5
Create an object on a form
7.6
More practice at working with forms
Reports
8.1
Modifying a report
8.2
More practice at working with reports
8.3
Mail Merge
Relationships
9.1
Why use Relationships
9.2
Types of relationships
9.3
A sample database
9.4
Finding out More about Database Design
4
5
7
7
8
9
9
11
11
11
12
12
14
14
15
15
16
16
16
18
19
19
20
21
21
22
22
23
23
24
ii
Doc. 5.131
1 Introduction
The aims of this training material are:
To introduce basic techniques for creating a database using the
Microsoft Access 2003 relational database management system (RDBMS).
In this introductory material we will not cover the more advanced features of
Microsoft Access 2003 such as macros and modules. These topics are necessary
for the development of full-blown applications
To demonstrate the need for requirements analysis before attempting
a database design. Before you create a document with a word-processor, you
need to do some planning of the content and appearance. For a simple
document you may begin typing straight away. However, a database needs much
more planning before you even think of using a RDBMS such as Microsoft Access
2003
To build a simple database. You will start by learning about the topics
needed to build a single table database, that is the simplest type of database, such
as a list of names and addresses. Topics include what is a database, steps in
designing and building a database, Tables, Forms and Reports and Queries
To become familiar with using some of Wizards that are provided with
Microsoft Access 2003
To introduce terminology commonly used in database design and in
the Windows 2000 environment. In order to understand the on-line help and
other further reading, you need to become familiar with the terminology that is
used in the documents
Work through these notes in sequence. Read the hints and explanations before you
attempt the exercises. The exercises need to be completed in the order that they
appear, as they depend on objects 1 that have been created in previous exercise.
2 Some Theory
2.1 What is a Database?
A database is a simply structured collection of data, with the emphasis very much on
structured. Everyone encounters databases in their daily lives, even if they are not in
electronic form. Telephone books, diaries, and mailing order catalogues are all
instances of databases.
Computerised databases are created, stored, manipulated and maintained using
Database Management Systems (DBMS); DBMS are the programs or groups of
programs through which we gain access to databases. In the personal computer
world, DBMSs are often referred to as database packages or database applications.
Depending on the way data is modelled, we have relational, network, hierarchical, and
object-oriented data models. The data model defines how real-world data is
represented in the database. DBMSs are typically tied to one type of data model.
Doc. 5.131
Microsoft Access 2003 is based on the relational model and is therefore a relational
DBMS, or RDBMS.
Relational databases are built up of Tables. The column headers in the table are
called fields. The rows of data in the table are called records. RDBMSs are extremely
fussy and insist that each record is uniquely identified by one or more fields. We will
see when we design tables how a unique identifier or key is used.
3 Case Study
The database that you are going to build is to hold the records of the Software
Made Simple company. The company sells training material to educational
Doc. 5.131
institutions. They need to keep details of the Institutions that they deal with and the
orders that they place.
3.3 Analysis
The important thing at this stage is not your knowledge of computers, but an
understanding of the problem. As this is not a course in Systems Analysis, the
analysis has been done for you, so that you can practise using Microsoft Access 2003
straight away. The Entities about which to keep information are Institutions and
Orders, so we will start with two tables in our database.
Since the institution is represented by the site contact, the table representing the
institution will be called Contacts. We will call our other table Orders. Tables are
structured into fields (columns) and records (rows). All records must have the same
fields, though as we shall see in this example there may be instances where a field is
redundant in some records but not in others.
The Attributes (i.e. the details that we need to know) of a Contact are the
institution code, the number of students, the name of the institution, the
educational sector and the contacts forename and surname. Since the contact is
representing an institution the institution code must be unique
The attributes of an Order are the order number, which must be unique, the
institution code, the value of the order and the order type (CD-ROM or books).
For book orders we need the number of books and for CD-ROM orders we
Doc. 5.131
need the licence renewal date. For CD-ROM orders the number of books field
is redundant. For book orders the renewal date is redundant. However, we
must include the field for all records to maintain a consistent structure
In order to identify the Relationship between an institution (represented in the
database by its Contact) and its orders we will give each institution a unique
identifier in the Contacts table. We will include this identifier as a field in the
Orders table. It is good practice, when designing a database, to use unique
identifiers. In our example each contact is uniquely identified by the institution
code. There are good reasons for this. The contact might change in the future.
In order to link an institution to the orders it has placed we need to maintain
consistency. Even if the contact name changes the institution code remains the
same so we still know where to send the order. In addition, there may be
several institutions with contacts named (say) John Smith
)The field that uniquely identifies a record in a database is called the Primary Key
Table 1 Fields for Contacts table
Field Name
Surname
Forename
Edu Sector
Institution
Num Students
Institution Code
Data Type
Text
Text
Text
Text
Number
Autonumber
Data Type
Number
Autonumber
Number
Number
Text
Date: dd/mm/yy
Doc. 5.131
4 Creating a database
Microsoft Access 2003 also provides wizards. Wizards are software tools that help you
with a programmes features. However, in this document you will learn how to
create your database from scratch, so that you are better able to match the database
to the requirement. SMS guide 2.4 describes how to create a database using a
wizard.
Exercise 1 Starting Microsoft Access 2003 on ITS
workstations
At the Windows log in prompt, type your username in the Username box. Press
the TAB key to move to the Password box, and type your password. Then press
the ENTER key or click on OK Wait a few minutes while Windows 2000 checks
your username and password, and allocates your network resources
Click on the Start button on the taskbar at the bottom of the screen. The Start
Menu will appear
Click on Programs on the Start Menu
Click on Microsoft Office on the Program menu and then on Microsoft
Access 2003
You have now created a file that will become the database. Unlike programs such as
Word and Excel, Access saves the objects that you create and the data that you enter
automatically to this file.
Doc. 5.131
When you create a new database it will look something like Figure 4-1.
On the left of the window are buttons representing various database objects.
Select the class of object that you wish to work on by clicking on the button.
Tables are the foundation of the database; just as you start building a house by
laying the foundations you start building a database by creating the tables. The
first time that you use the database the table button is selected by default
On the right of the window you will see a list of tasks that you can perform on
the selected class of object. If a wizard matches your requirements, then it is the
simplest way to create a table (see SMS section 2.7). In this document we will
learn how to create a table from scratch
Figure 4-1 New database
Doc. 5.131
Select Create table in Design view and click on the New button
In the New Table window select Design View and click on OK
Enter the field names and data types that we discussed in the analysis section.
(Surname, Forename, Edu Sector, Institution, Num Students, Institution Code)
By default the field type is text. Use the TAB key to move between fields
To change the data type display the data type menu. To do this:
either click in the data type field, then click on the drop down arrow that
appears in the right of the
or press the ALT+DOWN ARROW keys together
Choose the data type that you require. Your table structure should look like
Figure 5-1
Choose Save As from the File menu and save your table as Contacts
When Microsoft Access 2003 asks if you want to create a primary key now click
on Yes
Doc. 5.131
Click on the datasheet view button (see right). It is situated at the right
hand end of the tool bar. You do not need to enter the Institution
Code as it is an autonumber Access will do it for you. Note that if you make a
mistake and delete a record Microsoft Access 2003 will not reuse a number; your
numbers may therefore be different to those given in the examples. You should
use your numbers in your database.
Enter the data values shown in Table 3. You can use the TAB key to move
between fields. Use the BACKSPACE or DELETE key to correct mistakes
Num
Institution
Students
Callow Grammar 500
Lermouth
120000
Mawley
8000
North Camber 8000
Abdul Kameer 600
Callow
1000
Newbridge
160000
Roberts High
700
Southfields Tech 700
Westchester
150000
Wyre High
800
Westmouth
800
Schofield Tech 900
Waverley
9000
Eastman
7000
Aberdare
100000
Bradbury City
9000
Folklands
140000
Pershore
12000
Truman
5000
Trowbury
5000
Institution
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Note that you do not have to save the data. Microsoft Access 2003 saves each record
automatically as you enter it
Doc. 5.131
Make the sms contacts database window the active window (either by clicking
on it or choosing it from the Window
menu)
Using the techniques that you learnt in
Exercise 3 create a table called
Orders, with the field names and data
types shown on the right
Note that the date for renewing CD licences (CD Renewal) needs to be
expressed in Short Date format. To change the format select the CD
Renewal field, click in the Format field in the Field Properties section of the
table design view window. Click on the down arrow to display the format menu
and choose the option that you want see Figure 5-2
Save the table as Orders
Doc. 5.131
Click to
display
menu
Using the technique that you learnt in Exercise 4 enter the data shown in Table 4
into the Orders table. As Order Num is an autonumber there is no need to
enter it yourself
10
Num
Books
200
540
0
0
0
0
0
3200
100
53
0
0
0
0
20
0
340
0
0
0
44
50
400
352
32
70
Order
Value
Type
1,190.00 Books
3,213.00 Books
170.00
CD
720.00
CD
720.00
CD
720.00
CD
170.00
CD
19,040.00 Books
595.00
Books
315.00
Books
720.00
CD
170.00
CD
720.00
CD
720.00
CD
119.00
Books
720.00
CD
2,023.00 Books
720.00
CD
720.00
CD
720.00
CD
261.00
Books
298.00
Books
2,380.00 Books
5,095.00 Books
190.00
Books
416.00
Books
CD Renewal
31/12/01
05/05/02
01/08/02
04/08/02
01/07/02
04/03/02
05/06/02
20/10/02
19/11/01
25/01/02
27/02/02
18/12/01
Doc. 5.131
Sort the table in ascending order on the Value field. What is the fourth value?
6 Queries
6.1 What are Queries?
You use queries to view, change, and analyze data in different ways. You can also
use them as the source of records for forms and reports. The examples that we will
build today is called a Select Query because we are selecting fields and records from
the database.
The steps in designing a query are:
In the above example you moved all the available fields from one table. However, if
you only need some fields
Click on the field name in the Available fields list
Click on > to move it to the Selected fields list
Repeat this for all the fields that you need
11
Doc. 5.131
If you need fields from another table or query select the other table or query
from the Table/Query list
If you change your mind about a selected field, select it in the Selected fields list
and click on < to return it to the Available fields list.
Query by Example
(QBE) grid
Run the query by clicking on the Run button on the toolbar at the top of the
screen (see right)
Close your query by clicking on the close button (x) at the top right corner of
the query window. (When asked if you want to save changes click on No.)
12
Doc. 5.131
Run the query. The results should look like Figure 6-3
13
Doc. 5.131
If you do not want to display a field then clear the check box in the Show row in
the designer view.
The records are sorted in ascending order by Institution code. In design view you can
choose to sort on a different field by clicking in the Sort row of the column for your
chosen field. You can select a sort order from the drop down menu.
Click on the Totals button on the toolbar (hint: it looks like a ). A totals row
appears in the QBE grid
In the Edu Sector field choose Group By from the totals menu
In the num students field choose Avg (average) from the totals menu
Run the query
Save the query as Sector Average Query
Use the technique that you learnt in Exercise 11 to calculate the total number of
students (sum) for each educational sector.
14
Doc. 5.131
Use the technique that you learnt in Exercise 8 to create a select query titled
Orders Query, based on the Orders table, which will list all the fields in the orders
table. Experiment by using the criteria =Books and =CD in the Order Type
field to select all the book orders or all the CD orders.
Hint: In the following exercises include only those fields that are essential. For
example, in the next exercise you are interested only in those fields that define the
order type as books and the number of books purchased. You need to display the
totals row to locate the maximum function.
Exercise 14 Find maximum number of books
Create a query titled max book order query that finds the record for the
largest number of books.
Find all the CD orders with renewal dates after 31/12/01. Save the query as CD
Renewal Query.
Find the average number of books ordered. Save the query as avg book orders
query
Find the order for books with the lowest value. Save the query as min book
value query. Hint you will need to include a criteria that displays only orders
for books.
7 Forms
7.1 What are forms?
Forms provide an alternative and more accessible way of presenting and entering
data than using a datasheet. The form permits the viewing of a single record at a
time, and the design of the form may be easily customised to suit your requirements.
Other advantages of forms are:
they are familiar, as they are similar to a paper form
it is often quicker to enter data using a form rather than the datasheet
data can be presented in a more attractive and accessible manner and can include
graphs and pictures
15
Doc. 5.131
Each form is based on either a table or a query. In order to create a form that uses
fields from more than one table you will need to create a query that contains all the
necessary fields. The quickest way to create a form is by using the form wizard.
Doc. 5.131
To change a property click in the relevant field. Some properties will display a
drop down arrow, allowing you to choose a value from a menu. For other
properties you will need to type a value.
To select several objects located next to each other, hold down the SHIFT key and
click on each object in turn.
To select several objects that are not next to each other hold down the CTRL key and
click on each object in turn
You may have used a similar technique when selecting files with Windows Explorer.
Windows applications use consistent techniques for performing tasks. The
techniques explained above work equally well for selecting emails in Outlook and
selecting files in Windows Explorer
All Windows applications require you to select an object before you try to do
anything with it. For example, you must select a block of text in Word before you
can copy it
The form should now be in design view. If it is not then switch to design
view by clicking on the Design view button on the tool bar
Click on the label Surname. Hold down the SHIFT key and click on the other
labels (forename, edu sector, Institution, Num Students and Institution
code). Your form should now look like Figure 7-1
Still holding down the SHIFT key display the properties window (as explained
above). The properties window should have the title Multiple Selection
If the Format tab is not on top click on it to select it
Click to select Fore Color
(scroll down the window if
you cannot see it). A
17
Doc. 5.131
continuation button (), appears (see right). Click on it to see the choice of
colours
Select a suitable shade of red and click on OK
Click on the form and you will see that the text labels are now red
Select Save from the file menu to save your design
Make sure that you have the Contacts form open in design view. Position the
mouse pointer between the header section and the detail sections on the form
(see Figure 7-2). The pointer will change to a bar with a double arrow across it
Position
cursor here
18
Drag the detail section down so that a gap of about 1cm appears between the
form header and detail sections.
Click on the label tool on the tool box to select it. To find the tool move the
cursor along the tool bar without clicking on the buttons. You will see a label
appear to indicate the function of the button. This is called Hover Help
Click in the space that you created between the header and detail sections, then
drag to draw a box. In the box type Software made Simple Contacts
Doc. 5.131
In the Format section of the properties box set the Text align property to
center, font size to 10 and font weight to semi bold. Set the fore colour
to a colour of your choice
Save your changes
Using the Orders table create a columnar form using the Form Wizard. Title the
form Orders Form
8 Reports
(SMS Section 6)
Reporting is the way in which you choose to present the contents of your database
as printed pages.
Reports enable you to select which fields will be printed out and what data
should be included. Reports can also summarise data in various ways
Microsoft Access 2003 gives you the opportunity to design the layout of a
particular report, including the position of headings, labels and the data itself
along with graphics such as pictures and logos
Reports can take their data from tables or existing queries or you may create a
query especially for the report
In order to create a report that uses fields from more than one table a query
should be created which contains all the necessary fields. For example you could
create a report based on the Large Institutions Query that you created, which
uses data from the contacts and orders tables
The best way to create a report is by using the report wizard
We will use the Contacts table to create a report grouping the fields by Edu
Sector. We will sort on Institution and Surname fields in ascending order, and
display the report in landscape format.
Exercise 22 Create a report
19
Doc. 5.131
In the sorting window select Institution in the box labelled 1 and Surname in
the box labelled 2. The sort buttons are already set for ascending order. Click
on next >
In the layout window select Stepped layout and landscape orientation. Click
on next >
Select a style that you like and click on Next >
Type the title Contacts Report in the title box and click on Finish
Your report will now appear as a Print Preview. You can print it by either clicking on
the print button on the toolbar or by selecting print from the file menu.
20
Doc. 5.131
Switch to design view by selecting it from the View button on the toolbar
Use the same technique you previously used to select labels in the contacts form
to select all the labels in the page header section of the Contacts Report
Change the Format property Text Align to Center
Save your changes
Preview your report by clicking on the view button on the toolbar
Using the Orders table create a report, grouping the fields by Institution
Code
Sort the value field in ascending order
Use the Summary Options button on the sort window to display the Sum
option of the Value field
Select layout Align Left 1 and display the report in landscape format
Choose a suitable style for your report
Give your report the title Value of Orders Report
You may wish to experiment with different styles, layouts and summary options.
SMS section 6 gives you some ideas about what you can do with Microsoft Office
2003. In the following section we will consider how you can combine components of
Microsoft Office 2003 to produce customised documents.
21
Doc. 5.131
Figure 8-2
Click on the button Insert Merge Fields. You will see a list of fields from your
contacts table. Double-click on Forename (or click once and click insert). You
will see a place marker <<Forename>> in your document.
Type a space, then insert the merge field surname
Type the some text in your letter so that it appears as shown below:
Dear Forename Surname
Please find enclosed an evaluation copy of the latest version of
Software Made Simple material on CD-ROM. We can also supply
this material in book form.
Yours Sincerely
Simon Simple
Use Hover Help to locate the button Merge to New Document. When you
click on this button Word will generate a set of form letters, based on your Access
data source. The names of the people in the contacts table have been substituted
for the place markers in your master document. This document may be printed
9 Relationships
9.1 Why use Relationships
In the first session we discussed the tables needed for the database and introduced
the idea of relationships between the entities (mapped to tables) in the database.
We have seen that by using the Institution Code as the primary key in the
Contacts table and as a foreign key in the Orders table, it is possible to join data
from the two tables into a dynaset using the Large Institutions Query.
In the small sample data base that youve worked with you may wonder why we did
not combine all the data in one table SMS Orders (Table 5)
Table 5 SMS Orders
Field Name
Order Num
Institution Code
Surname
Forename
Edu Sector
Institution
Num Students
Num Books
22
Data Type
Autonumber
Number
Text
Text
Text
Text
Number
Number
Value
Order Type
CD Renewal
Doc. 5.131
Number
Text
Date: dd/mm/yy
Consider what would happen if an institution placed many orders. Institutions that
order books sell them on to the students and re-order each academic year. For
each order the SMS Orders table would have repeated fields for all the fields
shown above in italics.
Remember also that we simplified matters by omitting contact details such as
address, phone etc. which would otherwise have been repeated and you will see
how inefficient such a design would be. For example:
We would need to re-enter the data in italics every time an institution placed an
order time consuming, and with more scope for errors
The table would occupy more disk space
Our two table structure is more flexible. For example, if the institution contact
changes we need only change it in the Contacts table once.
Open Access
Close any databases that you have open and select New from the File menu
under Templates
In the task pane, click on
Click on the Databases tab
From the database templates select Event Management and click on OK
In the File New Database dialogue box click on create
23
Doc. 5.131
As the database wizard takes you through the steps click on next > until it is
finished
After a few minutes Microsoft Access 2003 will create a database. From the Tools
menu select relationships. You will see that the relationships are rather
complicated! Let us look in more detail at the relationship between the Events,
Attendees and Registration tables.
Events.eventid is related to Registration.eventid by a one to many
relationship (shown as 1 to ).
Attendees.attendeeid is related to Registration.attendeeid by a one to
many relationship (shown as 1 to ).
The Registration table is the junction table used to model the many to many
relationship between Events and Attendees. Attendees can attend one or
more events, events are attended by one or more attendees. Registration
relates an employee to an event
Consider a similar situation. Students can attend one or more Classes; Classes are
attended by one or more Students. However, you can link students to classes by a
Registration table.
Exercise 27
Create a small database with the tables necessary to register students on classes.
(Hint you may want to look at the design of the attendees, events, and
registration tables in the Event Management database)
Create the appropriate forms & enter some test data for about 5 or 6 students
and about 3 or 4 courses. One of your courses should be Database Design.
You will need to remove the input mask from the postcode and phone number
field properties because Microsoft Access 2003 uses American masks
Create a query and generate a report to list the names of all the students who
are studying database design
24
Document 5.131