Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS...

26
INTRODUCTION TO MS ACCESS 2003 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 Introduction 1 2 Some Theory 1 2.1 What is a Database? 1 2.2 Where does Microsoft Access 2003 fit in? 2 2.3 Steps in Designing and Building a Database 2 3 Case Study 2 3.1 About the Educational Institutions 3 3.2 About the orders 3 3.3 Analysis 3

Transcript of Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS...

Page 1: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

INTRODUCTION TO MS ACCESS 2003

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 Introduction 1 2 Some Theory 1

2.1 What is a Database? 1 2.2 Where does Microsoft Access 2003 fit in? 2 2.3 Steps in Designing and Building a Database 2

3 Case Study 2 3.1 About the Educational Institutions 3 3.2 About the orders 3 3.3 Analysis 3

Page 2: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc 5.131 Ver 2 Introduction to Access 2003

3.4 Data Types 4 4 Creating a database 5 5 Working with tables 7

5.1 Creating Tables 7 5.2 Entering Data into a table 8 5.3 Sorting Data 9 5.4 More Practice on working with tables 9

6 Queries 11 6.1 What are Queries? 11 6.2 Query Wizard 11 6.3 Adding criteria to a query 12 6.4 Joining tables in a query 12 6.5 Calculated criteria 14 6.6 More practice at working with queries 14

7 Forms 15 7.1 What are forms? 15 7.2 Create a form with Form Wizard 16 7.3 Entering Data with a form 16 7.4 Modify the form design 16 7.5 Create an object on a form 18 7.6 More practice at working with forms 19

8 Reports 19 8.1 Modifying a report 20 8.2 More practice at working with reports 21 8.3 Mail Merge 21

9 Relationships 22 9.1 Why use Relationships 22 9.2 Types of relationships 23 9.3 A sample database 23 9.4 Finding out More about Database Design 24

About this Document

Words that you type or choose are in bold type, e.g. Choose Programs from the Start menu

Keys that you press are shown in small capitals e.g. press ALT Press KEY1 + KEY2 means press both keys together e.g. press CTRL + Z Press KEY1, KEY2 means press each key consecutively e.g. press A, B General guidelines on how to perform a task are indicated by bulleted lists using

the ® symbol. Note that these are not tasks for you to perform now. Exercises for you to practice as you work through the notes are labelled Exercise, e.g. Exercise 1 Importing Database Objects. The steps that you do to complete the exercise are marked by round bullets (•).

Hints and tips are indicated by If you are not very familiar with using computers then you should pay particular attention to the hints and tips.

ii Birkbeck College Central Computing Services

Page 3: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 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 objects1 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.

1 Database components in Access 2003 are referred to as objects.

1

Page 4: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

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. RDBMS’s 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.

2.2 Where does Microsoft Access 2003 fit in? You should consider building your database with Microsoft Access 2003 if your

data structures can be represented by the relational model. This is true of most business applications, and the sample databases provided with Microsoft Access 2003 demonstrate this. Microsoft Access 2003 can be used for quite large, complex database applications. However, even if your data structures are quite simple, and could be easily held in a card index, you may want to consider using Microsoft Access 2003, as it works well with Word. If you frequently need to send out mail shots then Microsoft Access 2003 is particularly suitable for use with the mail merge tool in Word

There are more specialist databases available for bibliographic databases (ITS supply Endnote in workstation rooms). Contact ITS Reception/Help Desk on our website for information on available documentation

If the data contained in your tables is mostly numeric data, then you are probably better off using a spreadsheet program such as Excel. Spreadsheet programs are specifically designed to handle numeric data

2.3 Steps in Designing and Building a Database Define the problem Decide what real world things (entities) you need to store information about.

For example Employees, Products, Customers, Orders. Entities are represented by tables in the database

Decide what facts (attributes) you need to know about each entity, e.g. an Employee’s date of birth, salary. Attributes are represented by fields in the tables

Decide on the relationships between the entities in the database; i.e. what attributes do they have in common. Relationships are formed in the database between entities that have common attributes. They have common fields in the related tables. For example, customer ‘Fred Bloggs’ can place an order for product ‘eggs’. So the Orders table has relationships with Customers table and Products table

Decide what information you need to extract from your data. This will allow you to design the queries

Design Forms that will allow users to enter data values for running queries or entering data

Design Reports that will print information extracted from the database To illustrate what this means, we will examine a sample case study, and then build a very simple database to provide the required functions.

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

2

Page 5: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

institutions. They need to keep details of the Institutions that they deal with and the orders that they place.

3.1 About the Educational Institutions The details SMS need to know about each institution are:

A unique identifier for each institution The number of students in the institution – this helps to know the potential size

of the market for their products The educational sector – whether pricing is covered by special deals for certain

types of institutions The name, address, phone, fax, email of the institution. For simplicity we will

include only the institution name in our sample database The company deals only with a named contact at each institution. SMS needs to

know who this person is

3.2 About the orders The training material is provided either as printed books or on a CD-ROM. For each order SMS need to know

The institution code. This links each order to the institution that placed it The order number. This uniquely identifies the order If the order is for books then they need to know the number. Since all the

books are the same price there is no need at this stage to break down the order into book titles

The value of the order The order type i.e. whether it is for books or CD-ROM CD Renewal date. Sites buy an annual licence to make copies of the material on

the CD-ROM so SMS need to know the renewal date

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 contact’s 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

3

Page 6: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

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

3.4 Data Types (SMS 2.10) Having decided what tables we need, and what fields we need in the tables we need to decide what type of data we are going to put in each field. Is it, for example, a number, a date, or some text. When you are building a database from scratch you will usually make use of the Microsoft Office 2003 autonumber to allocate a unique identifier for each record. Note however that if you are building a database to contain data that already exists with a unique identifier (e.g. Student number, National Insurance number, bank account number), you should use your existing unique number in preference to an autonumber.

The field that uniquely identifies a record in a database is called the Primary Key

Table 1 Fields for Contacts table

Field Name Data Type Surname Text Forename Text Edu Sector Text Institution Text Num Students Number Institution Code Autonumber

Table 2 Fields for Orders Table

Field Name Data Type Institution Code Number Order Num Autonumber Num Books Number Value Number Order Type Text CD Renewal Date: dd/mm/yy

4

Page 7: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

4 Creating a database Microsoft Access 2003 also provides wizards. Wizards are software tools that help you with a programme’s 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

Exercise 2 Create a new database

• On the menus, choose File – New • Select Blank Database from the task pane on the right-hand side of the screen • In the File New Database dialogue box make sure that you have selected the My

Documents folder on your N: drive, and replace db1.mdb in the Filename box with sms contacts.

• Click on Create 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.

5

Page 8: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

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

6

Page 9: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

5 Working with tables

5.1 Creating Tables In the analysis section we looked at the tables needed, what fields should appear in the tables and what data types those fields should have. It is important to realise that we are talking about the data structures in the database. We are not talking about the data values which will ultimately be contained in the tables. For example the field forename is a part of the contacts table data structure. When we enter names (Tony, Eleanor, Nurinder) we are entering data values into the table.

Exercise 3 Create a table

• 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

Figure 5-1 Contacts Table in Design View

• 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

7

Page 10: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

5.2 Entering Data into a table (SMS Section 3)

Exercise 4 Entering data into a table

• 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

Table 3 Data values for Contacts table

Surname Forename Edu Sector Institution

Num Students

Institution Code

Taylor Tony School Callow Grammar 500 1 Blake Victor University Lermouth 120000 2 Ambrose Guy University Mawley 8000 3 Patterson Peter College North Camber 8000 4 Keenan Penny School Abdul Kameer 600 5 Wright Eleanor College Callow 1000 6 Waverley David University Newbridge 160000 7 James Philip School Roberts High 700 8 Su Chi Hong School Southfields Tech 700 9 Patel Nurinder University Westchester 150000 10 Clarke Lenny School Wyre High 800 11 Patrick Roger School Westmouth 800 12 Parkinson Helen School Schofield Tech 900 13 Mathews Karen University Waverley 9000 14 Quentin Terry College Eastman 7000 15 Walters Trudy University Aberdare 100000 16 Peabody Ruth University Bradbury City 9000 17 Young Alice University Folklands 140000 18 Lomar Shireen University Pershore 12000 19 Booth Charlie College Truman 5000 20 Cazinaska Dimen College Trowbury 5000 21

Note that you do not have to save the data. Microsoft Access 2003 saves each record automatically as you enter it

8

Page 11: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

5.3 Sorting Data (SMS Section 4.0) Each record in the contacts table is stored in the order in which it was entered. You will often want to view it in a different order for example sorted by date or alphabetic order.

Exercise 5 Sorting Data

• Click in the Surname field to select it • Click on the Sort Ascending button (see right) to sort the records in

ascending alphabetic order. What is the seventh surname in the list?

5.4 More Practice on working with tables Now practice using the techniques that you have just learnt to create another table, enter and sort data.

Exercise 6 Create Orders Table

• 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

9

Page 12: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

Figure 5-2 Field Property

Exercise 7 Entering & Sorting Orders Data

• 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

Table 4 Orders Data

Institution Code

Order Num

Num Books Value

Order Type CD Renewal

8 1 200 £1,190.00 Books 14 2 540 £3,213.00 Books 11 3 0 £170.00 CD 31/12/01 17 4 0 £720.00 CD 05/05/02 20 5 0 £720.00 CD 01/08/02 16 6 0 £720.00 CD 04/08/02 13 7 0 £170.00 CD 01/07/02 4 8 3200 £19,040.00 Books 5 9 100 £595.00 Books 1 10 53 £315.00 Books 2 11 0 £720.00 CD 9 12 0 £170.00 CD 04/03/02 10 13 0 £720.00 CD 05/06/02 15 14 0 £720.00 CD 20/10/02 12 15 20 £119.00 Books 7 16 0 £720.00 CD 19/11/01 3 17 340 £2,023.00 Books 6 18 0 £720.00 CD 25/01/02 21 19 0 £720.00 CD 27/02/02 19 20 0 £720.00 CD 18/12/01 18 21 44 £261.00 Books 12 22 50 £298.00 Books 3 23 400 £2,380.00 Books 1 24 352 £5,095.00 Books 8 25 32 £190.00 Books 12 26 70 £416.00 Books

Click to display menu

10

Page 13: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 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:

Decide on the type of query to design Add the tables that contain the fields to be displayed Add the fields which contain the data to be displayed Add any criteria you want to use. Criteria are instructions that you use to tell

Access which records to display. When you come to design your own databases you should use the on-line help to search for examples of criteria expressions

6.2 Query Wizard (SMS Section 4.3)

A query creates a dynamic subset (called a dynaset in database terminology) of those records in the underlying tables that satisfy the criteria specified in the query.

The next task is to create a query that finds all the records in the Contacts table. We will then see how to apply criteria to the query to return dynasets.

Exercise 8 Create a simple select query

• In the database window select the Queries button • Double click on Create query by using wizard. The query wizard starts • In the box labelled Tables/Queries select Table: Contacts from the drop

down menu • Click on >> to move all the fields from the Available fields list to the

Selected fields list • Click on Next > • In the next step select Open the query to view information (it should be

selected by default) and click on Finish. Your query will return data and should display the data that you entered into the contacts table

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

Page 14: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

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.

6.3 Adding criteria to a query (SMS Section 4.4) You enter criteria in the criteria cell, that is the intersection of a row and a column. You may use a text value as a selection criteria; e.g. a person’s surname or you may use mathematical values e.g. >=10

Exercise 9 Select a contact

• In the query window switch to design view by clicking on the Design view button on the tool bar (see right)

• In the Criteria Row and Surname column type Parkinson. Your query should look like Figure 6-1

Figure 6-1 Select Query example

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.)

6.4 Joining tables in a query You can also create a query that uses more than one table. For example, if we want to find all the orders made for institutions with more than 10,000 students we need to know the institution and number of students, which is held in the Contacts table, and the order numbers which are held in the Orders table. The institution code in

12

Page 15: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

both tables allows us to link together the information. In database terminology it identifies a relationship between the tables. When working with several tables, designers often use the Table.Field notation to clarify the specification. In the following exercise you will see this notation used; for example Contacts.Institution Code refers to the Institution code field in the Contacts table.

Exercise 10 Joining tables in a query

• In the database window double click on Create Query in Design view • In the Show Table window click on contacts, then click on add to add the

table to the query. • Do the same for orders, then click on Close • A query design window appears. Add the following fields by double clicking on

the field name Contacts.Institution Code (i.e the institution code field from the contacts table) Contacts.Institution Contacts.num students Orders.Institution code Orders.Order num

• In the Criteria row, Num students column type >10000 • Save as Large Institutions Query. Your query in design view should look like

Figure 6-2

Figure 6-2 Joining tables query

• Run the query. The results should look like Figure 6-3

Figure 6-3 Results of Large Institution Query

13

Page 16: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

• Close Large Institutions Query

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.

6.5 Calculated criteria You can group records based on fields that have the same value. You can calculate mathematical functions such as totals or averages. To do this you need to add the Totals row to the design view window.

Exercise 11 Calculate the sector Average

• In the database window open a new query in design view • Using the techniques that you learnt in Exercise 10, add the contacts table, then

add the fields Edu Sector and Num Students

• 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

Exercise 12 Calculate the sector totals

• Use the technique that you learnt in Exercise 11 to calculate the total number of students (sum) for each educational sector.

6.6 More practice at working with queries You will find sample solutions to these exercises in the sample database Q:\Samples\Access2k\sms contacts.mdb.

14

Page 17: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

Exercise 13 Create an Orders query

• 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.

Exercise 15 Select CD Renewal dates

• Find all the CD orders with renewal dates after 31/12/01. Save the query as CD Renewal Query.

Exercise 16 find average book order

• Find the average number of books ordered. Save the query as avg book orders query

Exercise 17 Find lowest value order

• 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

Page 18: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

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.

7.2 Create a form with Form Wizard (SMS section 5.1) We will create a columnar form based on the contacts table, using the Form Wizard. The contacts table is the record source of the form, i.e. the source from which it retrieves data.

Exercise 18 Create a form with Form Wizard

• In the database window select the Forms button • Double click on Create form by using wizard. The form wizard starts • In the box labelled Tables/Queries select Table: Contacts from the drop

down menu • Click on >> to move all the fields from the Available fields list to the

Selected fields list then click on Next > • Select columnar layout and click on Next > • Select standard style and click on Next > • Type Contacts Form in the title box, select Modify the form design and

click on Finish

7.3 Entering Data with a form To enter data using the form you simply type the data into the boxes, using the mouse or clicking on the TAB key to move to the next field.

7.4 Modify the form design (SMS Section 5.2) In Access jargon everything is called an object. Tables, Forms, Queries and Reports are all Objects. The labels, boxes, text boxes and other items on a form or report are a special type of object known as a control. Before you can do anything with an object you have to select it, by clicking on it when you are in design view. On Forms and Reports you can make Multiple Selections; that is you can select several objects at once. All objects have properties that you can edit.

If you cannot see the properties window then click the right mouse button and choose Properties from the menu that drops down. The properties window is displayed (see right). In the title bar is the name of the object that you have selected (the form in this example).

16

Page 19: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 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

Exercise 19 Modify a form

• 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

Figure 7-1 Selected objects on a form

• 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

Page 20: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

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

7.5 Create an object on a form (SMS section 5.2) You can add objects such as labels to your forms. These objects are known as controls. You create controls by clicking once on an object from the Toolbox (see right) and clicking and dragging the mouse pointer to draw it on the form. When you point (do not click) to a control the name of the control appears as a small yellow label. If you cannot see the Toolbox then, in form design view, select toolbox from the view menu. We will start by creating a simple label on the form. However, some controls are more sophisticated. For example you can use a combo box to display a menu of items that the user is allowed to select (see SMS section 5.3). You can use a calculation as the control source from which a text box takes its data.

Exercise 20 Add a control to a form

• 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

Figure 7-2 Header/Detail sections

Position cursor here

• 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

18

Page 21: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 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

7.6 More practice at working with forms

Exercise 21 Create an orders form

• 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

• In the database window select the Reports button • Double click on Create report by using wizard. The report wizard starts • In the box labelled Tables/Queries select Table: Contacts from the drop

down menu • Click on >> to move all the fields from the Available fields list to the

Selected fields list • Click on Next > • In the grouping window select Edu sector from the list of fields & click on >

Your window should now look like Figure 8-1 • Click on Next >

19

Page 22: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

Figure 8-1 Group by Edu Sector

• 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.

8.1 Modifying a report (SMS Section 6.2) A basic report is divided into three sections

Page Header

Page Footer

Contain data that will appear at the top or bottom of each page of the report. You might use this to include information about the source of the report, the date it was completed or the page number.

Detail The most important section since any fields added here display the actual information from the database for every record in the table or query on which the report is based.

You will usually need to refine some aspect of the report in order to present information in the clearest possible way (SMS Section 6.2). The techniques that you use for modifying the controls on a report are exactly the same as for a form. You can click on controls to select them, and use the CTRL and SHIFT keys to make multiple selections

20

Page 23: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

Exercise 23 Modify a report

• 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

8.2 More practice at working with reports

Exercise 24 Create Value of Orders Report

• 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.

8.3 Mail Merge Although Microsoft Access 2003 is good for producing tabulated reports, there will be occasions when you need something more flexible than Access. The Microsoft Office suite of programs allows you to combine the data management features of Access with the formatting and layout features of Word, using a feature called Mail Merge.

Exercise 25 Mail Merge with Word

• Make sure that the Contacts table is not open • In the database window select the Contacts table by clicking on it once – do not

open it • 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 • When you have located the Office Links button click on it and select Merge it

with Microsoft Office Word • In the Mail Word Wizard dialogue box select Create New Document…

and click on OK • When Microsoft Word starts type Dear followed by a space • You should be able to see the mail merge toolbar – if not, choose View –

Toolbars – Mail Merge

21

Page 24: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

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 you’ve 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 Data Type Order Num Autonumber Institution Code Number Surname Text Forename Text Edu Sector Text Institution Text Num Students Number Num Books Number

22

Page 25: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Introduction to Access 2003 Doc. 5.131

Value Number Order Type Text CD Renewal 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.

9.2 Types of relationships Microsoft Access 2003 can handle two types of relationships:

One to Many relationship – a record in one table can be matched to many records in the second table. However, the record in the second table can only be related to one record in the first table. For example a site contact (identified uniquely by the institution code) can place many orders. But each order is associated with only one institution

One to One relationship – a record in one table can be matched to no more than one record in the second table, and vice versa

There is a third type of relationship between entities. That is a Many to Many relationship. In a many-to-many relationship, a record in Table A can have many matching records in Table B, and a record in Table B can have many matching records in Table A. This type of relationship is only possible by defining a third table (called a junction table) whose primary key consists of two fields — the foreign keys from both Tables A and B. A many-to-many relationship is really two one-to-many relationships with a third table.

9.3 A sample database We will now use a Microsoft Access 2003 database template to build a rather more complicated database than the simple example that we have looked at so far.

Exercise 26 Create a database using a wizard

• Open Access • Close any databases that you have open and select New from the File menu

• In the task pane, click on under Templates • 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

Page 26: Introduction to MS Access 2003 - Birkbeck, University of ... · PDF fileINTRODUCTION TO MS ACCESS 2003 Introduction The aim of this document is to introduce basic techniques for creating

Doc. 5.131 Introduction to Access 2003

• 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

There is a sample solution to this exercise in Q:\samples\access2k\students in classes.mdb

9.4 Finding out More about Database Design The critical factor for successful database design is to get the design of the tables and relationships right. This takes practice. A recommended book on relational database design is Peter Domanski & Philip Irvine, A Practical Guide to Relational Database Design, Domanski-Irvine Book Company, ISBN 9526043 02. See web page http://www.personal.u-net.com/~dibookco/rdbms.htm

24 Document 5.131 Version 3 September 2007