Professional Documents
Culture Documents
S YS T E M
Seluk CANKURT
Mkremin ZKUL
http://book.zambak.com
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.
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
Reports ...........................................................................11
Options............................................................................55
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.
Book Catalogue
That provides a simple interface for browsing and accessing any book in a library.
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.
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
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.
11
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
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.
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
13
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
14
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,
15
Clicking the close button on the far right side of the title bar,
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
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).
17
3
Figure 1-19: Right-clicking a
database object in the navigation
pane
Figure 1-20: An open table
18
19
20
Test Questions
1.
5.
b. Dictionary
c. This textbook
d. Address book
d. It is an application program
e. Inventory list
2.
6.
b. Copying files
a. Form
b. Report
c. Macro
e. Compressing data
d. Menu
e. Table
3.
4.
7.
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. I and III
II. Form,
III. Query,
IV. Report
21
Test Questions
8.
a. Only IV
c. I and II
d. I and III
e. Only I
22
IV. Click the File menu and select the Open option
b. Only II
9.
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
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.
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
Chapter 2 / Tables
25
26
Student ID
Name
Surname
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
Chapter 2 / Tables
27
28
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
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
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.
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
32
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
OK)
FathersName, MothersName, City fields: set Field size to 50, Required to
No, Indexed to Yes (Duplicate OK)
Chapter 2 / Tables
33
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
Input Mask
Decimal Places
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
Chapter 2 / Tables
35
36
6
1
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).
<
>
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
Examples of values
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
8
7
3
2
6
5
4
38
Description
<>0
1 or >5
<#1/1/2010#
if you
if you
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).
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
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.
Chapter 2 / Tables
41
command.
The Replace tab is used to change the data found by the the Find command to
the data in the Replace text box.
42
In the Design view when you select Lookup Wizard in the Data Type column.
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.
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).
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
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).
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.
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
5. When you are finished typing the calculation expression, click OK.
After you enter values for the UnitPrice and Discount fields, SellingPrice field will
be calculated and displayed automatically.
Chapter 2 / Tables
47
Test Questions
1.
5.
a. Field
b. Category
c. Attribute
2.
a. List
b. Memo
c. Text
d. Long text
e. Number
d. Record
e. Column
6.
b. Indexed field
c. Field names
d. Field properties
e. Key fields
b. Memo
c. Bounded Picture
d. List
7.
e. Text
3.
a. Indexed fields
b. Queries
c. Macros
d. Relationships
e. Lookup fields
a. Byte
b. Integer
c. Decimal
d. Long Integer
e. Single
8.
4.
48
c. Text
b. III, V and VI
d. OLE
c. III and IV
d. I, II and III
e. Date/Time
e. V and VI
Test Questions
9.
c. Query
d. Macros
d. AutoNumber field
e. Calculated field
e. Birth date
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.
A form contains a Form Header, Detail, Form Footer, Title bar, and Status bar.
52
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
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
Method, Option
Icon
Description
Form
Form Wizard
Datasheet
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
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.
or Multiple Items
, Split
option.
Chapter 3 / Forms
55
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.
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
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.
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
58
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.
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.
After you finish laying out the controls on your form, you should also define
sources for unbound controls.
Chapter 3 / Forms
59
60
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]
=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]
=Avg([Score])
Mesut
Chapter 3 / Forms
61
Expression
XXL
XXL
Istanbul
Istanbul
Date() + 7
62
<> 0
0 Or > 5
Like S????
< #1/1/2010#
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.
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
64
Test Questions
1.
5.
a. List box
a. Label
b. Option box
b. Navigation
c. Check box
2.
c. Input box
d. Menu
d. Check box
e. Textbox
6.
a. a macro
I. Text Box
b. a table field
c. a table
d. a form control
e. a report
V. Option Box
a. I, II and III
3.
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.
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.
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
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).
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
In the next dialog box, name your query (Figure 4-5) and click the Finish button.
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
The query design window and Show Table dialog box are displayed (Figure 4-7).
Chapter 4 / Queries
71
72
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
Chapter 4 / Queries
73
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
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).
Use the logical operations and, or, and not to combine your criteria.
The query returns those records in which the Quantity value is bigger than 0 and
the UnitPrice field equals $10.
Chapter 4 / Queries
75
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
Query result
Turkey
Right([Name], 1) = a
< 50
<= 100
Not #2/1/2010#
Date()
76
Character
Name
Description
Asterisk
Question mark
[]
Brackets
Exclamation point
Query result
Like M*
Not Like S*
Like *y
Like [A-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).
Chapter 4 / Queries
77
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]
Run your query to see similar results as those illustrated Figure 4-18.
78
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
Chapter 4 / Queries
79
between
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
The query displays all the records that fall into the dates
between 1/1/2010 and 31/12/2010.
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.
When a user enters a, all records whose name starts with the letter a will be
listed.
Chapter 4 / Queries
81
Choose the Structure And Data option and then click OK.
The new table is identical to the original, including the data.
82
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.
Chapter 4 / Queries
83
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.
84
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.
Chapter 4 / Queries
85
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.
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
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.
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
Chapter 4 / Queries
89
Test Questions
3.
1.
b. display
c. modify
d. update
e. add
4.
I. The upper part displays the field list
a. retrieve
b. display
c. select
d. modify
e. print
b. II and III
c. I and II
d. I and III
5.
e. II and III
2.
b. Sort
c. Show
d. Field
e. Calculated field
6.
90
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?
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.
94
Chapter 5 / Reports
95
Report Header
Page Header
Group Header
Detail
Group Footer
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
Report 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
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.
98
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.
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.
To add a control, click the tool for the control you want; and then click on the
form where you want to position it.
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.
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.
You can rename the label controls to name the data fields. Double-click a label
and type new names.
100
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.
Chapter 5 / Reports
101
To add page information, in the Header/Footer group, click the Page Number
button.
102
Click the Date and Time button; the Date and Time dialog box opens.
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
104
Test Questions
1.
4.
a. Forms
b. Modules
c. Tables
d. Queries
e. Reports
2.
5.
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.
6.
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
e. I, II and III
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.
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.
108
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
Chapter 6 / Relationships
109
110
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.
Chapter 6 / Relationships
111
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
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.
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.
Chapter 6 / Relationships
113
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.
114
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.
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
Set the column Count option to 2, Column Widths option to 0,2 (Figure 17).
116
For the AuthorID field, repeat steps 3, 4, and 5 to set the Row Source query
as seen in Figure 6-18.
For the AuthorID field, set the Lookup Field Properties to (Figure 6-19): Bound
Column: 1; Column Count: 2; Column Widths: 0,2.
Save and close the table design view and open your BooksAuthors table form
the Navigation Pane to see the results.
Chapter 6 / Relationships
117
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:
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).
118
Add the tables by double-clicking the tables or selecting the table name and
then clicking the Add button.
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.
Chapter 6 / Relationships
119
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
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
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.
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.
122
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.
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
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.
124
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).
Chapter 6 / Relationships
125
126
Test Questions
1.
4.
I. Key Field
I. one-to-one
II. many-to-many
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.
I. Key Field
II. many-to-many
III. one-to-many
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.
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.
8.
9.
128
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