Sunteți pe pagina 1din 131

MO D U L A R

S YS T E M

DATABASE BASICS WITH


MS ACCESS

Seluk CANKURT
Mkremin ZKUL

http://book.zambak.com

Copyright Zambak Yaynclk ve


Eitim Gereleri A..
All rights reserved.
No part of this book may be reproduced,
stored in a retrieval system or transmitted
in any form without the prior written
permission of the publisher.
Digital Assembly
Zambak Typesetting & Design
Editor
Osman AY
Language Proofreader
Nanette DAY
Page Design
Hasan ZDEMR
Publisher
Zambak Yaynclk ve
Eitim Gereleri A..
Printed by
alayan A..
Gaziemir / zmir, May 2012
Tel: +90-232 252 22 85
+90-232 522 20 96 / 97
ISBN: 978-605-112-382-0
Printed in Turkey

D I ST R I B U T I O N

Zambak Yaynclk ve
Eitim Gereleri A..
Mahmutbey Merkez Mah.
Souksu Cad. No. 31 Tek-er Merkezi
Baclar / STANBUL
Tel.: +90-212 604 21 00
Fax: +90-212 604 21 12
http://book.zambak.com

Microsoft, MSN, Microsoft Access 2010, Microsoft Office 2010, and Windows
are either registered trademarks or trademarks of MicrosoftCorporation in the
United States and/or other countries.

CHAPTER 1: INTRODUCTION TO DATABASE ......... 6

Rules and Steps in Designing a Table........................... 27

What is a Database? ...................................................... 6

Identify the fields names. ...............................................27

What can be done using a Database


Management System? ................................................... 6
Applications Area for Databases ..................................... 6
Class List ..........................................................................7
Book Catalogue .................................................................7
Medical Record or Health Record.......................................8

Components of a Microsoft Access database ................. 9


Tables ...............................................................................9
Forms..............................................................................10
Queries ............................................................................10

Identify the data types that will be stored in the fields. ......27
Identify the indexed fields.................................................27
Identify the primary key field. ...........................................29
Use of the primary key field .............................................29
Creating a Table .............................................................31
Creating a Table in the Design View ................................31
Design a table by entering Data in Datasheet view ...........35
Designing a Table Using a Template ................................36

Controlling Data Entry .................................................. 36


Using Input Mask.............................................................36

Reports ...........................................................................11

Validating Data Entry ........................................................38

Macros and Modules .......................................................12

Working with Data in the Tables ................................... 39

MS Access Interface, View, or Outline .......................... 12

Sorting Records in the Tables ..........................................39

Database File Operations in MS Access........................ 13


Opening a Blank (New) Database .....................................13

Filtering Records in the Tables .........................................40


Finding and Replacing Records in Tables .........................42

Open & New ....................................................................14

Using Look up Wizard for Data Entry ............................ 42

Saving a Database ...........................................................14

Using the Lookup Wizard .................................................43

Save As versus Save .......................................................15

Make a Calculated Field................................................ 46

Closing a Database .........................................................15

Project I: Registration Table ................................... 47

Quitting Access ...............................................................15


MS Access Templates .....................................................16
The Navigation Pane ........................................................16
Editing and Deleting Records ...........................................17

Project II: Inventory Table ....................................... 47

CHAPTER 3: FORMS ........................................... 52

The Quick Access Toolbar ...............................................18

Introduction to Forms ................................................... 52

Practice : Database Fundamentals .......................... 19

Making a Form in MS Access ...................................... 54


Creating a Form Using the Form, Split Form, and

CHAPTER 2: TABLES ........................................... 24

Multiple Form ..................................................................55

Introduction to Tables .................................................. 24

Options............................................................................55

Field Data Types .......................................................... 25


Choosing the Optimal Data Types ....................................26

Planning a Table from Scratch ..................................... 26

Creating a Form Using the Datasheet Option ....................55


Create a Form Using the Form Wizard ..............................55
Creating a Form Using the Blank Form Option ..................57
Creating a Form from Scratch Using the

Pratice I: Student Registration................................. 26

Form Design Option .........................................................58

Pratice II: Computerized Book Catalog ................... 26

Using Calculated Controls ............................................ 60

Pratice III: Computer Lab Inventory ........................ 27

How to Use Expressions for Calculated Controls ..............61

Using Expressions for Default Value Property ...................62

Making a Report Using Auto Report ............................. 97

Using Expressions for the Validation Rule Property...........62

Making a Report Using Wizard ..................................... 97

Project I: Student Database Form .......................... 64


Project II: Inventory Database Form ........................ 64

Making a Report by Using Blank Form Option ............... 98


Making a Report from Scratch...................................... 99

CHAPTER 4: QUERIES ......................................... 68

Using Calculated Controls in Reports ......................... 101

Use of Queries ............................................................. 68

Project I: Student Database Reports ..................... 104

Query Types ................................................................ 68

Project II: Inventory Database Reports .................. 104

Creating a Query .......................................................... 69


Using the Query wizard ....................................................69

CHAPTER 6: RELATIONSHIPS ........................... 108

Filters and Sort Commands vs. Queries............................71

Introduction ............................................................... 108

Create a Query from Scratch ............................................71

Types of Relationships ............................................... 109

Customizing Queries .................................................... 73

Making a Relationship ................................................ 109

Sorting Records using a Query ........................................73

Making One-to-Many Relationships ................................110

Grouping and Summarizing Records using a Query ..........73

Making Many-to-Many Relationships .............................112

Using the Show Option ....................................................74


Using Criteria ...................................................................74
Using Wildcards in Criteria ...............................................76
Using a Calculated Field ...................................................78

Creating a Parameter Query.......................................... 78


Entering a Parameter .......................................................79

Establishing Relationships .......................................... 118


Data integrity rules .........................................................120
Join types......................................................................120
Editing and Deleting Relationships..................................121

Using Subform and Subreport .................................... 121

Using Multiple Parameters ...............................................80

About Subforms ............................................................122

Combining Parameters with Wildcards .............................81

Make a Form that Contains a Subform using the

Creating Action Query (Optional) .................................. 82


Creating a Make Table Query (Optional) ....................... 82
Creating an Append Query (Optional)............................ 83
Making an Update Query (Optional) .............................. 84
Making a Delete Query (Optional) ................................. 85

Form Wizard ..................................................................122


Make a Subform from the Beginning ..............................124

Project I: From Flat-File to Relational Database ..... 125


Project II: Designing a Relational Database ........... 125
Project III: School Library ..................................... 126

Making a Crosstab Query (Optional) ............................. 87


Project I: Student Database Queries ........................ 89
Project II: Inventory Database Queries..................... 89

CHAPTER 5: REPORTS ........................................ 94


What is a Report? ........................................................ 94
Reports vs. Forms ...................................................... 94
The Anatomy of a Report ............................................ 95
Making Reports .......................................................... 96

ANSWER KEY .................................................... 129

INDEX ............................................................... 130

INTRODUCTION TO DATABASE
What is a Database?
A database is a large, organized, and structured collection of data. Any data
that can be divided (categorized) into the groups (columns) are suitable for use
in a database. For example, data in a class list can be organized by names,
surnames, school numbers, and classes as a database. Data are a collection of
facts, measurements, and statistics used as a basis for reasoning.
A database management system (DBMS) is the software that controls database
operations for storing, retrieving, adding, deleting, and modifying data. Microsoft
Access, DB2, MySQL, Microsoft SQL Server, Oracle, and FileMaker Pro are all
examples of database management systems.
A database is much like a table or a group of listed items. Just like a spreadsheet
(MS Excel) tables, database tables consist of columns and rows. You can also
use any word-processing program, such as Notepad and WordPad to list items.
However, databases are actually much more powerful than spreadsheets or word
processors in terms of manipulating, sorting, searching, and reporting data as well
as some relational design concepts.

What can be done using a Database Management System?


Storing and manipulating data: you can enter, delete, and edit data.
Retrieving, sorting, and searching data: you can view, order, and find specific
data or filter data according to the specific criteria.
Reporting data: you can prepare and print out reports.
Sharing data: you can share data on networks with other users.
Protecting data: you can protect data from unauthorized users.

Applications Area for Databases


It is impossible to give a complete listing of applications that can use database
programs, but some common ones include:
Student records (name, surname, address, parents name) in a school;
User records (user names and passwords) in an e-mail server;
Computerized library systems in a school library;
Flight reservation systems for airlines companies; and
Computerized inventory systems.

Database Basics with MS Access

Some examples of databases encountered in daily life include:


Class List
That is an example of the simplest database illustrated in Figure 1-1, prepared
using Excel.

Figure 1-1: A class list in MS Excel

Book Catalogue
That provides a simple interface for browsing and accessing any book in a library.

Figure 1-2: Classical method for looking for a book

Figure 1-3: Modern method of accessing a book

Chapter 1 / Introduction to Database

Medical Record or Health Record


That is systematic documentation of a patients medical history and healthcare
in a database in an electronic environment. Traditionally, medicals records have
been written on paper and kept in folders.

Figure 1-4: Traditional medical filing system

Figure 1-5: Modern medical electronic


database system

Database Basics with MS Access

GETTING STARTED WITH ACCESS


Microsoft Access is an application program for designing relational databases. It
is a member of the Microsoft Office suite. MS Access might be used for personal
information management and small business organizations to keep track of data
in an organized manner.

Components of a Microsoft Access database


When you create a database in MS Access, you build a database file that includes
different objects (parts) that are also referred to as database objects:
Tables are a collection of data, arranged in rows and columns.
Queries make a request to the database, asking it to find, select, or filter data
stored in the table.
Forms provide a quick and easy way to modify and enter records into the table.
Reports are used to present the selected data from the table on the screen or
to get a print out in a datasheet format or in different layouts.
Macros are a set of instructions and commands to automate specific tasks.
Modules are a block of programming codes used to perform specific tasks.
Tables
A table is a place to store and keep data in a database. A table is the central
point of a database file. It is made of rows and columns. Rows are considered to
be records, and columns are considered to be fields in which details about one
specific record of the table are recorded.

Figure 1-6: An example of an MS Access table

Chapter 1 / Introduction to Database

Forms
A form is a window object that gives the user a more convenient and interactive
way of editing, deleting, and looking at the data in a table or entering data into a
table. Whereas a table allows the user to view many records at a time in only the
datasheet fashion, a form can display the contents of a record one at time in a
form view or more records in a datasheet view or in different layouts. Forms offer
a graphical environment that is easily navigated by anyone familiar with standard
computer techniques.

Figure 1-7: An example of an MS Access form

Queries
A query is like a request from a database table to make a selection and then modify
(change) the data. When you open your a table, you see all the data in the table
at one time; a query will help you select specific data that meet specific criteria
and modify the selected data.
When you run a query, it displays only fields that meet the specific criteria. For
example, Figure 1-8 shows a select query that is used to select and display only
Name, Surname, Form (Class), and Birth Date columns from the table in Figure 1-6.

10

Database Basics with MS Access

Figure 1-8: An example of an MS Access query

Reports
Reports are used to preview data on the screen in an easy-to-read format or to
get out a printout for distribution in a usable view. For example, Figure 1-9 shows
a report listing the students birth date with an attractive view and design rather
than a plain list.
Reports do not allow users to make changes to data within a database whereas
tables, forms, and queries can allow users to interact with the database to work
with data.

Figure 1-9: An example of an MS Access report

Chapter 1 / Introduction to Database

11

Macros and Modules


Macros are collections of stored actions that enable you to perform some repeating
database tasks automatically and easily. Modules are collections of Visual Basic
Access (VBA) codes used to perform some custom database actions automatically.

Figure 1-10: An MS Access module with VBA code

MS Access Interface, View, or Outline

Figure 1-11: Initial view of MS Access

After you launch MS Access, you see a window like the one shown in Figure 1-11
Initial view of MS Access, with a title bar, a menu bar, and a toolbar. The starting
screen provides access to some professionally designed database templates for
tracking assets, contacts, events, tasks, etc.

12

Database Basics with MS Access

When a database is open, you encounter the interface for the workplace, which
is the control point of the different parts of your database.
2

3
1. MS Office ribbon menu tabs.
2. Quick access toolbar.

3. New style ribbon toolbar.

4. Navigation pane.
5. Tabbed objects instead of
stacked.
6. View buttons.
7. MS Access Help.

6
Figure 1-12: MS Access interface for the workplace

MS Access has a contextual interface. It changes according to what you are


presently doing.
The Navigation pane provides easy access to all of the database objects. You can
also use the Navigation pane to organize the database objects by object type,
date created, date modified, or custom groups that you define.
The File tab provides commands such as Open, Save, and Save As; other menu
tabs display the commands with graphical menu called the ribbon.
To close open MS Access objects, you can click the close button on the far right
side of its tab or press CTRL+F4.

Database File Operations in MS Access


Opening a Blank (New) Database
After you launch MS Access, you can do one of the following:
Click the File menu and select the New option (Figure 1-11); click the Blank
Database button.
Type a name for your new database in the File Name textbox and then click
the Create button.

Chapter 1 / Introduction to Database

13

Press CTRL+N and select the Create button.


MS Access opens a new blank database.

Figure 1-13: New blank table in the Datasheet view

Now you have a blank database. In a blank database, the first thing you should do
is create a table to keep your data which is why MS Access automatically starts a
new table in the Datasheet view.
Open & New
You can use the Open command to work with an existing database whereas the
New command creates a new blank database, designing it from the beginning.
To open or retrieve an existing file:
Click the File menu and select the Open command; select your database file
and then click the Open command.
Or:
Press CTRL+O and select your database file; then click the Open command.
Saving a Database
If you want to use your Access database for later use, you should save it. This
can be done by:
clicking on the Save command

on the Quick Access toolbar or

clicking the File tab and selecting Save command or


pressing CTRL + S.

14

Database Basics with MS Access

When a database is saved for the first time, you are prompted to type a file name.
The name should be descriptive in helping to identify your database file later. MS
Access automatically adds .accdb the extension name to database file.
Save your database file form time to time during your work. All newly created or
modified database objects need to be saved. However, unlike other MS Office
applications, data entered in database objects are saved automatically.
Save As versus Save
The Save as command always opens a Save As dialog box whereas - the Save
command only opens Save As dialog box during the first use. The Save command
saves the changes you make over the existing file without showing the Save as
dialog box.
In the Save As dialog box, you can:
save your file with another name;
change the file type;
choose the place you want to save your file.
Closing a Database
To close a database file:
Click on the File tab; then click the Close Database command.
Click the close command on the title bar.
Quitting Access
Save all database objects that you create or modify before quitting MS Access.
If there are any unsaved MS Access objects open, you will be prompted whether
you want to save the open object(s).
It is a good practice to first close hte database. Some of the ways to quit MS
Access are listed below.
Double-clicking on the MS Access control menu button on the far left side of
the title bar,

Figure 1-14: MS Access control menu button

Chapter 1 / Introduction to Database

15

Clicking the close button on the far right side of the title bar,

Figure 1-15: MS Access close button

Clicking on the File menu and selecting the Exit menu option
Pressing Alt + F4.
MS Access Templates
Access includes several database templates. If you do not have much experience
with databases or if you just do not want to spend time designing a database from
scratch, Access provides various templates that you can use to quickly and easily
create a database that is ready for use:
Events: to keep track of important events, such as meeting and appointments.
Faculty: to manage information about faculty members, such as telephone
numbers and addresses.
Marketing Projects: to manage information about a marketing project.
Northwind: to manage sales for a fictitious company called Northwind Traders.
Sales Pipeline: to monitor the progress of sales.
Students: to manage information about students.
Tasks: to track a group of work items.
After creating a database using a template, you can modify and customize the
database to better suit your needs.
Figure 1-16: Northwind database
template icon

The Northwind database (Figure 1-16) is one of the great database template using
sample data. It contains some tables, queries, reports and other database features.
You can open it as previously explained.
The Navigation Pane
When you open any template database or existing database by clicking the Shutter
Bar Open / Close Button you can expand or collapse the navigation pane to

16

Database Basics with MS Access

list the existing database objects such as tables, forms, queries, and reports, in a
currently open database file (Figure 1-17).
You can also use the F11 key to open and hide the navigation pane as an alternative
to shutter bar.
When you expand the the navigation pane, by clicking the All Access Objects
bar, you can customize how your objects are displayed in the navigation pane.
According to these selected options, all the database objects will be listed by their
type (Figure 1-18).

Figure 1-17: Navigation pane


shutter bar open/close button

Figure 1-18: Customizing the


navigation pane

To open a database object listed in the navigation pane:


Right-click on the name of the object and select the Open option or doubleclick the name of the object (Figure 1-19).
To close a database object:
Right-click on the respective objects tab (an object tab is illustrated in
Figure 1-20/1) and select the Close option or click the Close button on the bar
displayed on the objects tab (an object close button is illustrated in Figure1-20/2)
or click its tab to activate a database object and then press CTRL+F4.
Editing and Deleting Records
To edit data in a record in a table or in a form:
Click the corresponding place and edit it (editing a record in a table is illustrated
in Figure 1-20/3).

Chapter 1 / Introduction to Database

17

To delete a record in a Table;


Click the row select or empty box in front of any record (line) and then press
Delete key on the keyboard or just right-click on the row selector in front of the
record (line) and then select Delete Record option clicking the row selector in
front of any record to select a record is illustrated in Figure 1-21).

3
Figure 1-19: Right-clicking a
database object in the navigation
pane
Figure 1-20: An open table

Figure 1-21: Selecting a record in a table

The Quick Access Toolbar


MS Access has a new toolbar called the Quick Access toolbar located on the far
left side of the title bar (illustrated in Figure 1-12/2).

18

Database Basics with MS Access

To Customize Quick Access Toolbar:


By adding and removing buttons, you can customize the Quick Access Toolbar.
For example you can add the frequently used options to the Quick Access Toolbar.
To add or remove buttons to the Quick Access Toolbar:
Click the up-down arrow on the far right side of the Quick Access Toolbar
(Figure 1-22) to open the short-cut menu.
Select the option you want to add or uncheck the option you want to remove.
If the menu option you want to add is not listed in the short-cut menu, click the
More Commands option to see all menu options.

Figure 1-22: Customizing the Quick Access Toolbar

Practice : Database Fundamentals


Making a Database by Using a Template
1. Start MS Access 2010. You will see the screen illustrated in Figure 1-11 - Initial
View of MS Access. If the New menu option is not selected, select it.
2. Click Sample Templates, and then click the template that you want to use.
3. In the File Name box, type a file name.
4. Click Create.

Chapter 1 / Introduction to Database

19

Opening Northwind Database


If it is not available on your MS Office installation, download and install the latest
version from the Internet:
1. Go to http://office.microsoft.com web site
2. Click the Templates tab
2. Under the Templates Categories, click see all categories.
2. Under Databases, Access 2010, click Samples.
2. Click Northwind web database Access 2010.
Deleting Records
While the Northwind web database is open, from the navigation pane, open the
Products table:
1. Add a new record after the last record in the table.
2. Change the data in the first record.
3. Delete the third record from the table.
Leave the table open and go on the next practice.
Editing Records
1. From the navigation pane, open the Product Detail form.
2. Add data for two new products using the form.
3. Change the data in the fifth record.
4. Switch back to the Products table and look for the data you added and changed
in the second step through the Product Detail form.
Tables / Queries / Forms / Reports
1. From the navigation pane, open the ProductList query and look at the result.
What is the difference between the contents of the Product table and the
ProductList query?
2. From the navigation pane, open the CustomerList report and look at the result.
3. From the navigation pane, open the CustomerExtended query and the
CustomerList report. What is the difference between the query and report
views?
Customizing the Quick Access Toolbar
1. Customize the Quick Access Toolbar by adding the New, Open, and Copy
buttons.

20

Database Basics with MS Access

Test Questions
1.

Which one of the following collections of data


is not suitable for use in a database system?

5.

a. Student class list

a. It is a relational database management program

b. Dictionary

b. It is a program used to access the Internet

c. This textbook

c. It is a member of the MS Office program suite

d. Address book

d. It is an application program

e. Inventory list
2.

Which one of the following statements is not


true about the MS Access program?

e. It is a small business and personal DBMS

Which one of the following tasks might be easily


done with the help of a database management
system?
a. Formatting a diskette

6.

. is not a component of MS Access.

b. Copying files

a. Form

c. Searching and sorting data

b. Report

d. Healing computer viruses

c. Macro

e. Compressing data

d. Menu
e. Table

3.

Which one of the following program does not


require a database system?
a. Hotel booking program
b. Library catalogue program
c. Grade book program
d. Calculator program
e. Flight reservation system

4.

7.

Assume that you have a database to keep


track of college students records. Using this
database you only want to get an attractive
printout of the list of the boarding students.
To achieve this task, which of the following
database component(s) is /are not required?

.. is used to manage organized data for


personal and small business purposes.

I. Table,

a. MS Access

a. Only I

b. Oracle

b. Only II

c. MS Word

c. Only III

d. MS SQL

d. II and III

e. File Maker Pro

e. I and III

II. Form,

III. Query,

IV. Report

Chapter 1 / Introduction to Database

21

Test Questions
8.

Which one of the following option(s) will not


work to design a new database from the
beginning?
I. Launch the MS Access program and click the
Create button
II. Open the File menu and select the New option,
then click the Create button
III. Press CTRL+N and select the Create button

b. Clicking on the File menu and selecting the


Close menu option
c. Pressing ALT+F4
d. Clicking close button on the far right side of
the title bar

a. Only IV

e. Clicking the control menu box, then selecting


Close option

c. I and II
d. I and III
e. Only I

.. is the extension name automatically


given to the database files in MS Access.
a. accdb
b. docx
c. dbx
d. accx
e. xlsx

22

a. Double-clicking on the MS Access control


menu button on the far left side of title bar

IV. Click the File menu and select the Open option

b. Only II

9.

10. Which one of the following option is not a way


to quit MS Access program?

Database Basics with MS Access

TABLES
Introduction to Tables
A table is a place where you keep your data in your database. It is made up of
rows and columns. A database can include one or more tables.
Column

Row

Figure 2-1: Table components

A table is made up of the similar data sets, groups, and columns, which are called
fields as shown in Figure 2-2. The column headings are called field names or
attributes, which actually describe the data groups in the table.

Figure 2-2: Fields names

In the column of a table, the data change for each row, but the type of data stays
the same. For example, in Figure 2-1, the Name column displays different names,
but the data are all the same text data type. In a database, each column can be
set to contain a different data type. Thus, database operations done on your table
can be executed more specifically and efficiently.

24

Database Basics with MS Access

Field Data Types


We can use different kinds of data in a table in an Access database.
Text

represents a group of characters such as names but it is limited to 255


characters. Names, surnames, and addresses are examples of the text data
type.
Memo is used to write larger text (maximum of 64,000 characters). Long text
entries can be kept in memo data type fields.
Number is for numeric data. Age, amount, and weight are examples of the
number type. The number data type can be chosen in one of the following
formats from the data field properties:
Byte is used for small numbers (integers) that range from 0 to 255.
The Integer (2-byte) is larger than a Byte; it ranges from -32768 to 32767.
The Long Integer (4-byte), Single (4-byte), and Double (8-byte) represent
larger numbers.
The Replication ID is a 16-byte Globally Unique Identifier (GUID). If
AutoNumber option is used, it assigns a globally random unique numbers
to each record. If the Number type is used, a globally unique number can
be assigned manually.
A Decimal (16-byte) is used when your number has a decimal value.
Date/Time is used to store a date or time. Purchase date and birth date are
examples of this data type.
Currency is used to represent a currency (money) value. Selling price and
school fee are examples of this type.
AutoNumber sets the numeric values automatically. It is the easiest method
for assigning unique numbers to each record in a table.
The OLE Object allows you to include other kinds of files in your database,
such as graphics, and other applications files (e.g. MS Word documents, MS
Excel worksheets).
Attachment allows you to include pictures, images, and MS Office files.
With the Hyperlink, you can provide a link from your document to a file in your
computer or to a web page on the Internet, or to an email address.
The Lookup Wizard is used to enter some pre-entered data or data from
another table in a particular column.
Yes/No is used for Boolean values. You can use one of three formats: Yes/No,
True/False, or On/Off. Any value requiring one of two options can be kept in
yes/no fields.

Chapter 2 / Tables

25

Choosing the Optimal Data Types


Choose the most suitable data types for your fields. By choosing the optimal data
type, you can decrease both:
Disk space used to store data,
Processing time to retrieve, manipulate, and write data in the database.

Planning a Table from Scratch


Tables are the central point of a database, which is why you should spend some
time planning and organizing your tables.
Before starting to design a table you should decide:
What is your aspect, and events to be tracked and processed? Answering this
question identifies the content of the table and name of the table.
Which fields do you need to organize similar values and data? Answering this
question identifies the name of the fields.
Which type of data will you store in these fields? Answering this question identifies
the field types.

Pratice I: Student Registration


On a piece of paper, create a rough copy of a single database table used to track
the registration information of students in your college. As you design the table,
consider the application form students fill out when they apply to the college.
Solution:
1. What is your aspect? Our aspect is designing a table to record the registration
data of the students. We can name this table with a descriptive name such as
StudentRecordTbl.
2. Which fields do you need (i.e. which data will be collected from the students)?
Name, Surname, Fathers Name, Mothers Name, Address, Phone Number ,
Email Address, Birth date, Gender, etc. More fields according to the required
data.
3. Which type of data will you store in these fields? Look at the Fields Data Type
table. Name Text, Surname Text, Fathers Name Text, Mothers Name
Text, Address Text, Phone Number - Text or Number, Email Address
Hyperlink, Birth date - Date and Time, Gender lookup.

Pratice II: Computerized Book Catalog


On a piece of paper, create a rough copy of a single database table to track the
books in your colleges library.

26

Database Basics with MS Access

Pratice III: Computer Lab Inventory


On a piece of paper, create a rough copy of a single database table to track assets
or inventory items in the computer room.

Rules and Steps in Designing a Table


Identify the fields names.
Identify the data types that will be stored in the fields.
Identify the indexed fields.

Student ID

Identify the primary key field.

Name
Surname

Step I - Identify the fields names.

FathersName

A Table with fields for a school database for recording student information is
shown in Table 2-1.

MothersName

Step II - Identify the data types that will be stored in the fields.

City

For the given fields in step I, the following data types can be used.

Boarding

Student ID

AutoNumber

Name

Text

Surname

Text

FathersName

Text

MothersName

Text

Address

Text

City

Text

Boarding

Yes/No

PhoeNumber

Text

WebAddress

Hyperlink

BirthDate

Date/Time

SchoolFree

Currency

Photo

OLE Object

Address

PhoeNumber
WebAddress
BirthDate
SchoolFree
Photo
Table 2-1: Field names

Table 2-2: Fields and corresponding types

Step III - Identify the indexed fields.


Using of the indexed fields: An index speeds up data searching and accessing
time in a database. Using an index is optional in small or single table databases.
In a large and professional database, an index provides the following advantages:

Chapter 2 / Tables

27

Speeds up searches in a database.


In a database with more than one table, links the tables to each other.
Despite these advantages, indexes take up disk space and slow down the adding,
deleting, and updating of records. In most situations, the speed advantages of
indexes greatly outweigh these disadvantages. However, if a database updates
data very frequently or if you face some disk space constraints, it is better to limit
the number of indexes.
Indexes are not used in fields that contain much the same data or only contain
two or three different values. For example, indexing a Yes/No field will not speed
up the data search. To check the number of unique entries in an index, use the
Access DistinctCount property. Compare this value to the number of records in
the table to determine if the index will do any good.
Unique index: If a unique index is selected, duplicate entries are not allowed. To
set a unique index, from the Field properties pane, in the Indexed combo box
select the Yes (No Duplicate) option. In this example, the unique index type can
be used for the school number, GSM number, and Email address fields. Before
setting an index field, determine which fields will be used for searching, filtering,
sorting, listing etc., information. For example, the following fields can be set as
indexes in a student database:
List the students from a particular city (e.g. from Istanbul).
List the students whose father or mother is doing a particular job (e.g. whose
fathers are doctors)?
List the students whose birth dates are in a specific this month, then set the
index property in those fields.

Figure 2-3: Indexed field

28

Database Basics with MS Access

Step IV - Identify the primary key field.


A primary key is the field(s) in a table that uniquely defines the records in the table.
Actually, the primary key field is a unique indexed (no duplicate) field. When you
set a primary key property for a particular field, MS Access will automatically set
the Index Yes (No Duplicate) property which means unique index for it.
To set a primary key property for a particular field:
Right-click on the field name in the design view and select the Primary key
option.

Figure 2-4: Setting primary key

Figure 2-5: Primary key

In design view, MS Access indicates the key field with a Key symbol.
Use of the primary key field
Primary keys are essential for database for many reasons:
They ensure that each record in a table is uniquely identified. A primary key
avoids entering the same record more than once into a table, even if you have
done so mistakenly.
They help establish and enforce various types of integrity. This concept will be
further explained in Chapter VI Relationships.
They serve to establish table relationships. This concept also will be further
explained in Chapter VI Relationships.
The main purpose of a primary key is to uniquely identify the records in the tables.
A primary key is also used to relate the records in a particular way between different
tables.
For a single table database using a primary key field is optional. For a large
relational database (containing two or more tables linked to one another), you
must define key fields for your tables.

Chapter 2 / Tables

29

Three vital rules for selecting key field(s):


To decide which fields in your table can be used as key field(s) you can consider
a few rules for each table:
1. All data in the field which will be used as the key field must be unique. In other
words, the key field should be chosen among those fields that do not have
repeating values.
2. A records primary key value cannot be null. In other words, the key field should
be chosen among those fields that do not have any empty values. The primary
key value must exist when the record is entered.
For example, citizenship number, book ISDN number, and social security
number are examples of unique fields and can be used as primary fields.
Ways to make key field(s)
After the design of your table is complete, to set the key field, close the database
design table window:
Access displays the following message box.

Figure 2-6: Primary key warning dialog box

Click the Yes button, Access creates a key field.

You can have several fields that meet criteria for all the key field rules. For

example:
a. In a table about books, book ISDN
b. In an employee table, social security number or citizenship number
c. In a school table, field for students registration numbers
When there is not a field in a table that meets all key field rules, you can use
combination of several fields as key fields. This will create a unique value for each
record, such as a persons first and last name, together with the birth date.
To make a key field of your own, do the following steps:
a. Open your table in design view and go to the beginning of the table
b. Make a field and name that field with ID in its name (e.g., StudentsID,
BooksID, OrdersID).

30

Database Basics with MS Access

c. Set the Autonumber type for that field.


d. Right-click on it and select the primary key option.
Creating a Table
There are three methods for designing a table in MS Access:
1. Creating a table from scratch in the Design view
2. Creating a table in the Datasheet view
3. Creating a table by using a template
Creating a Table in the Design View
You can design your table from scratch. Lets design the following table.

Figure 2-7: A table

Open a blank database.

Figure 2-9: Opening a table in Design view


Design view button

Figure 2-8: Blank database in Datasheet view

Chapter 2 / Tables

31

Access opens a table in the Datasheet view; to switch your view to the Design view:
Click the Design view button (Figure 2-9/1) or from the Table Tools/Fields on
the ribbon in the Views group.
Select the Design View button (Figure 2-10) or in the Database navigation pane
right-click the table name and then select the Design View option (Figure 2-8)
Access switches Table 1 in the Datasheet view to the Design view, as seen in
the following figure.

Figure 2-10: Table design view buttons

When prompted Do you want to save?, give a proper name to your table
and select the OK button (Figure 2-11).
Type the field names and set the proper Data Types.
Figure 2-11: Table save as dialog box

Figure 2-12: A table in Design view

32

Database Basics with MS Access

Figure 2-13: Entering field names and selecting their types

Click the Name field to activate it as shown in Figure2- 14/1 and from the Field
Properties pane (Figure 2-14/2), then change the following settings:

2
Set Field size to 50
Set the Required option to Yes
Set the Indexed option to Yes
(Duplicate OK)
Figure 2-14: Field properties

For the other fields:


Surname field: set Field size to 50, Required to Yes, Indexed to Yes (Duplicate

OK)
FathersName, MothersName, City fields: set Field size to 50, Required to
No, Indexed to Yes (Duplicate OK)

Chapter 2 / Tables

33

Address field: set Field size to 255, Required to No, Indexed No


Currency field: set Format to Euro, Decimal Place to 2.
For further information on Field Properties, review the following table.
Field Property Table
Field Property

Description

Field Size

You can specify the size of a field. For Text fields, The maximum number of characters (0 to 255)
that can be entered in the field; the default setting is 255. For Number / Currency fields, stores
the number as a Byte, Integer, Long Integer, Single, Double, or Replication ID. The default setting
is Long Integer.

Format

How the data in the field will be displayed on the screen.

Input Mask

Make a pattern in which data must be entered.

Decimal Places

The number of decimal places in Number and Currency fields.

Caption

A label for the field that will appear on forms. If you do not enter a caption, Access will use the
field name as the caption.

Default Value

A value that Access enters automatically in the field for new records.

Validation Rule

An expression that restricts or limits the values that can be entered in the field.

Validation Text

The error message that appears when an incorrect or restricted value is entered in a field with a
validation rule.

Required

Specify whether or not a value must be entered in the field. The default is No.

Allow Zero
Length

Determines whether or not the field allows zero-length text strings (a string containing no characters).
Zero-length text strings are useful if you must enter data in a field, but no data exists. For example,
if an ISBN number field requires data, but you do not know the ISBN number of a book, you can
enter a zero-length text string in the field. To enter a zero-length text string type in the cell. The
cell will appear empty. The default is No.

Indexed

Determines whether or not you want to index the field to speed up searches and sorts time. The
default is No.

StudentID field is selected as the primary key field and the data type of the
field is set to AutoNumber type. Name, Surname, FathersName, MothersName,
and City fields are identified as index fields.
To index a field:
Select the field in the Design view; and from the Field Properties pane, set
the index value.

34

Database Basics with MS Access

Design a table by entering Data in Datasheet view


Now lets design the same table using the Datasheet view method.
Open a blank database. A blank table in the Datasheet view is displayed.
Choose the Click to Add text box and enter your data. As you enter data, Access
automatically names the fields Field1, Field2, etc. and selects the field types
according to the data you enter (Figure 2-15).

Figure 2-15: Designing a table by entering data

To rename the default Field names:


Double click on the Field1 or click Field1 and click the Name & Caption button,
and then type a new field name (Figure 2-16).

Figure 2-16: Renaming fields

To change the field type:


Click the field, and then select the type of the field from the
Data Type drop-down box on the ribbon.
Access automatically sets the Primary Key. In order to change
the Primary Key field, switch the Design View. You can also
change the name, type, and properties of the fields in the
Design view.
To index the fields:
Click the field and then click the Unique check box to set
No duplicate type index or click the Indexed check box to
set Duplicate Ok index type.

Figure 2-17: Setting the type of the fields

Chapter 2 / Tables

35

Designing a Table Using a Template


Access also lets you use some predesigned tables based on templates such as
Events, Students, and Task.
To design your tables:
1. Start Access. Click the Sample Templates buttons (Figure 2-18). Select one
of the templates and click the Create button.

Figure 2-18: Templates

Controlling Data Entry


Using Input Mask
Setting an input mask for a field helps you enter data in a particular format. For
example, you can add an input mask for the telephone field in a table. As you
enter the numbers into the field, Access automatically adds characters (e.g.,
parentheses or hyphens).
An input mask can also be used in queries, text boxes, and combo boxes in forms.
You can set the InputMask property in the Design view of a table, query, or form.
Do the following steps to set an input mask for a field:
1. Select your table from the navigation pane.
2. Open your table in the Design view. Click in the field that you want an input
mask for.
3. In the Field Property window at the bottom of the screen, click the Input Mask
box.

36

Database Basics with MS Access

4. Click the Build button, which is (). A wizard appears.


5. Follow the instructions in the wizard dialog box. Save your table.

6
1

Figure 2-19: Setting Input Mask

Figure 2-20: Input Mask Wizard

Some input mask Characters:


Character

Description

Digit (0 through 9, entry required; plus [+] and minus [-] signs not allowed).

Digit or space (entry not required; blank positions converted to spaces, plus and minus signs allowed).

Letter (A through Z, entry optional).

Any character or a space (entry optional).

<

Causes all characters that follow to be converted to lowercase.

>

Causes all characters that follow to be converted to uppercase.

Causes the input mask to display from right to left, rather than from left to right. Characters typed
into the mask always fill it from left to right. You can include the exclamation point anywhere in the
input mask.

Password

Password creates a password entry text box. Any character typed in the text box is stored as the
character but is displayed as an asterisk (*).

Chapter 2 / Tables

37

Input Mask Examples:


Input mask definition

Examples of values

Input mask definition

Examples of values

(000) 000-0000

(308) 444-0247

(000) AAA-AAAA

(206) 555-TELE

(999) 999-9999!

(308) 444-0247
()
444-0247

#999

-10
1000

>L0L 0L0

K3M 8N3

00000-9999

67223 - 67223-4009

>LL00000-0000

KB71351-0037

ISBN 0-&&&&&&&&&-0

ISBN 1-55514-607-9
ISBN 0-13-765261-3

Validating Data Entry


In a table, you can restrict the data allowed in a field. Incorrect data entry is controlled
by setting rules. If the data is breaks the rule, Access prompts a message showing
the user what kind of entries are allowed. There are two kinds of validation rules:
field validation rules and record validation rules.
A field validation rule is used to check the value entered into a field.
For example, If you set >=1 And <=5 as the validation rule for a Number
field, to allow only values from 1 to 5 to be entered.
A record validation rule controls when an entire record can be saved.
For example, you could define [RequiredDate]<=[OrderDate]+7 as the
validation rule for an Orders table. This rule would make sure that the date
entered into the RequiredDate field is within 7 days of the date in the OrderDate
field.

8
7

3
2

6
5
4

Figure 2-21: Validating Data Entry

38

Database Basics with MS Access

Figure 2-22: Expression Builder

1. Select your table from the navigation pane.


2. Open your table in Design View using View button.
3. Click on the field name you want to limit. This will open the Field Properties
section at the bottom left of the screen.
4. In this section, next to the Validation Rule option, you will see the small button
with the ellipsis symbol (three dots).
5. Click that button, which opens the Expression Builder command box.
6. Then click the Functions option.
7. In the white box above, enter values or key words to define the range of data
that may be entered. You may use commands like AND, OR and NOT.
8. When the validation rule finishes, click OK. You may enter a validation text in
the Field Properties box to display a message when users enter an incorrect
value.
Some Validation Rule Examples:
Validation Rule

Description

<>0

Please enter a nonzero value.

1 or >5

Value must be either 1 or over 5.

<#1/1/2010#

Enter a date before 2010.

>=#1/1/2000# and <#1/1/2001#

Date must be in 2000.

Working with Data in the Tables


Sorting Records in the Tables
You can sort and rearrange contents of the fields in a table in a specified order.
1. From the Navigation Pane, open your table by double-clicking it. You can use
the F11 key to open the Navigation Pane if it is not visible.
2. Click the field that you want to use to sort the records.
3. From the Home menu ribbon, click the Sort Ascending button

if you

want to sort the records from 0 to 9 or from A to Z.


4. From the Home menu ribbon, click the Sort Descending button

if you

want to sort the records from 9 to 0 or from Z to A.

Chapter 2 / Tables

39

Using Criteria
You can sort MS Access records using more than one field. Access sorts criteria
by first sorting the column to the left; the column immediately to the right of that
column is sorted next.
1. Open the table that you want to sort according to the criteria.
2. Click on Home ribbon menu; in the Sort & Filter group, click Advanced, then
click the Advanced Filter / Sort... option. A window appears.
3. Select your sort criteria. By clicking in the first field of the first column, a small
menu marked by an arrow will appear.
4. Click on the arrow to access a list of all column (field) titles. Select the first
column you want to sort by. In the text box directly below, select the sort order
that you want (ascending or descending).

Figure 2-23: Sorting Data Entry

5. Repeat these steps for the second and any additional fields that you want to
sort by.
6. When your criteria are selected, click the Apply Filter button.
You will see
the records sorted according to the criteria you have just established.
Filtering Records in the Tables
Filtering allows you to retrieve specific information from a table. For example, in
your College Database, you can run a filter to select the students whose names
start with the letter S.

40

Database Basics with MS Access

Here are the steps:;


Open the table containing the records that you want to filter.
Click the arrow next to the Filed name and then select the any filter option.

Figure 2-24: Filtering records in tables

Or click the Filter button

on the Home tab ribbon in Sort & Filter group.

Access displays a menu at the location of the selected field. Select your filter
type.
For our example: select the Begins With... option and type S to filter all of
the students whose name starts with the letter S. The result will list the filtered
students.
To further filter your search, select a second field from the filtered table and
continue.

Remove the Filter


If you want to see all your records again without filtering them, you can remove
the filter:
1. Select the filtered table, select the filtered field, then click the Filter button
again.
2. From the menu, click the Clear filter from .... option or from the Advanced
button select Clear All Filters option to remove all filters.
3. To delete filter(s) temporarily, click the Toggle Filter button.

Figure 2-25: Removing a filter


from a field

Chapter 2 / Tables

41

Finding and Replacing Records in Tables


To use the Find and Replace dialog box, open your table in the Datasheet view
and, from the Home tab, in the Find group click the Find

command.

The Replace tab is used to change the data found by the the Find command to
the data in the Replace text box.

Figure 2-26: Find and Replace dialog box

Using Look up Wizard for Data Entry


A lookup column (or field) is a field in a table whose value is retrieved from another
table or from a value list. You can use a lookup column to display a list of choices
in a combo box or list box. The choices can come from a table or query, or they
can be a list of values that you supply. You can create a lookup column manually
by setting a fields Lookup field properties; or automatically by completing the
Lookup Wizard. Whenever possible, you should use the Lookup Wizard to create
a lookup column. You can also create a lookup column in either the Datasheet
View or the Design View.

Figure 2-27: Using Multiple Criteria

42

Database Basics with MS Access

Using the Lookup Wizard


The Lookup Wizard starts in the following cases:
When you create a lookup column in the Datasheet view, click the
arrow key next to the Click to Add field notification, then select the
Lookup & Relationship option.
When you drag a field from the Field List pane to a table that is opened in
the Datasheet view,

Figure 2-28: Using Lookup in


Datasheet view

Figure 2-29: Using Lookup with a Field list

In the Design view when you select Lookup Wizard in the Data Type column.

Figure 2-30: Using Lookup in Design view

The wizard guides you through the steps needed to create a lookup column and
automatically sets the appropriate field properties to match your choices.

Chapter 2 / Tables

43

The wizard provides two type sources for data: lookup column on a table or query
or on a list of values that you enter.

Figure 2-31: Lookup Wizard dialog box

Choose the option I want the lookup column to look up the values in a table
or query.
Click the Next button (Figure 2-31); the Lookup Wizard displays a list of tables
and queries.
Select a table or query, click the Next button (Figure 2-32).

Figure 2-32: Lookup Wizard dialog box

The Lookup Wizard lists the available fields from the selected table or query. Select
the field(s) you want to add and click the Next button (Figure 2-33).

44

Database Basics with MS Access

Figure 2-33: Lookup Wizard dialog box

The Lookup Wizard displays a page that allows you to specify a field(s) to sort
the lookup column. This sorting is optional. When you are finished, click the Next
button (Figure 34).

Figure 2-34: Lookup Wizard dialog box

The next page of the wizard displays the fields that supply lookup values. You
should choose a field that uniquely identifies the row. Usually, the primary key
field of the source table makes a good choice. When you are finished, click the
Next button.

Chapter 2 / Tables

45

On the final page of the Lookup Wizard, you should type a name for your lookup
column, which will become the name of the field in your table
If you want to allow for the selection of more than one value and store the
multiple values when the lookup column appears, select the Allow Multiple
Values check box.

Make a Calculated Field


You can make a calculated field in the Design view.
1. Open the table in which you want to make a calculated field in the Design view.
2. In the first empty row of the table design grid, under Field Name, type a name
for the calculated field.
3. Under Data Type, choose Calculated.
The Expression Builder opens.

Figure 2-36: Expression Builder

4. Enter the calculation that you want to perform in the Expression Builder. For
example, you can multiply Field1 and Field2 and display the result in a calculated
Field3.
For example to calculate the selling price: [UnitPrice]-[UnitPrice]*[Discount]

46

Database Basics with MS Access

5. When you are finished typing the calculation expression, click OK.

Figure 2-3: Table with calculated field

After you enter values for the UnitPrice and Discount fields, SellingPrice field will
be calculated and displayed automatically.

Project I: Registration Table


Design a table to keep track of students registration information, such as name,
surname, fathers name, address, birth date, and photo, and save your database
as studentdb.

Project II: Inventory Table


Design a table to keep track of the inventory in a school; save your database as
inventorydb.

Chapter 2 / Tables

47

Test Questions
1.

Which one of the following is different from the


others?

5.

a. Field
b. Category
c. Attribute

2.

a. List

b. Memo

c. Text

d. Long text

e. Number

d. Record
e. Column

Which data type will be the best selection for


the book summary field in a table?

6.

To keep the pictures in a field in a table which


data type do you need?
a. OLE

To design a table, you should first decide


______________.
a. Data types

b. Indexed field

c. Field names

d. Field properties

e. Key fields

b. Memo
c. Bounded Picture
d. List

7.

e. Text

What do you use to achieve the following goals?


I. Speed up your data search and access time
II. Link tables to one another

3.

To maintain the order number of the computers


in a computer room, which data type will be the
best selection?

a. Indexed fields

b. Queries

c. Macros

d. Relationships

e. Lookup fields

a. Byte
b. Integer
c. Decimal
d. Long Integer
e. Single

8.

Which one of the following fields is not suitable


for adding an index?
I. Boarding status (yes / no field)
II. School fee field

4.

Which data type will be the best selection for


the key field in the main table?
a. AutoNumber
b. Long Integer

48

III. Student picture fields


IV. Notes (with memo data type)
V. Birthdate
VI. Fathers Name

c. Text

a. I, II, III and IV

b. III, V and VI

d. OLE

c. III and IV

d. I, II and III

e. Date/Time

e. V and VI

Database Basics with MS Access

Test Questions
9.

The main purpose of a _____________ is to


uniquely identify the records in a table and
implement a relationship between two tables.
a. Primary key
b. Indexed field

13. Which of the following fields would be the wrong


selection for use as a key field in a table?
a. Citizenship number
b. Social security number

c. Query

c. International Standard Book Number (ISBN)


number

d. Macros

d. AutoNumber field

e. Calculated field

e. Birth date

10. If you are designing a flat-file database (which


contains only one table), you do not need to
specify:
a. Indexed fields
b. Field names
c. Field types
d. Key field
e. Table name
11. Which of the following option is not important
to select a field to use as a primary key field?
a. Primary key must uniquely identify each record.
b. The primary-key value cannot be null.
c. The primary-key value cannot be changed after
entered
d. The primary key field must be first field in a
table.
e. The primary-key value cannot be repeated.
12. Primary key fields are set automatically by MS
Access to
a. Non-indexed field
b. Indexed Duplicate OK field
c. Indexed No Duplicate field
d. First field
e. AutoNumber field

Chapter 2 / Tables

49

FORMS
Introduction to Forms
Forms are used to view, enter, edit, and modify data quickly and easily in tables
directly or through a query.

Figure 3-1: Form

A form contains a Form Header, Detail, Form Footer, Title bar, and Status bar.

Figure 3-2: Form in Design view

52

Database Basics with MS Access

To design a form you should use form controls located in the


Form Header, Detail, and Form Footer sections.
Figure 3-3: Record navigation buttons
Table 3-1 Some form controls

Control
Label

Text Box

Description
Displays text. Good for naming objects on a form, titles, and fixed text.

Displays the value of a field from a record or is used for data entry into the fields.

Check Box

Displays the two state values in a graphical representation: yes/true/on state is


represented with checked mark and no/false/off state is displayed with clear box.

List Box

Displays a list of values. This list can be a list of values you have pre-entered statically
during the form design stage or it can be drawn from another table or query, which
also can be updated at any time in the running stage.

Combo Box

This is a combination of a Text Box and a List Box. When you click the drop-down arrow
of the Combo Box, it displays a list of choices.

Attachment

Allows adding several types of files, such as bitmap files, jpg files, sound files, and
Office documents, to the fields without increasing the size of the database.

Option
Button and
OptionGroup

Like check box, displays Yes/No (two states) values in a graphical fashion; useful when
one option should be selected at any time.

Hyperlink

Displays a blue underlined hyperlink text that, when clicked, navigates to the pointed
object. It is usually a web page, but it can also be a picture, an e-mail address, any
type of file, or a program.

Image

Displays a fixed picture on the form. It is good for displaying logos and decorating
the form.

Unbound
Object
Frame

Displays fixed old-fashioned OLE (Object Linking and Embedding) objects such as an
embedded bitmapped picture, Excel worksheet, audio file, or Word document within
your form.

Chapter 3 / Forms

53

Check box is used to make a


choice. It enables the users to
select more than one option at
a time.

Label is used to display


the descriptive names.
Text box is used to enter
or display the data.

Option box is used to make a


choice. It enables the users to
select only one option at a time.

List box is used to select


data from the list.

Commend button is used


to perform action.

Tab is used to categorize


a group of controls.

Combo box is used to select an


option from the drop down list.

Figure 4: Illustration of some Form Controls

Making a Form in MS Access


There are several ways to create a form in MS Access:
Table 3-2: Creating forms

Method, Option

Icon

Description

Form

Automatically creates a form from the currently selected table or query.

Form Wizard

Creates a form using the Wizard.

Datasheet

Automatically creates a form to display multiple records at once in datasheet


fashion.

Form Design

Creates a blank form in Design view to design a form from the beginning.

Blank Form

Creates a blank form in Layout view. In Layout view, while form is running, it
can be edited. When you locate a bound form control, It dynamically displays
current data.

54

Database Basics with MS Access

Creating a Form Using the Form, Split Form, and Multiple Form
Options
You can use the Form tool to quickly create a form that displays information about
one record at a time (Figure 3-1):
1. In the Navigation Pane, click the table or query that contains the data that you
want to use on your form.
2. On the Create ribbon menu, in the Forms section, click More Form
then click the Datasheet

button,

option.

3. Access creates the form automatically and displays it in the Datasheet view.

Creating a Form Using the Datasheet Option


You can use the Datasheet tool to quickly create a form in the Datasheet format,
which displays all records at a time.
In the Navigation Pane, click the table or query that contains the data that you
want to use on your form.
On the Create the ribbon, in the Forms section, click the Form
Form

or Multiple Items

, Split

option.

Access creates the form automatically and displays it in Layout view.


To start your new form, switch to Form view. On the Home ribbon menu, in the
Views group, click View, and then click Form View

Create a Form Using the Form Wizard


When you use Form, Split Form, Datasheet, and some other options to make a
form, Access automatically adds all fields and uses its own default styles.
The form wizard provides options about how fields appear on the form, what layout
and style will be used, and how the data are grouped and sorted.
To use the Form Wizard option:
1. In the Create tab on the ribbon, in the Forms section, click the More Forms
command, and then click Form Wizard

Chapter 3 / Forms

55

2. Follow the directions on the dialog boxes of the Form Wizard.

Figure 3-5: Form wizard

3. In the first dialog box, you can select the table or query that contains the data
you want to work on and then select the fields to locate the data in your Form;
then click the Next button.

Figure 3-6: Form wizard

4. In the second dialog box, chose the form layout and then click
the Next button.
5. In the last dialog box, you can name your form and decide to open
your form or continue developing your form design yourself.
6. On the last page of the wizard, click Finish.
Figure 3-7: Form wizard

56

Database Basics with MS Access

The Form Wizard produces a variety of forms depending on the options


that you select.

Creating a Form Using the Blank Form Option


Using the Blank Form option creates a blank form and allows you design a form
in Form view. In the Form view display, you can design and modify the form. Each
control displays current data as you double-click or drag them from the Field list
onto the Form.
To modify your form you can use:
Property sheet to modify the properties for the form and controls located in
it and sections. To display the property sheet, press F4 or click the Property
Sheet button on the Design tab in the Tools group.
Field List pane to add fields from the associated table or query. To display
the Field List pane, click the Add Existing Fields button on the Design tab in
the Tools group or Press ALT+F8. Then drag fields directly from the Field List
pane onto the form.
In the Create tab on ribbon, in the Forms group, click Blank Form.

Figure 3-8: Blank form

Access opens a blank form in Layout view, and displays the Field List task
pane on the right side as shown Figure 3-8.
In the Field List task pane, expand the table or tables that contain the fields by
clicking the plus symbols.
To add a field to the form:
Double-click or drag it onto the form.
When you drag or double-click any field it will be locate on the form and
also be automatically bounded to the table and its corresponding field.

Figure 3-9: Form tools

Chapter 3 / Forms

57

From the Design tab on the ribbon, use the tools in the Controls group to add
a logo, title, page numbers, or the date and time.
You can also switch to the Design view by right-clicking on the form, clicking

, and then using the tools in the Controls group on the


the Design View
Design tab.
To add a control;
Click the tool for the control you want; then click on the form where you want
to position it.

Figure 3-10: Form controls

Creating a Form from Scratch Using the Form Design Option


If you want to design your form completely from the beginning, you can use the
Form Design Option, which opens a blank form in the Design view.
To design a form from the beginning, you should follow four steps:
On the Create tab, in the Forms group, click the Form Design command.
Access opens a blank form in Layout view.

Figure 3-11: Form in Design view

58

Database Basics with MS Access

Next, bound your form to a table. To define the forms source, you should bound
(link) the form and table to one another.
To bound a blank form to a table or query;
Right-click on the form control box
and select Form properties or click
the form and press F4 to display the property sheet on the right side. Click the
Data tab; then select the Record Source box and select the source.

Figure 3-12: Form properties

Locate your controls using the tools in the Control group on the Design tab
(Figure 3-9).
You can rename the labels of the controls to the name of the data fields. In
order to rename a control, double-click on the label and type a new name.

Figure 3-13: Locating form controls

After you finish laying out the controls on your form, you should also define
sources for unbound controls.

Chapter 3 / Forms

59

To bound a form control to a table field:


Right-click on an unbound form control and select the properties. You will see
the Property Sheet pane to the right as shown in Figure 3-14b. On the pane
click the Data tab, then click Control Source box and select the corresponding
field name.
Repeat this for every unbound form control.
After you finish your form, you can switch to the Form view to use your form.
On the Home ribbon menu, in the Views group, click View and then click Form
View.

Figure 3-14a: Form control properties

Figure 3-14b: Control source

Using Calculated Controls


Instead of bounding a form control to a field, you can type an expression as a data
source for a form control. Text box controls are usually used as calculated control.

60

Database Basics with MS Access

An expression is a combination of:


Identifiers the names of table fields or controls on forms or reports, or the
properties of those fields or controls, such as [Name] and [OrderDate]
Operators mathematical or logical operators such as + (addition), / (division),
and < (smaller)
Functions built-in functions such as SUM or AVG
Constants values that do not change such as 0.6, 303, and College

You can use expressions to:


calculate values
set a default value for a field or control
validate data
How to Use Expressions for Calculated Controls
1. In the Navigation Pane, right-click on the form or report that you want to change
and click Design View

on the shortcut menu.

2. On the form or report, right-click the text box control that you want to change,
then click Properties on the shortcut menu.
3. If necessary, click the Data tab.
4. Click the box next to the Control Source property and type your expression.
For example, you can copy and paste an expression from Table 3-3 Some
Examples for Expressions
5. Close the property sheet.
Table 3-3: Some Examples for Expressions

Expression

Description

Example result

=[Page]

Displays the page number.

=Date()

Displays the current date.

=[Price]*1.1

The product of the value of the Price field or control and 1.1 If value in Price field 100
(adds 10 percent to the Price value).
produce 110

=Forms![Student]![Name]

The value of the Name control on the Student form.

=Avg([Score])

Uses the Avg built-in function to display the average of the


scores of a field named Score

Mesut

Chapter 3 / Forms

61

Using Expressions for Default Value Property


If you want to assign a default value to a field or control, you can use the Default
value property.
To add a default value for a control in a form:
In the Navigation Pane, right-click the form that you want to change and click
Design View on the shortcut menu.
Click the field that you want to change; and on the General tab, click the Default
Value property box.
Type the expression, or click the Build button

to the right of the property

box to type an expression using the Expression Builder or by yourself.


Table 3-4: Expressions for Default Field Values

Expression

Default Field Value

XXL

XXL

Istanbul

Istanbul

Date() + 7

one week later from today

Using Expressions for the Validation Rule Property


To add a Validation Rule for a control in a form:
In the Navigation Pane, right-click the form that you want to change and click
Design View on the shortcut menu.
Click the field that you want to change; on the General tab, click the Validation
Rule property box and then type your expression.
Table 3-5: Example expressions for Validation Rule

Validation Rule property

62

Validation Text property

<> 0

Enter a value other than zero.

0 Or > 5

Enter a value that is either 0 or more than 5

Like S????

Enter a five-character long text, beginning with the letter S.

< #1/1/2010#

Enter a date prior to 1/1/2010.

>= #1/1/2010# And < #1/1/2011#

Date must be in the year 2010.

Database Basics with MS Access

The following example in the following form demonstrates how to use the Validation
Rule property and Validation Text property.
Open a form in the Design view (Figure 3-15); select the Zip/Postal Code field
and then press F4 to open the Property Sheet.
Set the Rule property to <>0 and Validation Text to Please enter a value
other than 0.

Figure 3-15: Setting Validation Rule and Text properties

Open your form in the Form view or run it (Figure 3-16) and enter the zero value
for the Zip/Postal Code field.
Press enter to confirm your entry.
A warning message shows that you are not allowed to enter a zero value in this field.

Chapter 3 / Forms

63

Figure 16: Validation Text Warning

Project I: Student Database Form


Open the studentdb database that you have designed in Chapter II and design
a form for that table.
a. Open the form and create records for five students.
b. Open the table and find the records that you have just created with the help of
the form.
c. Open the table and create three new records at the end of the table.
d. Open the form and find the records that you have just created directly into the
table.

Project II: Inventory Database Form


Open the inventorydb database that you designed in Chapter II and design a
form for that table.

64

Database Basics with MS Access

Test Questions
1.

Which one of the following is not a form control?

5.

a. List box

a. Label

b. Option box

b. Navigation

c. Check box

2.

Which form control can be used for a Yes/No


field in a form?

c. Input box

d. Menu

d. Check box

e. Textbox

e. Bound Object Frame

You can link a form to _____________

6.

Which form controls cannot be used for field


used to display students class in a form?

a. a macro

I. Text Box

b. a table field

II. List Box

c. a table

III. Combo Box

d. a form control

IV. Check box

e. a report

V. Option Box
a. I, II and III

3.

You can link a form control to _____________

b. III and IV

a. a table

d. III, IV and V

b. a table field

c. I and II
e. IV and V

c. a form
d. a query
e. a report

4.

To link a form, use the _____________ option in


design view.
a. Table source
b. Record set
c. Record source
d. Data source
e. Query

Chapter 3 / Forms

65

QUERIES
Use of Queries
You can consider a query as a request used primarily to extract, gather, and select
required data from a database. It is also used in certain cases to manipulate data,
such as updating and deleting records or calculations in data fields. There are also
some sophisticated queries to automate these data operations.
A query can select different data fields from one or more related tables to create a
list, such as a list of names, surnames, fathers names, and fathers phone numbers
of the students who attended Computer Science course.
You can use the queries to perform calculations on data to produce the sum,
average, count, and some other mathematical operations. For example, you can
calculate the averages of all students as well as the class averages.

Figure 4-1: A query

Query Types
Select query enables you to select specific fields out of the tables instead
of displaying all fields in the tables. A select query can do three basic query
operations easily: selection, filtering, and sorting.
Find Duplicate Query finds the repeated records in one or more fields that
you specify.
Find Unmatched Query finds records that do not have related records in
another table.
Crosstab query calculates outcomes, such as sum, average, and count.
Action Queries perform an action on the results of the selected data. There
are four types of action queries:
1. Delete query deletes records from the table(s).
2. Append query adds records from one or more tables to the end of one or
more other tables.

68

Database Basics with MS Access

3. Update query changes present records with the updated version in the
table(s).
4. Make Table query makes a new table from all or part of the data in one or
more tables.
Parameter queries prompt the user for criteria that restricts or filters the results.

Creating a Query
Using the Query wizard
Using the Query wizard is a quickly, effective method to design a query easily. You
can use the Wizard to create:
Simple Query
Crosstab Query
Find Duplicate Query
Find Unmatched Query
To make a simple select query:
On the Create tab, click the Query Wizard button (Figure 4-2).
The New Query dialog box opens. (Figure 4-3).

Figure 4-2: Query Wizard Figure

Figure 4-2: Query Wizard button

Select the Simple Query Wizard in the list, and click OK. (Figure 4-3).
In the Tables/Queries list, click Table: Table1. Then double-click the required
fields from the Available Fields list to the Selected Fields list.

Chapter 4 / Queries

69

For example, Name, Surname, Birthdate, and EmailAddress (Figure 4-4).

Figure 4-3: Query Wizard

In the next dialog box, name your query (Figure 4-5) and click the Finish button.

Figure 4-5: Query Wizard

Access runs the query and displays the results in the Datasheet view
(Figure 4-6). You can scroll through the results and see that information is

displayed in order.

70

Database Basics with MS Access

Figure 4-6: A select query

Switch to Design view if you want to customize the query.


Filters and Sort Commands vs. Queries
The Filter and Sort commands are usually faster to implement than queries.
The Filter and Sort commands can be used temporarily; queries can be
permanently saved and run again in the future.
The Filter and Sort commands can be used with only the currently open table
or form. A query can use tables or queries.
Create a Query from Scratch
On the Create tab, in the Query group, click the Query Design command.

Figure 4-7: A query in Design view

The query design window and Show Table dialog box are displayed (Figure 4-7).

Chapter 4 / Queries

71

In the Show Table dialog box:


Double-click a table to add it to the query window. Close the dialog box.
The upper part of the window displays a field list for each table or query that
you add.
The selected table is represented in the top portion of the window with a field
list. An asterisk at the top of each list represents all fields in the list.
The lower part of the window, known as the design grid, is used to define the
querys criteria and accept expressions as criteria.

Figure 4-8: A query in Design view

Drag or double-click on the fields Name, Surname, Birthdate, and EmailAddress


to add them to the columns in the Design grid.
On the Design tab, in the Results group, click the Run command.
Access runs the query and displays the results in the Datasheet view.
(Figure 4-9).

Figure 4-9: A select query

72

Database Basics with MS Access

Customizing Queries
With a custom a query you can sort, filter, restrict records, group, summarize records
in the selected fields, and create calculations in the records in the selected fields.
Open your query in the Design view to customize it.
Sorting Records using a Query
A query displays records in the order in which they are entered. You can also sort
records alphabetically, numerically, or by date in ascending (A to Z) or descending
(Z to A) order.

False

To sort the records in a query:


Open the query in the Design view.
The third row in the design grid is labeled as Sort (Figure 4-10). You can set
sorting criteria for one or multiple fields.

Figure 4-10: Sorting in a query

Grouping and Summarizing Records using a Query


You can also group records in a query, usually for the purpose of summarizing
data. A group is a collection of related records; it can be useful when you are
analyzing data.
To group the records in a query:
Open the query in the Design view. On the Design tab, click the Totals
command.
In the design grid, the Total row appears as shown in Figure 4-11.

Chapter 4 / Queries

73

Select one of the functions to group your records.

Figure 4-11: Using the Total option in a query

An Aggregate function returns information about a set of records and some important
aggregate functions, as follows:
Sum: Totals the values for each group.
Avg: Averages the values for each group.
Count: Returns the number of items in each group (excluding nulls and blanks).
The other three options are listed here:
Group By: Defines the group by
reducing the data to unique entries.
Expression: Returns a calculation
based on an aggregate function.
Where: Specifies search criteria.
Using the Show Option

Figure 4-12: Using the Show option in a query

Open your query in the Design view. In


the Show row, use the check boxes to
hide or display the fields.

Using Criteria

True
74

You can limit the results of your query by specifying criteria expressions. For
example, you might want to see only students from a specific state (city).

Database Basics with MS Access

How to Use Criteria


Open your query in the Design view.
Type your criteria in the corresponding criteria place, as shown in Figure 4-13.

Query will return only those records


in which the UnitPrice entry equals
to $10.

Figure 4-13: Using criteria

Use the logical operations and, or, and not to combine your criteria.

Figure 4-14: Two different methods of using the AND condition

The query returns those records in which the Quantity value is bigger than 0 and
the UnitPrice field equals $10.

Chapter 4 / Queries

75

Figure 4-15: Two different methods of using the OR condition

The query returns those records in which the Quantity value is bigger than 0 or
the UnitPrice field equals to 0.
Table 4-1: Some sample criteria expressions

To include records that...

Use this criterion

Query result

Exactly match a text value

Turkey

Returns records where the value of a field is Turkey.

Match some certain characters

Right([Name], 1) = a

Returns the names ending with the letter a from the


Name field.

Match a numerical value smaller


than a value

< 50
<= 100

Returns records less than 50 (<50). The second


expression (<=100) displays records less than or
equal to 100.

Do not match a date value

Not #2/1/2010#

Returns records that took place on a day other than


February 1, 2010.

Match todays date

Date()

Returns the records with the dates on the current day.

Using Wildcards in Criteria


Wildc ards minimize search efforts by returning records that fit a clue rather than
matching each condition exactly. Access supports several wildcard characters;
the four most commonly used wildcards are listed in Table 4-2.

76

Database Basics with MS Access

Table 4-2: Wildcards

Character

Name

Description

Asterisk

Matches any single character or more characters in that position

Question mark

Matches any single character in the same position

[]

Brackets

Specifies a range of characters (including numeric values)

Exclamation point

Excludes characters from the series specified in []

Table 4-3: Examples for wildcards

To include records that...

Use this criterion

Query result

Start with the specified string

Like M*

Returns records that begin with M, such


as Merdan, Michael, and Mak.

Do not start with the specified string

Not Like S*

Returns records that begin with a character


other than S, such as London or Derbent.

End with the specified string

Like *y

Returns records that end with y, such as


Italy and Turkey.

Fall within a specific range

Like [A-F]*

Returns records that start with any letters between A and F.

If you add the * wildcard to the end of the search string (e.g., a*), the query matches
any entry that begins with a (Figure 4-16).

Figure 4-16: An example using a wildcard character *.

Chapter 4 / Queries

77

Using a Calculated Field


A calculated field, or calculated column, is any field that contains an expression.
It is created as an expression in a field cell in the design grid.

Figure 4-17: Using a calculated field

For example, by using UnitPrice, Discount and Quantity fields, you can calculate
the new price after the discount. To do this calculation;
Add the following expression into the field box of the new column in the design
grid.
Total: ([UnitPrice]-[UnitPrice]*[Discount])*[Quantity]

Figure 4-18: A Query result with a Calculated Field

Run your query to see similar results as those illustrated Figure 4-18.

Creating a Parameter Query


Whenever a query is run, it always reflects the most up-to-date information in the
database. Sometimes it is needed to run the same query with different parameters,
such as running a query that displays the member of the same specific class with
a different class name.

78

Database Basics with MS Access

Access provides a query type called the Parameter Query. When a parameter
query is run, Access displays a dialog box prompting for the parameters. You can
add a parameter to any one of the following types of queries:
Select
Crosstab
Append
Make-table
Update
Entering a Parameter

Figure 4-19: Entering a parameter

Instead of typing a fixed value or expression into the criteria cell:


Type a text enclosed in square brackets ([ ]), which will be used as a message.
The text you type will appear as a prompt in a dialog box.

Figure 4-20: Prompting parameter

Chapter 4 / Queries

79

Enter your criteria after the query is run.


The query displays all the records matching your criteria.

Figure 4-21: a Parameter Query Result

Using Multiple Parameters


You can enter more than one parameter in the criteria row.

between

Figure 4-22: Using multiple parameters

Instead of typing the constant beginning and end dates into the
criteria cell;
Type a prompt in square brackets.
The user will see two separate dialog boxes, each asking for
the dates.
Figure 4-23: Prompting parameters

80

Database Basics with MS Access

The query displays all the records that fall into the dates
between 1/1/2010 and 31/12/2010.

Combining Parameters with Wildcards


You can combine parameters and wildcards using the ampersand symbol (&).
For example:
A single wildcard:
Like [type prompt here] & *
Two wildcards:
Like * & [type prompt here] & *
The following example displays the use of a wildcard:

Figure 4-24: Using wildcards with parameters

The parameter Like [Enter starting letter] & * displays a prompt in which
the user can enter the first letter or a starting sequences of the letters of the
names they want to list.

Figure 4-25: Prompting parameter

When a user enters a, all records whose name starts with the letter a will be
listed.

Chapter 4 / Queries

81

Creating Action Query (Optional)


You can use an action query when you need to modify data such as editing records,
adding new records, deleting existing records, or making a new table filled with data.
An action query can change data. It is a good practice to back-up your data to
protect the information before starting to work with action queries.
To make a copy of a table:
Click the table in the Database window, and press Ctrl + C.
Press Ctrl + V. Access will display the Paste Table As dialog
box, shown in Figure 4-26.
Type a name for the copied table. Use a name that will indicate
that it is a backup table.
Figure 4-26: Copying a table

Choose the Structure And Data option and then click OK.
The new table is identical to the original, including the data.

Creating a Make Table Query (Optional)


You use a make table query when you need to copy some fields in a table to
another table or backup data. A make table query can copy data from one or more
tables into a new table.
To make a make table query:
1. In the query Design view, make a select query and add the fields you want to
copy into the other table.
2. Convert the select query into a make table query.
3. Run the query to make your new table.

Figure 4-27: Query tools

82

Database Basics with MS Access

Create a select query.


On the Query Design on the ribbon, in the Query Type group, click the Make
Table command.
Make Table dialog box opens (Figure 4-28).

Figure 4-28: Make table dialog box

Type a name for your new table in the Table Name text box and select a location
in the current database or another existing database.
Run the query.
The table is added into the current database or another database.

Creating an Append Query (Optional)


An append query adds new records to an existing table using data from other
sources.
To make an append query:
Create a select query.
Convert the select query to an append query. Click the Append command in
the Design tab.

Figure 4-29: Query tools

Chapter 4 / Queries

83

the Append dialog box opens (Figure 4-30).

Figure 4-30: Append query dialog box

Select the name of the table you want to add new records to and select the
location of the table .
Access automatically adds an Append To row in the Design grid and selects
the proper destination fields for you (Figure 4-31).
Run the query.

Figure 4-31: Append query in Design view

Making an Update Query (Optional)


An Update query finds existing data and replaces it with new data in the same table.
When you update two tables, the updated data will be deleted from the source
table and added to the corresponding fields in the target table.

84

Database Basics with MS Access

To create an Update query:


Create a select query and add the fields you want to update.
Convert the select query into an update query. Click the Update button in the
Query Design tab.

Figure 4-32: Query tools

Access adds an Update To row in the Query Design grid. For example; if you
enter 4A into the Update To box and 3A into the Criteria box, only those
records that are 3A will replace with 4A.
Run the query.

Figure 4-33: Update query in design view

Making a Delete Query (Optional)


A delete query deletes entire records (rows) from your tables. You can use criteria
to specify which rows to delete. A delete query provides you with the opportunity
to review the rows that will be deleted before you continue the deletion.

Chapter 4 / Queries

85

Create a select query.

Figure 4-34: Query tools

On the Query tab in the Query Type group, click the Delete button to convert
the selected query into a Delete query.
In the design grid, the Sort and Show rows disappear, and the Delete row
appears.

Figure 4-35: Delete query in Design view

Click the asterisk symbol * to add all fields automatically added to the design
grid (Figure 4-36). You can also add only one or some fields by double-clicking
on them manually in the design grid. The word Where should appear in the
Delete box to indicate that you can use it for criteria. The word From should
appear in the Delete box.
Run the query to delete records that have Istanbul in the City field from the
Customers table.

86

Database Basics with MS Access

Figure 4-36: Delete query

Making a Crosstab Query (Optional)


A crosstab displays the data in a different way in the datasheet view. A crosstab
query requires at least three fields, one of which should be either numeric (so that
its values can be used for some calculations, like totals or averages) or suitable
for counting (i.e., to count the number of records).

Row Headings
a

Column Headings
b

Values
Figure 4-37: a Crosstab query result

Chapter 4 / Queries

87

To make a crosstab query, you need at least three fields: for row headings, for
column headings, and last one for values.
Row headings in the crosstab query come from the Surname and Name fields
Column headings in the crosstab query come from the values (data) in the
Course
Values (data) in the crosstab query come from the Exam Score field
Click the Query Design command on the Create tab.

Figure 4-38: Query design tab

In the Show Table dialog box, double-click each table or query that you want
to use as a record source.
Close the Show Table dialog box. On the Design tab, click the Crosstab
command.
In the query design, double-click each field that you want to use as a source
for row headings.
In the query design grid, in the Crosstab row for each row heading field, select
Row Heading.
Double-click the field that you want to use as the source for column headings.
You can select only one field for column headings.
In the query design window, double-click the field that you want to use to
calculate summary values. You can select only one field to use for summary
values.
In the query design grid, in the Total row for the summary values field, select
an aggregate function to use to calculate the values.
In the Crosstab row for the summary values field, select Value.
On the Design tab, in the Results group, click Run.

88

Database Basics with MS Access

Project I: Student Database Queries


Open the studentsdb database that you designed in Project I in Chapter II.
a. Make a select (simple) query selecting the fields that contain information about
the names, surnames, and birth dates of the students.
b. Make a parameter query to list the students whose birthdays occur in the
current month.
c. Open the table that contains the student registration information in the design
view; if a field that contains entries about the students classes (forms) does not
exist, add a field to enter students class information to the table. Enter class
information data into this new field for every student.
d. Design a parameter query that asks for the class name and list the students
names and surnames from that class.
e. Make an update query to change the students classes from 1A to 2A.
f. Make a make table query to copy the students from the last grade to the new
table.
g. Make a delete query to delete the students from the last grade.

Project II: Inventory Database Queries


Open the inventorydb database that you designed in Project II in Chapter II.
a. Make a select (simple) query to list all the computers and their places in the
school.
b. Make a parameter query that asks for the room name and list all the items from
that place.

Chapter 4 / Queries

89

Test Questions
3.

1.

By using select queries, you can _____________


data.
a. delete

b. display

c. modify

d. update
e. add

4.
I. The upper part displays the field list

By using action queries, you can ____________


data.

II. The lower part is called the design grid

a. retrieve

b. display

III. This is a query design window

c. select

d. modify
e. print

Which ones are correct?


a. I, II and III

b. II and III

c. I and II

d. I and III

5.

e. II and III
2.

In the query design, to add fields to the design


grid:

You can limit the number of records in a query


by using the _____________ property.
a. Criteria

b. Sort

c. Show

d. Field
e. Calculated field

6.

If you see the following results as a query


result, which one of the following cannot be
your criteria for the Name field?

a. Double-click the field in the field list. Access


will add the field to the first blank column.
b. In the design grid, choose a field from the Field
drop-down list.
c. Drag the field to a column
d. Right-click and select a field
e. Drag the asterisk symbol (*) to the design grid
to add all the fields.

90

Database Basics with MS Access

a. an*

b. [A-B]n*

c. An*

d. [a-c]n*
e. An?

Test Questions
7.

When prompted, you enter the letter s. You get the following results. Which of the following statements
should be used in the query for the Name field?

a. Like * & [enter any letter:] & *


b. [enter any letter:]
c. Salih, Jasenko, Mustafa
d. Like * & [enter any letter:]
e. Like [enter any letter:] & *

Chapter 4 / Queries

91

REPORTS
What is a Report?
Reports display selected and required information in an organized fashion for
previewing on the screen or as hard-copy printouts.

Figure 5-1: A Report

Reports vs. Forms


Reports and forms are used to give people easy access to the information stored
in the tables in a database. However, reports and forms have different aims for use:
Forms are used to enter, view, delete, and edit information. Reports are used
only to view information.
Forms are usually used to display information on the screen, whereas Reports
are usually designed for printing needs. Although you can print tables, queries,
or forms, they lack the formatting features for printing purposes that are available
to reports.
Forms generally provide information for the people who actually work with the
database. Reports are often used to group and summarize data and are often
for people who do not work with the database, but who use its information for
other business tasks.

94

Database Basics with MS Access

The Anatomy of a Report


Reports are made up of many parts and sections. A report usually has the following
three sections, as shown in Figure 5-2.
Page Header section
Detail section
Page Footer section
Access automatically adds these three sections to every report.
Page Header: Page header appears at the top of each page of the report. The
Page Header section automatically prints at the top of every page of the report. You
can use this section for column headings, page title, or some common information
for all records on the page.
Detail: The Detail section is the main section of the report; it is used to show the
details of each record from the table or query.
Page Footer: The Page Footer section automatically prints at the bottom of every
page of the report. The footer is used to specify information that you want to appear
at the bottom of each report page, such as page numbers, dates, or copyright
notification.

Figure 5-2: The Anatomy of a Report

Chapter 5 / Reports

95

Sections that might appear in a report:

In addition to these three sections, a report can also include:


Report Header section

Report Header

Report Footer section

Page Header

Group Headers section

Group Header
Detail
Group Footer

Group Footers section


Report Header: The header appears and prints once at the beginning of
a report. The report header is often used to show the Report title or make
a cover page for a report.

Page Footer
Report Footer

Report Footer: This appears and prints once at the end of a report. The
most common use of the report footer is for grand totals, but it can also
include any other information for a report.
In addition to the report and page headers and footers, an Access report can have
several sub-group headers and footers. Report groupings separate data logically
and physically.
Group Header: When you group records in a report, the group header is used
to show where each group begins. The group header prints before the detail for
the group.
Group Footer: Each group header can have a corresponding group footer that is
typically used to add separators or summaries (such as group totals). The group
footer prints after the detail for the group.

Making Reports
There are several ways to create a report in MS access:
Table 5-1: Ways to make a Report

Method, Option

Icon

Description

Report

Creates a simple report automatically based on currently selected table or


query.

Report Wizard

Creates a report using Wizard.

Report Design

Creates a new blank report in the design view to design a report from scratch.

Blank Report

Create a blank report in layout view but lets you layout the report controls.

96

Database Basics with MS Access

Making a Report Using Auto Report


An Auto Report automatically adds all the fields in its data source, whether a table
or query. It designs a tabular report where the labels for all fields appear on one
line at the top of the report, with all records visible below them. To create a report
using Auto Report:
Click on the table or query in the navigation pane.
From the Create ribbon menu, click the Report button. Access automatically
makes a tabular report from the selected table or query.

Figure 5-3: MS Access Create ribbon menu

Making a Report Using Wizard


The Report Wizard provides an easy and fast way to make a report. The Report
Wizard guides you through each step. You answer questions about your report,
and Access makes a report according to the options you have selected.
To start the Report wizard:
Click the Report Wizard icon from the Create ribbon menu in the Reports
group.
Follow the directions in the dialog boxes of the Report Wizard.
From the first dialog box, you can select the table or query and the
fields to include in your Report, then click the Next button.
In the second dialog box, add grouping levels and then click the Next
button.
From the third dialog box, select sort order for your records and then
click the Next button.
From the fourth dialog box, select layout and orientation styles. If you
do not select any layout, Access will select the Tabular layout and
Portrait orientation style as a default setting. Click the next button.

Figure 5-4: Report wizard

Chapter 5 / Reports

97

In the last dialog box, you will be prompted to enter a name for your new report.
After you type a name, click the Finish button to see your report. If you want
to change your report design in this step you can select Modify the reports
design option, and then click the Finish button.

Figure 5-5: Report wizard

Making a Report by Using Blank Form Option


This command creates a new blank report in the layout view and guides you to
select which table(s) and which field(s) will be used. You can then use the layout
report controls to customize and format your report.
Click Blank Report, on the Create ribbon menu in the Report section.
Access opens a blank report in the Layout view, and displays the Field List
task pane on the right side as shown in the Figure 5-6.

Figure 5- 6: Blank report in Layout view

98

Database Basics with MS Access

In the Field List task pane, expand the table or tables that contain the fields by
clicking the plus symbols.
To add a field to the form, double-click it or drag it onto the form.
When you drag or double-click any field, it will be located on the form and
will automatically be bounded to the Table and its corresponding field.
From the Design tab on the ribbon, you can use the tools to add a logo, title,
page numbers, or date and time to the form.

Figure 5-7: Report tools

You can also switch to the Design view by right-clicking the report and then
. You can then use the tools in the Controls group
clicking Design View
on the Design tab.

Figure 5-8: Report controls

To add a control, click the tool for the control you want; and then click on the
form where you want to position it.

Making a Report from Scratch


The Report Design command opens a new blank report in the Design view. To
design a report from scratch;
On the Create ribbon menu, in the Report section, click the Report Design
Option.
Access opens a blank report in the Design view as shown in Figure 5-9.

Figure 5-9: Blank report in the design view

Chapter 5 / Reports

99

In the second step, bind your report to a table or query. Binding (linking) to a
table or query defines the source for your report.
To bind a blank report to a table or query:
Right-click the report control box and select Report properties or click the
report and press F4 to display the property sheet on the right. Click the Data
tab, then select Record Source box and select the source.
Locate your controls using the tools on Control section in the Design menu
ribbon.

Figure 5-10: Report controls


Figure 5-11: Setting report properties

Forms and reports almost always use the same controls, which means that
you can place any type of control on a form that you can place on a report by
clicking their icons on the ribbon. However, some controls are designed for onscreen interaction, such as combo boxes, option groups, command buttons,
and tab controls. These controls are not suitable for reports, which you can look
at but cannot interact with. For example, you cannot select or clear checkboxes
on reports.

Figure 5-12: Making a report in the design view

You can rename the label controls to name the data fields. Double-click a label
and type new names.

100

Database Basics with MS Access

When you have finished laying out the controls on your report, you should
define sources for unbound controls. A bounded control works with data in the
corresponding fields of the tables or queries.
To bound a report control to the table or query field:
Right-click on an unbound form control and select the properties. You will see
Property Sheet pane as shown in Figure 5-13. In that pane, click the Data tab;
then click the Control Source box and select the corresponding field name.

Figure 5-13: Setting the control source for a field

Repeat this for each unbound report control.


After you finish your report, you can switch to Report view to use your report. On
the Home ribbon menu, in the Views group, click View, and then click Report
View.

Using Calculated Controls in Reports


A calculated control is one whose source of data is an expression rather than a
field. For example, assume that you have a table for payments and you want to
give the students an extract (statement of account) based on that table. Also you
can include the reports current date, time, and page number information as a
calculated control.

Figure 5-14: Payments table

Chapter 5 / Reports

101

Figure 5-15: Report with calculated controls

To design this report:


Open a blank report in design view.
Add the PaymentDate, PeymentDescription, and PaymentAmount fields
from the Payments table to your new report, as seen in Figure 5-16.

Figure 5-16: Calculated controls in the Design view

To add page information, in the Header/Footer group, click the Page Number
button.

Figure 5-17: Report tools

102

Database Basics with MS Access

The Page Numbers dialog box opens.


Select the same options as in Figure 5-18, then click the OK button.
To manually set the page number calculation control:
Draw a text box in the Page Footer section. Right-click on it and select the
Properties option. On the Data tab,
enter =Page & [Page] & of & [Pages] to the Control Source box.

Figure 5-18: Page Numbers dialog


box

Figure 5-19: Control source

Click the Date and Time button; the Date and Time dialog box opens.

Figure 5-20: Date and Time dialog box

Select the same options as in Figure 20 then click the OK button.


Date() and Time() are built-in functions that return the current date and time
as displayed in your computers system clock.
To manually set the date and time calculated control, draw two textboxes in the
Page Header section. Right-click the textboxes; select the Properties option.
In the Data tab, enter =Time() and =Date() to the Control Source boxes.

Chapter 5 / Reports

103

To calculate the sum of payments for the entire report, add a textbox control
in the report footer and set the ControlSource property of the text box to
Sum([PaymentAmount])
The Sum function calculates the total for a set of values from your record source
in this case, the field named PaymentAmount. You can see some others
functions in Table 5-3.
Table 5-3: functions

Figure 5- 21: Numbering items

Project I: Student Database Reports


Open the studentdb database that you designed in Project I in Chapter II.
a. Design a report to make a list of the students. List the name, surname, and
their classes.
b. Design a report that asks for class name and lists only those students from that
class. Display the students names and surnames in the detail section; at the
top of the report, automatically display the class name that the user entered
one time.

Project II: Inventory Database Reports


Design a database to keep track of students payments. (Design a single table
database with fields such as name, surname, address, city, school fee, payment
amount, payment description, and payment date)
a. Design a report to prepare a receipt voucher to give to students after payment.
b. Design a report to prepare the extract (statement of payments) included the
calculated controls to display the total payment and the loan.

104

Database Basics with MS Access

Test Questions
1.

You want to get a nicely formatted and appealing


printout of some information. Which database
object will help you prepare such a printout?

4.

a. To view and enter data

a. Forms

b. To store and modify data

b. Modules

c. To filter and search data


d. To display and present data

c. Tables

e. To display and modify data

d. Queries
e. Reports

2.

5.

You want display the date and your class name


on every page of a report; you should add this
information to the _______________.
a. Page group

All reports must contain _______________.

b. Page header

a. A title section

c. Detail section

b. A date/time section

d. Page footer

c. A detail section

e. Report footer

d. A footnote section
e. A logo section

3.

Which of the following is the main purpose of


reports?

Which of the following database components


can be the source of a report?

6.

In the following diagram, you are going to see


directions of the flow of the data (in other words,
dependencies between database objects). How
should the boxes be labeled?

I. Table
II. Form
III. Query
IV. Report
a. I and II
b. II and III
c. I and III

C
a. A: Form, B: Table, C: Query, D: Report
b. A: Table, B: Form, C: Query, D: Report
c. A: Report, B: Form, C: Query, D: Table

d. II and IV

d. A: Table, B: Query, C: Form, D: Report

e. I, II and III

e. A: Form, B: Report, C: Query, D: Table

Chapter 5 / Reports

105

RELATIONSHIPS
Introduction
Generally there are two main database types: flat-file and relational.
Flat-file database is a type of database that contains only a single table. Designing
flat-file databases is simple and does not require much design knowledge. Flat
files can be developed using just about any database engine.

Figure 6-1: A flat-file database

Relational database contains more than one table, and the tables are related to
one another. In a relational database, data can be subdivided into different tables.
The primary advantage of a relational database is that information is not duplicated
in the records in the same table or different tables.

Figure 6-2: Relational database

Access is a relational database management system. Relationships are used to


link tables to one another.
Database relationships will help you bring the data together in meaningful ways.

108

Database Basics with MS Access

Types of Relationships
ONE-to-ONE
In a one-to-one relationship, a record in one table can have only one related record
in another table.
Typically, two tables are linked in a one-to-one relationship because:
Some of the data are used less frequently. Thus, you can divide one large table
with many columns into two smaller tables and you can link them and then store
the frequently used data in one table and less frequently used data in the other
table.
If some of the data are more confidential, it is a good idea to isolate part of a
table for security reasons; for example, you can store general information about
students in one table and password and username information in another table
that is linked to the first table.
Store data that are short-lived and can be easily deleted in a table that can be
simply deleted.
ONE-to-MANY
In a one-to-many relationship, a record in one table can have multiple matching
records in another table. One-to-many relationships are the most common
relationships in Access databases.
MANY-to-MANY
In a many-to-many relationship, many records in one table can have many matching
records in another table. A many-to-many relationship actually is a pair of one-tomany relationships.

Making a Relationship

Figure 6-3: Relationship

Chapter 6 / Relationships

109

Relationships can be established by linking two fields located in different tables.


The field from the first table, called Primary key field (must be a unique field that
can be a key field or Indexed, No duplicate field) links to a corresponding field
(called a Foreign key) in the second table.
The Primary key field in the first table and Foreign key in the second table can
have different names in their tables, but they must be of the same data type.
When you want to establish a relationship between two tables, Access makes some
decisions based on your predefined settings in the fields that will be related and
automatically determines the type of the relationship. Access establishes a oneto-many relationship only of one of the related fields is a primary key.
Primary Key Field
A primary key field must be a unique field in which values cannot be duplicated.
In a table, two types of fields can be unique fields: a Key field or a field with No
Duplicate Indexed property.
A table can have a field with unique data that can be set as the primary key field,
such as a field for NationalID number in a table. Otherwise, you need to create and
add a field of the AutoNumber type to a table and use it as the primary key field.
Foreign Key Field
A foreign key field does not need to be unique in the one-to-many relationship, but
it is strongly recommended that it be an index field with the duplicate OK option
to speed up the database operations.
Making One-to-Many Relationships
In this type of relationship, a row in table A can have many matching rows in table
B, but a row in table B can have only one matching row in table A.
To establish a one-to-many relationship:

Figure 6-4: Fields in the tables

110

Database Basics with MS Access

Figure 6-5: One-to-many relationship considerations

Open the Student table in the design view and create a field with the name
StudentID; set its type to AutoNumber and then set it to Key field.
Open the Payments table in the design view; create a field with the name
PaymentID and set its type to AutoNumber. Set it to Key field.
In the second table, create a field with the name StudentID (which is the Key
field of the first table).
The StudentID field is an attribute of the first table, which helps to uniquely
identify the students in the first table. The StudentID field in the second table
actually is not a field that belongs to the Payment table. It is copied from the
first table to the second table to relate the two tables through those fields. Thus,
it is a foreign key for the second table.

Figure 6-6: One-to-many relationship

The primary key side of a one-to-many relationship is indicated by a key symbol


or number 1. The foreign key side of the relationship is denoted by an infinity
symbol.

Figure 6-7: One-to-many relationship

Chapter 6 / Relationships

111

Making Many-to-Many Relationships


Access does not provide an explicit many-to-many relationship type, but a many-tomany relationship can be set up indirectly by using a linking table (or junction table).
A linking table has the foreign keys from both table A and table B. Making oneto-many relationships from table A to the junction table and then making one-tomany relationships from table B to the junction table establish a many-to-many
relationship from table A to table B.
For example, the Authors table (table A) and the Books table (table B) have a manyto-many relationship that can be established by a one-to-many relationship from
each of these tables to the BookAuthors table (junction table).

Figure 6-8: Fields in the tables

Figure 6-9: Many-to-many relationship considerations

Open the Authors table in the design view, create a field with the name AuthorID,
set the type to AutoNumber, and set it to Key field.
Open the Books table in the design view, create a field with the name BooktID,
set the type to AutoNumber, and set it to Key field.
Open a new table in the design view and save it with the name BooksAuthors.
This table will be a junction table.

112

Database Basics with MS Access

Create a field with the name BooksAuthorsID, set its type to AutoNumber and
set it to Key field.
To name the key field, you can use the Table name with the ID suffix.
Create a field with the name AuthorID (which is the Key field of table A).
As previously mentioned, in a one-to-many relationship, the related fields must
have the same data types. The StudentID field in the first table is an AutoNumber
type; in the AuthorID field in the second table, the AutoNumber type cannot be
used because it is located on the many side of the relationship and can result
in duplicate values. Access has an exception in that the AutoNumber field can
be matched with the Long integer field.
Set the type of the AuthorID field in the second table to the Long integer type.
Repeat these steps with the BookID field in the junction table.

Figure 6-10: Many-to-many relationship

The primary key side of the many-to-many relationships is denoted by the number
1. The foreign key side of a relationship is denoted by an infinity symbol.

Figure 6-11: Many-to-many relationship

Chapter 6 / Relationships

113

Figure 6-12: Linking a table in a many-to-many relationship: BooksAuthors table

When you open the BooksAuthors junction table, you will see only some numbers.
To make this junction table more meaningful:
Right-click the BooksAuthors table form in the Navigation Pane and open the
BooksAuthors table in the design view.
In the Design view, click the BookID (Figure 6-13/1), from the Field Properties
section; open the Lookup tab (Figure 6-13/2), then open the Display Control
drop-down box (Figure 6-13/3) and select the Combo Box option (Figure 6-13/4).
Row Source determines the source of the combo box. To use only the selected
fields as a source, use a query.
Bound Column determines which of the Row Source columns will be used as
the combos value.
Column Count determines how many of the Row Source columns will be
displayed.

Figure 6-13: Design view

114

Database Basics with MS Access

Figure 6-14: Design view, Lookup tab

As shown in Figure 6-14/2, click the ellipses symbol (...) at the far right side of
the Row Source property (Figure 6-14/1). The Query Builder window opens.

Figure 6-15: Query builder dialog box

From the table list (Figure 6-15/1), double-click the Books table to add it to the
query design (Figure 6-15/2). Double-click the BookID and Title fields (Figure
6-16/1); add these fields to the design grid (Figure 6-16/ 2) and then close the
Query Builder to return to the Table design view. Optionally, you can select
the Ascending sorting option (Figure 6-16/3).

Chapter 6 / Relationships

115

Figure 6-16: Table design view

Set the column Count option to 2, Column Widths option to 0,2 (Figure 17).

Figure 6-17: Table design view

116

Database Basics with MS Access

For the AuthorID field, repeat steps 3, 4, and 5 to set the Row Source query
as seen in Figure 6-18.

Figure 6-18: Query builder

For the AuthorID field, set the Lookup Field Properties to (Figure 6-19): Bound
Column: 1; Column Count: 2; Column Widths: 0,2.

Figure 6-19: Table design view

Save and close the table design view and open your BooksAuthors table form
the Navigation Pane to see the results.

Chapter 6 / Relationships

117

Figure 6-20: BookAuthors table

Compare the previous table (Figure 6-12) with the new one (Figure 6-20). Instead
of just seeing numbers, you now see information. It is also now more comfortable
with the combo box controls for data entry.

Establishing Relationships
In order to relate the tables:

Figure 6-21: Database tools tab

Select the Database Tools tab (Figure 6-21/1), and then click the Relationships
button (Figure 6-21/2).
Click the Show table button or right-click and select the Show tables option.
The Show Table dialog box opens (Figure 6-22).

Figure 6-22: Show table dialog box

118

Database Basics with MS Access

Add the tables by double-clicking the tables or selecting the table name and
then clicking the Add button.

Figure 6-23: Relationships window

Drag the Key field of the first table over the foreign key of the second table. You
will see the Edit Relationships dialog box (Figure 6-24).
Select the Enforce Referential Integrity option and press. Enter to accept the
settings and make relationships.

Figure 6-24: Edit relationships dialog box

Chapter 6 / Relationships

119

Data integrity rules


Data integrity rules help you ensure the consistency of your data. In the Edit
Relationships dialog box (Figure 6-24), you will see three integrity rules:
Enforce Referential Integrity
Cascade Update Related Fields
Cascade Delete Related Records.
Referential integrity
The following rules apply when you use referential integrity:
You cannot enter a value in the foreign key field of the related table that does
not exist in the primary key of the primary table.
You cannot delete a record from a primary table if the matching records exist
in a related table.
You cannot change a primary key value in the primary table if that record has
related records.
Cascading updates and deletes
Cascade Update ensures that any time you change the primary key of a record
in the primary table, Access automatically updates the primary key to the new
value in all related records.
Cascade Delete ensures that any time you delete records in the primary table,
Access automatically deletes related records in the related table.
Join types
In the Edit Relationships dialog box, when you click the Join types command
button, the Join Properties dialog box opens (Figure 6-25):

Figure 6-25: Join type dialog box

1: Choose this option if you want to see one record in the second table for every
record that appears in the first table.

120

Database Basics with MS Access

2: Choose this option if you want to see all the records in the first table (even if
there is no corresponding record in the second table) as well as the records from
the second table in which the joined fields are the same in both tables.
3: Choose this option if you want to see all the records in the second table (even if
there is no corresponding record in the first table) as well as the records from the
first table in which the joined fields are the same in both tables.
Editing and Deleting Relationships

Figure 6-26: Relationship window

If the Relationships window is not open, from the Relationships tools on the ribbon:
Click the Relationships button. If related tables are not visible:
Click the Show table button and display the tables and then double-click the
relationships line between the related tables and open the Edit Relationships
dialog box and change the options you want.
To delete the relationships:
Click the relationships line between the related tables and press the Delete
key.

Using Subform and Subreport


A form or a report can be bound to only one table at a time. When there are several
related tables in a form or report, you need to use the subform and subreport
controls.

Chapter 6 / Relationships

121

About Subforms
A subform is a form inside another form. The primary form is called the main or
parent form, and the form that is inserted in the main form is called the subform
or child form.
The two tablesStudents and Paymentsare related to each other through the
one-to-many relationship. In one form, you want to display a students information
and payments. To design the form, you can make a main form that displays the
students information and add a subform that displays the students payments.
The data in the Students table is the one side of the relationship. The data in the
Payments table is the many side of the relationship. Each student can have more
than one payment.

Figure 6-27: An example form using one-to-many relationships

Make a Form that Contains a Subform using the Form Wizard


Access makes both a main form and a subform and links the subform to the main
form automatically.
Click the Form Wizard button on the Create tab in the Forms group.

Figure 6-28: Create ribbon menu

122

Database Basics with MS Access

On the first window of the wizard, in the Tables/Queries drop-down list, select
a table or query. For this example, select Table: Students.
Double-click the fields that you want to add.
On the same page of the wizard, in the Tables/Queries drop-down list, select
another table or query from the list. For this example, select the Payments table
(the many side of the one-to-many relationship). Double-click the fields that
you want to include from this table.
The wizard asks How do you want to view your data?
Select the table on the one side of the one-to-many relationship: Students
table.
Note: If the wizard does not ask How do you want to view your data? that
means that Access did not detect a one-to-many relationship between the tables
or queries that you have selected. The wizard will continue, but Access will not
add a subform to the form. You might want to click Cancel and examine your
table relationships before you continue.

Figure 6-29: Form wizard dialog box

At the bottom of the wizard page, select Form with subform(s), and then click
Next.
On the next dialog box you will be asked What layout would you like for your
subform? select the datasheet view.
On the last page of the wizard, enter the titles for the forms and then click Finish.
Access makes two forms: one for the main form that contains the subform
control and one for the subform itself.

Chapter 6 / Relationships

123

Make a Subform from the Beginning


Subform/Subreport control inserts a form into a form or a report into a report
and enables the main form or report to use fields from the other tables or queries.
The Source Object property of the subform control indicates what object (table,
query, form, or report) is displayed in the control.
The Link Master Fields property of the subform control specifies which field or
fields on the main form link the main form to the subform.
The Link Child Fields property of the subform control indicates which field or fields
in the subform link the subform to the main form.
Open the Form that will be used as a main form in the design view.
Make sure that the Use Control Wizards
group is not selected.

on the Design tab in the Controls

Click and drag a subform. If a wizard starts, click the Cancel button and close
it.
Right-click the subform control and select Properties to display the Property
sheet dialog box or press F4 to display it.

Figure 6-30: Setting source object

Click the Data tab.


Click the drop-down menu next to the Source Object and select the Payments
table.
If there is no relationship between the Students and Payments table:
Click
next to the Link Master Fields property box and select the Primary
key of the one side of the one-to-many relationships (here, StudentID field of
the Students table).

124

Database Basics with MS Access

Click
next to the Link Child Fields property box and select the Foreign key
of the many side of the one-to-many relationships (here, StudentID field of the
Payments table).

Figure 6-31: Example form designs using many-to-many relationship tables

Project I: From Flat-File to Relational Database


A single table (flat-file database design) has fields such as name, surname, address,
city, school fee, payment amount, payment description, and payment date for a
school database. Design the same database as a relational type.
a. Design a form to enter and display students payments.
b. Design a report to prepare a receipt voucher to give to students after payment.
c. Design a report to prepare the extract (statement of payments) included in the
calculated controls to display the total payment and the loan.

Project II: Designing a Relational Database


Design a relational database based on the tables StudentsTbI and StudentsTbII.
a. Develop forms for the data entries.
b. Design a two-page report that displays information from the StudentsTbI on
the first page of the report and information from the StusentsTbII on the second
page of the report.
c. Design the same database in a single table format based on the Students table.

Chapter 6 / Relationships

125

Project III: School Library


Design a relational database to keep track of the books in a school library.
a. Develop forms for data entries.
b. Design a report to list the books.
c. Design a report to list the authors.
d. Design a report that prompts the ISBN number of the book and displays all the
information about that book.

126

Database Basics with MS Access

Test Questions
1.

In a one-to-one relationship, which of the


following can be set for the related fields?

4.

I. Key Field

I. one-to-one

II. Indexed ( duplicates OK) field

II. many-to-many

III. Indexed (No duplicates) field

III. one-to-many

a. Only I

a. Only I

b. I and III

b. Only III
c. Only II

c. Only II

d. II and III

d. Only III

e. I and III

e. II and III
5.
2.

In MS Access, which type of the relationships


cannot be established directly?

In a one-to-many relationship, a foreign key


field cannot be __________________.

In a _______________ relationship, a record in


one table can have only one related record in
the related table.
I. one-to-one

I. Key Field

II. many-to-many

II. Indexed (Duplicates OK) field

III. one-to-many

III. Indexed (No duplicates) field

a. Only I

a. Only I

b. Only II

b. I and II

c. I and III

c. Only III

d. Only III

d. I, II and III

e. II and III

e. II and III
6.
3.

Matching fields in a relationship:


I. can have different field names
II. must have the same data type

Between the Students information table and


the Students payment table, there can be
____________ relationships.
I. one-to-one
II. many-to-many

III. can have different positions in the tables

III. one-to-many

a. I, II and III

a. Only I

b. Only III

b. II and III

c. Only I

c. I and III

d. I and III

d. Only II

e. Only II

e. Only III

Chapter 6 / Relationships

127

Test Questions
7.

Match the following descriptions.


I - is easy to design and manage.
A - Flat-file database
II - provides a way to not duplicate the records.
B - Relational database
a. I-A, II-A
b. I-B, II-A
c. I-B, II-B
d. I-A, II-B
e. None of them

8.

In a one-to-many relationship, if one side of the


related fields is AutoNumber, the related field
(foreign key) side can be the _______________
type.
a. AutoNumber
b. Text
c. Long Integer
d. Byte
e. Memo

9.

Between Students and Subjects (Courses)


tables, there can be _________________
relationships.
I. one-to-one
II. many-to-many
III. one-to-many
a. Only III
b. Only II
c. Only I
d. I and II
e. I and III

128

Database Basics with MS Access

10. Between Passengers and Seats (Travel


Coupons) tables there can be ________________
relationships.
I. one-to-one
II. many-to-many
III. one-to-many
a. I and II
b. Only I
c. Only II
d. Only III
e. II and III

Chapter 1

Chapter 2

Chapter 3

1. C

8. D

1. D

8. A

1. D

2. C

9. A

2. A

9. A

2. C

3. D

10. B

3. A

10. D

3. B

4. A

4. A

11. D

4. C

5. B

5. B

12. C

5. D

6. D

6. C

13. E

6. E

7. B

7. A

Chapter 4

Chapter 5

1. A

1. E

1. C

8. C

2. D

2. C

2. B

9. B

3. B

3. C

3. A

10. D

4. D

4. D

4. C

5. A

5. B

5. A

6. E

6. A

6. E

7. A

Chapter 6

7. D

A
Action Queries 68
Allow Zero Length 34
Append query 68
Attachment 25, 53
AutoNumber 25
Auto Report 97

Delete query 68
Design view 31
Detail 95
DistinctCount 28
Double 25

I
Identifiers 61
Image 53
Indexed 34
indexed fields 27
Input Mask 34, 36
Integer 25

expression 61
Expression Builder 39

Blank Report 96
Byte 25

key field 30

Field List 57
Field properties 28
Field Properties 33
fields 9
Field Size 34
field validation rules 38
Filter 40
Filtering 40
Find and Replace 42
Find Duplicate Query 68
Find Unmatched Query 68
Flat-file database 108
Foreign key 110
Format 34
form controls 53
Form controls 58
Form Design 54
Form properties 59
Forms 9, 10, 52
functions 74
Functions 61

Group Footer 96
Group Header 96

OLE Object 25
ONE-to-MANY 109
ONE-to-ONE 109
Operators 61
Option Button 53

C
calculated control 101
calculated field 78
Calculated Field 46
Caption 34
Cascade Delete 120
Cascade Update 120
Check Box 53
columns 9
Combo Box 53
Constants 61
Criteria 40
Crosstab query 68
Currency 25

D
Data 6
database 6
database management system 6
Data Entry 36
Data integrity rules 120
Datasheet 54
Datasheet view 31
Data Types 25
Date/Time 25
Decimal 25
Decimal Places 34
Default Value 34

H
Hyperlink 25, 53

Label 53
Link Child Fields 124
Link Master Fields 124
List Box 53
logical operations 75
Long Integer 25
Look up Wizard 42
Lookup Wizard 25

M
Macros 9
Macros and Modules 12
MANY-to-MANY 109
Memo 25
Modules 9
MS Excel 6

N
Navigation pane 13
Number 25

Page Footer 95
Page Header 95
Parameter Query 79
primary key 29
Primary key 110
Property sheet 57

Tables 9
Template 36
Templates 16
Text 25
Text Box 53
The Navigation Pane 16

Q
Queries 9, 10
query 68
Query wizard 69
Quick access toolbar 13
Quick Access toolbar 18

R
records 9
record validation rules 38
Referential integrity 120
relational database 29
Relational database 108
relationships 108
Replication ID 25
Report 96
Report Design 96
Report Footer 96
Report Header 96
Reports 9, 11, 94
Report Wizard 96, 97
Required 34
ribbon 13
Rows 9

S
Select query 68
Single 25
Sort Ascending 39
Sort Descending 39
Sort & Filter 40
Source Object 124
Split Form 55
subform 122
Subform/Subreport 124

U
Unbound Object Frame 53
unique 25
Unique 35
Unique index 28

V
Validating Data Entry 38
Validation Rule 34, 62
Validation Text 34
Visual Basic Access (VBA) 12

W
Wildc ards 76

Y
Yes/No 25

S-ar putea să vă placă și