Download as pdf or txt
Download as pdf or txt
You are on page 1of 165

INTRODUCTION TO DATABASES

AND INFORMATION ANALYSIS


USING MSACCESS

MS ACCESS Level-1 Course

MS ACCESS Level-1 1
Table of Contents

1.0 Introduction to MS Access.............................................................................................4


1.1 Database Concept.......................................................................................................4
1.2 A Database Example..................................................................................................4
1.3 Relational Database Management Systems (RDBMS)..............................................5
1.4 Starting MS Access....................................................................................................6
1.5 Objects of the Database Engine.................................................................................8
2.0 Tables and Data..............................................................................................................9
2.1 Creating Tables Using the Wizard.............................................................................9
2.2 Data Sheet View......................................................................................................11
2.3 Creating Tables Manually........................................................................................14
2.4 Primary Keys...........................................................................................................18
2.5 Filtering for specific Data........................................................................................21
2.6 Using the Sort..........................................................................................................24
2.7 Changing a Table Design.........................................................................................27
2.8 Changing Data in a Table........................................................................................27
3.0 Introduction to Queries................................................................................................28
3.1 Different types of Queries........................................................................................28
3.2 Select Query.............................................................................................................28
3.2.1 Create query by using wizard...........................................................................29
3.2.2 Create query in design view..............................................................................36
3.3 Opening Query in Design View...............................................................................41
3.4 Queries and Criteria.................................................................................................46
3.5 Sorting in Queries....................................................................................................51
3.6 Append Query..........................................................................................................59
3.7 Update Query...........................................................................................................66
3.8 Delete Query............................................................................................................71
3.9 Queries and Relationships........................................................................................73
3.10 Group by and aggregate functions.........................................................................83
4.0 Introduction to Forms..................................................................................................91
4.1 Creating Forms using the Wizard............................................................................91
4.2 Editing the data in the table using the Form............................................................96
4.3 Adding a new Record to the Table using the Form.................................................97
4.4 Other Form Layouts.................................................................................................99
4.5 Creating Forms in Design View............................................................................103
5.0 Introduction to Reports..............................................................................................112
5.1 Creating a report by using the wizard....................................................................112
5.2 Creating Groups.....................................................................................................118
5.3 Modifying report design........................................................................................130
5.4 Creating a report in design view............................................................................139
5.5 Sorting an Grouping...............................................................................................155

MS ACCESS Level-1 2
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________

MS ACCESS Level-1 3
1.0 Introduction to MS Access
1.1 Database Concept

A database is a collection of data, or information. A database management system


(DBMS) is a set of programs used to store and organize data and to make data retrieval
efficient. Mirosoft Access is a DBMS where it can be used to create databases and then to
use it as an efficient tool to retrieve data from the database in ways that serve the Analyst
purposes.

1.2 A Database Example

A simple table is considered a database because it contains data and information. The
table shown below helps in identifying some database related terminology.

Fields

Name Family Name Grade Class


Mohammed Al Mamari 90 Advanced Mathematical Methods
John Depak 78 Advanced Mathematical Methods
Abdullah Al Ismaili 50 Advanced Mathematical Methods
Noor Al Harthy 80 Advanced Mathematical Methods
Salim Al Siyabi 87 Advanced Mathematical Methods
Yousef Al Harthy 59 Advanced Mathematical Methods
Talal Al Fanah 67 Engineering Drawing
Vijay Sharma 89 Engineering Drawing Records
Manoj Kumar 90 Engineering Drawing
Ismail Al Sulaimani 96 Engineering Drawing
Sulaiman Al Asmi 95 Engineering Drawing
Sameer Al Asmi 85 Engineering Drawing
Ali Al Asmi 78 Engineering Drawing
Farzin Amit 69 Islamic Studies
Aya Al Siyabi 89 Islamic Studies
Maha Al Farsi 90 Islamic Studies
Muna Al Ramdhany 88 Islamic Studies

Data value
Figure 1-1

A Field is a specific type of information. The ‘Name’ column above is a field.


A Record is a single set of related data. The row starting with Noor above is considered a
record.
A Data value is any item of data in any record or under any Field.
The above data structure is called a Table.
MS Access uses tables to store information.

MS ACCESS Level-1 4
1.3 Relational Database Management Systems (RDBMS)

The below example introduces the concept of RDBMS.


Name Family Name Grade Class Age Address Date of Joing University
Mohammed Al Mamari 90 Advanced Mathematical Methods 20 Al Aziba 1-Sep-02
John Depak 78 Advanced Mathematical Methods 21 Qurum 1-Sep-01
Abdullah Al Ismaili 50 Engineering Drawing 21 Matrah 1-May-01
Mohammed Al Mamari 80 Engineering Drawing 20 Al Aziba 1-Sep-02
John Depak 87 Engineering Drawing 21 Qurum 1-Sep-01
Figure 1-2

This is a simple table of students’ information. This table contains 7 fields and 5 records.
A careful examination of the table reveals that the Age, Address and Date of Joining
University data values for the first 2 students have been repeated twice.

In a relational database management system (RDBMS), this situation can be avoided.

The same above data can be stored in the tables:

ID Name Family Name Grade Class


1 Mohammed Al Mamari 90 Advanced Mathematical Methods
2 John Depak 78 Advanced Mathematical Methods
3 Abdullah Al Ismaili 50 Engineering Drawing
1 Mohammed Al Mamari 80 Engineering Drawing
2 John Depak 87 Engineering Drawing

ID Age Address Date of Joing University


1 20 Al Aziba 1-Sep-02
2 21 Qurum 1-Sep-01
3 21 Matrah 1-May-01

Figure 1-3

By using a student ID, the same data now can be captured in the tables avoiding any
unnecessary redundancy of the data. Such IDs are called Primary Keys. More explanation
of this concept will come later in the course.

A relational database management system (RDBMS ) is a type of DBMS where the data
is stored on a set of tables that can relate to each other to achieve no data redundancy and
a more efficient data retrieval.

MS ACCESS Level-1 5
1.4 Starting MS Access

From the start menu choose Microsoft Access or double click the Access Icon to start the
MS Access RDBMS.

Figure 1-4

The below screen should appear. Click on the New Button or in the left side pane click
the Create a new file ... (Both circled in red in Figure 1-5 below).

Figure 1-5

After clicking on the new button, the below screen shown in Figure 1-6 should appear.

MS ACCESS Level-1 6
Figure 1-6

Click on Blank database … The ‘File New Database’ form will appear requesting the
user to specify the name and the location where the database is to be stored. After
defining the name and the location click the create button.

Figure 1-7

MS ACCESS Level-1 7
After clicking the Create Button the Main database window will appear. Note that this
window carries the name of the database we created (in the small circle in Figure 1-8).

Figure 1-8

This window is the general interface of the MS Access System. Using this interface,
tables can be created to store data. Different tools are also available to analyze and
retrieve data in the way that suits the analyzer.

1.5 Objects of the Database Engine

The database engine has the following Objects (Circled in Figure 1-8):

• Tables
• Queries
• Forms
• Reports
• Pages
• Macros
• Modules

Clicking any of these objects gives different options related to the selected Object.
Tables, Queries, Forms and Reports will be covered in details in this course.

MS ACCESS Level-1 8
2.0 Tables and Data
Tables in databases are the containers that hold the data. MS Access gives the designer
the capability to create and populate tables in an easy manner.

2.1 Creating Tables Using the Wizard

MS Access gives the capability of simple creation of tables to serve a special purpose
without the need of going into the details of designing a table from scratch.

In order to use the Wizard in creating a table, follow the following steps:

1. Open your database


2. Under Objects, click on Tables
3. Double click ‘Create table by using wizard’, the screen in figure 2-1 should
appear

Figure 2-1

There are some pre stored table templates that a designer can choose from in two
different categories: Business and Personal.
Choose the category you want and the Sample Tables list will reflect table samples
related to the chosen category.
The Sample Fields will change to reflect fields related to the chosen table sample.
To create the table choose the field that you want by clicking the field and then clicking
the > button.

MS ACCESS Level-1 9
Clicking the >> button chooses all the fields at once without having to select them one by
one.
You can unselect fields by clicking the < or << buttons.
A Field name can be changed by clicking the Rename Field .. button.
After selecting all the fields you find suitable for your purposes click the Next Button.
The Screen in Figure 2-2 will appear.

Figure 2-2

Choose a name for the table and type it in the ‘What do you want to name your table?’
box. The system also gives you the option to create a primary key. Primary keys will be
discussed later on in the course. Choose the ‘Yes, set a primary key for me.’ Option and
click the Next button. The ‘No, I’ll set the primary key.’ Option could have been chosen
as will without affecting the purposes of this exercise. Both options will become clear to
you later in the exercise.

After clicking the Next Button the screen shown in Figure 2-3 will appear.

MS ACCESS Level-1 10
Figure 2-3

Here three options are available:

1. Modify the table design – This option will be covered in the next section
2. Enter data directly into the table
3. Enter data into the table using a form the wizard creates for me – This option
will be covered later in the course

Select option 2 and click the Finish Button.


The System will then Open the Table for you in the Data Sheet view. Here you can start
entering the data into the table.
If you have selected the option for setting a primary key, you will notice that the system
has created an additional field that we did not originally choose from the field list. This
field is the primary key field that we instructed the system to set for us. This will become
clearer later in the course.

2.2 Data Sheet View

Start entering data into the table skipping the primary key field.

Very important note:

While entering a data into a table, the data will not be stored in the record as long as the
pen sign is on. Have a look at figure 2-4.

MS ACCESS Level-1 11
Figure 2-4

To store the data into the record you have to move to the next record. By moving to the
next record the data will be stored into the table and the pen sign will disappear.
Look at figure 2-5 below.

Moved to the next record and


the pen sign disappeared

Figure 2-5

The view in figure 2-5 is called the Table Data Sheet view. In this view you see the table
in a sheet format and you can add, modify or delete data.

This view also contains the navigation controls at the bottom which enables the designer
to go through the data quickly and to add new records.

Have a look at figure 2-6.

MS ACCESS Level-1 12
Total number of records
Create a new record
Goto the last record in the table
Goto the next record in the table
Current Record
Goto the previous record in the table
Goto the first record in the table
Figure 2-6

The box to the left hand side of each record is called the record selector. Figure 2-7
shows the record selectors.

Record
Selectors

Figure 2-7

You can select one or multiple records to become the active record(s) by clicking on
its/their record selector(s). Note how the selected record or records are highlighted to
indicate that they are the active records. Have a look at Figure 2-8.
Once a record or records is/are active, they can be deleted, copied or moved in the table
to a different position.

MS ACCESS Level-1 13
Figure 2-8

Any time data is clicked (a field in a record), that record becomes the active record. The
record selector reflects this fact by showing the triangle sign. Any time the data is being
modified, the record selector shows the pen sign which reflects that data is being
modified and still not stored.
As soon as another record is selected, the data will be stored in the database and the pen
sign will disappear from the record that was modified.

2.3 Creating Tables Manually

Tables can be created manually from scratch and without having to use the Wizard. This
gives the designer more flexibility in designing the table to the required purposes.

To create a table manually follow the steps below:

From the objects click on tables and then double click the ‘Create table in Design view’,
the following screen should appear.

MS ACCESS Level-1 14
Figure 2-9

This view is called the Design view. Here the table fields can be defined.

Suppose that we want to design a table that will contain employee information. As a
designer for the database, you need to determine the different information elements
(fields) that need to be stored about each employee. Let’s say that after some
conversation with the HR head you came up with the following list:

1. First Name
2. Last Name
3. Grade
4. Salary
5. Position
6. Date Joined Company
7. Married

Then you need to determine the type of each identified field. By Type we mean, is it an
integer, single, double, text, date/time, Boolean ..etc.

Suppose that we have decided on the following:

1. First Name – text (15 characters)


2. Last Name – text (20 characters)
3. Grade – text (4 characters)
4. Salary – Number (single)

MS ACCESS Level-1 15
5. Position – text (25 characters)
6. Date Joined Company – date/time
7. Married - Boolean

Now we need to map the fields and their types we just identified into the design view
screen.

Figure 2-10

Figure 2-10 shows how the design view would look like after mapping all the fields of
the table. Notice how the first field type is text and its size is 15.
Figure 2-11 shows the Salary field and its chosen size (single).

MS ACCESS Level-1 16
Figure 2-11

After finishing the above task of defining the fields and their types the table must be
saved so click the save button .
You will be prompted to enter a name for the table. Lets call it Employee Info, so type in
Employee Info in the box and press Ok.
The system will then ask you, if you want to create a primary key for the table, click No.
(More about primary keys will come shortly).

Close the table by clicking on the button. The general database interface will now
show and the table we just created will be displayed in the table list as shown in figure 2-
12 below.

MS ACCESS Level-1 17
Figure 2-12

To enter some data into the table, double click the table name and the table will open in
the data sheet view previously discussed. Now data can be entered into the table.

2.4 Primary Keys

A primary key is a field or multiple fields in a table that have a unique value for each
record. Setting a primary key in a table can be done from the Design view.
Let’s have an example. Suppose that we need to add a new Field to the table that we
created before called EmployeeID. This field will have a different ID for each employee
and we want to set the EmployeeID field as our primary key for the table, then we need
to do the following steps:

1. Click the Employee Info Table to select it

2. Click the Design View button


3. The table will be opened in the design view as in figure 2-10 above.
4. Go to the row under the Married Field
5. Type in EmployeeID and choose it to be number – long integer

Have a look at figure 2-13 below.

MS ACCESS Level-1 18
Figure 2-13

6. Select the EmployeeID Field by clicking at the field selector (The box that
contains the triangle)
7. Click the Primary Key Button to make this field the primary key field
8. Save the table

Have a look at figure 2-14.

MS ACCESS Level-1 19
Figure 2-14

Since the employeeID field is defined as a primary key, while entering data, you will not
be allowed to have a similar value for this record in two different records. Also you will
not be allowed to keep this field without a value. It has to have a value for each record in
the table.

If there is a need to have multiple field primary key then this can be done by selecting
two fields in the design view and then clicking the primary key button.

To select more than two fields, then select the first field and hold down the Ctrl key and
then select the other field. After that click the Primary Key button.

In the example below in figure 2-15, two fields have been chosen as primary keys.

MS ACCESS Level-1 20
Figure 2-15

Notice the primary key sign beside each one of the fields. In the example above you can
have similar values in the first name field for different records and you can also have the
same EmployeeID value in different records. But you cannot have both the same at the
same time. Note that you cannot leave any one of the primary key fields null (empty).

2.5 Filtering for specific Data

Suppose you have a table with thousands of records, how can you locate the data that you
want inside such a huge table? Access provides tools for the user to locate the desired
data quickly and easily inside a table.

To illustrate Access capabilities in this respect, let’s take the following example. Suppose
that you have a table for employees information and that it contains the information
shown in figure 2-16 below.

MS ACCESS Level-1 21
Figure 2-16

This table contains 12 records only and finding the data needed is very easy since the data
volume is so low. But the principles illustrated here would still work even if the table
contained thousands or even millions of records.

Suppose you need to see the people whose first name is Mohammed. To do that, it is
really easy. With the mouse move to the ‘First Name’ field and right click on any record.
The menu shown in figure 2-17 will be displayed.

Figure 2-17

In the ‘Filter For:’ fill in Mohammed (because we want to display the records where the
first name is Mohammed). Figure 2-18 shows how it would like.

MS ACCESS Level-1 22
Figure 2-18

Now press enter. The table would show only those records where the first name is
Mohammed as shown in figure 2-19 below.

Figure 2-19

In order to remove the filter that we just applied. Click on the remove filter button in
the tool bar or right mouse click anywhere inside the table and choose the remove
filter/sort option from the menu that would appear. The table would be restored to its
original position as shown in figure 2-16 (No filters and all the records showing).

Exercise: Filter for the Engineering staff. Then for the Sales staff. How many people are
there in each department?

Suppose now we want to see all the people whose salary is greater than 1000, all we need
to do is use the following criteria in the filter: >1000.

MS ACCESS Level-1 23
If we want to see the people whose salaries are 1000 or above then use the following in
the criteria: >=1000.

Suppose we want to see all the people who joined the company before 10-Feb-2006, then
use the following in the criteria: <=10-feb-2006.

Suppose we need to see the people who are married. Then in the married Field filter for:
yes. Filter for No to see all the unmarried people.

Suppose you want to see all the people whose Family Name has the letter d, then use the
following criteria for the Filter under the Family Name field: *d*

Note that we can apply multiple filters, for example suppose we want to see all the people
whose Salary is less than a thousand and who joined the company after 1st-Feb-2006.
Then in the Salary field filter for: <1000. Then after performing this, filter for >=1-feb-
2006 in the Date Joined Company field.

As you can see from these examples, Access can dig out the required information quickly
and efficiently.

This is a very valuable tool for any information analyst, especially if there are millions of
records to be analyzed.

Note that each time the filter is engaged, the total number of records is shown at the
bottom of the table. (Refer to Figure 2-6).

As you can Filter for a value, you can also filter to exclude a value. Suppose you want to
filer for all the non-Engineering staff. Then under the Department field, right mouse
click on a record that contains Engineering as data. Then choose Filter Excluding
Selection. This will exclude all the records that has Engineering under the Department
field.

You can also use the Filter By Selection, which will Filter the table for the value that is
selected just before engaging the Filter By Selection option.

2.6 Using the Sort

A table can be sorted using any field. Suppose in our previous examples you need to sort
the table in ascending order by salary, so the person with the lowest salary will be shown
as the first record and then one immediately above him as the second and so on. This is
what you need to do, open the table. Select the field you want to sort by, in this case the
Salary field by clicking on Salary column as showing in figure 2-20 below.

MS ACCESS Level-1 24
Now after highlighting the needed column as shown in Figure 2-20, click on one of the
sort buttons. is the sort Ascending button and is the sort Descending button.
After clicking on one of these you can see the whole records arranged by the required sort
sequence.

Another way to activate the sort is to click the right mouse button after highlighting the
required column. This will show a list from which you can choose the sort buttons as
shown below in Figure 2-21.

Sort can also be applied to multiple fields as long as they are adjacent. Suppose you want
to sort by the Salary and then by the First Name. In this case we need to make the
Salary column and the First Name column adjacent first. This can be done by

MS ACCESS Level-1 25
highlighting the Salary column and then moving the mouse on the column name until the
mouse pointer is on the column name. Now press and hold the mouse left button down
and drag the column to the beginning of the table. Please note that you can drag the
column and place it in any place you like. In our case we want it to be at the beginning of
the table.

Now since the Salary column and the First Name column are adjacent, select both of
them by selecting the first one and while continuing to hold the mouse button down,
select the other one, until both of them are highlighted as shown in Figure 2-22 below.

Now press the Sort Ascending button, this will result in the records sorted by the Salary
and then the First Name as shown in Figure 2-23.

As you can see from the above the data is sorted based on the two columns. If you can
see Ayshah comes before John though they both have the same Salary. The sort will
result in using the first column as a priority column for sorting and the second column

MS ACCESS Level-1 26
comes next. This sort mechanism can also be applied on more than one or two columns,
fast and effective.

2.7 Changing a Table Design

You can add new field to the table or remove fields from the table very easily.
First of all, make sure that the table you want to add or remove fields to or from is closed.
Then on the main database interface, click on the Tables from the Objects menu. This
will display all the table names in the pane.

Select the table you want to change the design for and then click on the design button

. This opens the table in the design view. Now you can add fields, remove
fields, change the field types and modify or remove the primary keys as explained in
section 2.3.

2.8 Changing Data in a Table

If you want to modify the data in a table, then open the table in the Datasheet view by
double clicking on the table name.

The table will open displaying all its data. From here, you can modify any data value.
You can delete a record. You can also add a new record.

MS ACCESS Level-1 27
3.0 Introduction to Queries
Queries are the most powerful tool that MSACESS presents. The information analyst
ability to analyze data using the queries is limitless. Queries extend the functions
explained before for sorting and filtering tables to multiple combination of tables linked
with relationships. This gives an extra powerful dimension to analyzing data using the
MSACCESS system.

When a query is executed the results are displayed in Datasheet view. The format of each
field that appears in a query datasheet depends on the properties set in the base table or
tables. Query results are similar to the results of a filter, but a query is a database object
that you can save permanently, whereas a filter provides only a temporary view. Queries
also can be applied to multiple tables that can be linked together while building the query.

In this section, you will discover for yourself this extra edge that the queries offer you as
an information analyst. The goal of this section is to equip you with solid understanding
of MSACCESS queries so that you can apply and use them in your information analysis
needs.

3.1 Different types of Queries

MSACCESS offers different types of queries for the data analyst. These queries are:

1. Select query
2. Append query
3. Update query
4. Delete query
5. Make table query
6. Cross tab query

In this course, we will be thoroughly discussing the first four types of queries. While we
will be discussing number 5 and 6 in MSACCESS Level 2 course.

3.2 Select Query

The Select Query is very simple to construct. What this query does as its name conveys,
it selects certain data values to display from a table or a group of tables based on certain
criteria. Creating the criteria is so simple and flexible. This gives the analyst a powerful
tool to mine the data and analyze it accurately and smartly.

To construct a query, on the database main window, click on Queries from the Objects
menu.

There are two options to create queries. Option number 1 is to create query using the
Design view and Option number 2 is to create query using the wizard as can be seen
from Figure 3-1 below.

MS ACCESS Level-1 28
Figure 3-1

Using the wizard is the simplest way to create a query as all will be automated and all
you need to do is pick up what you exactly want to do and the wizard will create the
query for you.

Creating the query using the Design view is the way most professionals prefer. It gives
you, the data analyst, great flexibility and control to achieve the desired outcome.

3.2.1 Create query by using wizard

To create query by using the wizard click on the Create query by using wizard option
from the query window. The Simple Query Wizard will open as shown in Figure 3-2
below.

MS ACCESS Level-1 29
Figure 3-2

The query will be based on a table, so from the Tables/Queries pull down menu select the
table you want to base your query on. This is the table that the query you are constructing
now will read the data from and extract only the data that matches your defined criteria.
The moment you select the table you want from the pull down menu all the fields of that
table will be shown in the Available Fields box as shown above.

From the fields, click on a field that you want to display in the query results and click on
the button. If you click on the button then all the fields will be selected. All the
selected fields will show in the Selected Fields box on the right hand side.

For illustration purposes, we have until now constructed a single table Employee Info.
That is currently the only table we can choose and all its fields show in the Available
Fields box on the left side.

Now let us select the First Name, Grade and Salary fields by clicking on each one of
them and then clicking the button. The result will be similar to the one showing in
Figure 3-3.

MS ACCESS Level-1 30
Figure 3-3

If you want to un-select fields, then click on the field you want to unselect and then press
on the return button . Clicking on the return all button , unselects all the fields
in one shot and returns all of them to the Available Fields box.

Click the next button when you finish. Figure 3-4 shows the screen that will display.

MS ACCESS Level-1 31
Figure 3-4

The interface has two options. Option number 1 Detail (shows every field of every
record) is the simplest. Keeping this option and clicking on the Next button displays all
the records for the 3 fields from the table that you chose. After clicking on the next button
the following figure, Figure 3-5 shows the result.

MS ACCESS Level-1 32
Figure 3-5

You can give the query a name that you desire. MSACESS suggests a name for the
query: Employee Info Query, you can change that to another suitable name. For our
purposes, let us call it Test1. After that, click on the Finish button.

Figure 3-6 below shows the result. What the query Test1 basically does, is that it goes to
the table and brings all the data from the 3 fields that we chose before, First Name,
Grade and Salary and then it displays them for you as shown below.

MS ACCESS Level-1 33
Figure 3-6

Click on the button to close the query. You will go back to the general database
interface. You will notice that under the Queries object the query Test1 is showing as
shown in Figure 3-7 below.

Figure 3-7
.
Double click on Test1 and the same results in Figure 3-6 will show. Congratulations, you
have created you first query. You designed this query to look at the Employee Info table
and bring back from it the First Name, Grade and Salary fields. If you add more records
to Employee Info table, Test1 will bring more data for you when you activate it.

MS ACCESS Level-1 34
If you click on Tables from the Objects menu, you can see the Employee Info table
there. Double click on Employee Info table, the table opens and all the data that was
stored in it will show. While if you go to the Queries and double click on Test1 only a
subset of that table shows. Keep in mind that a query is not a storage place for data, it is
merely a piece of code that looks at a table where data is really stored and it fetches back
the data that you tell it to fetch. Nice stuff.

Now let’s go back a little bit. Perform all the steps above in creating the query Test1 but
in this case choose only the Grade and the Salary fields for the query. Continue until
you reach the step showing in Figure 3-4.

Now this time choose Summary, then click on the button. The
Summary Options window appears as shown in Figure 3-8 below.

Figure 3-8

Check the box under Sum and then click on the OK Button. Click Next, name the query
Test2 and then click the Finish button.

The result should be the same as the one shown in Figure 3-9 below.

MS ACCESS Level-1 35
Figure 3-9

To understand what the results are, we need to go back to the Employee Info table which
contains the records below.

First Name Last Name Grade Salary Department Date Joined Compant Married EmployeeID
John Dilworth 1 1000 Sales 01-Jan-06 TRUE 1
Fraser King 1 1200 Engineering 01-Jan-07 TRUE 2
Javier Lazaro 2 800 Engineering 13-Jan-06 TRUE 3
Mohammed Awadh 2 900 Sales 14-Feb-06 FALSE 4
Said Hadhrami 2 700 Sales 01-Feb-06 FALSE 5
Antoni Hopkins 1 1100 Engineering 17-Jan-06 TRUE 6
Abbas Al Noori 2 750 Aministration 20-Jan-06 FALSE 7
Muna Al Farsi 3 500 Aministration 01-Jan-06 FALSE 8
Ayshah Hassan 1 1000 Engineering 01-Feb-06 TRUE 9
Eman Al Rabadi 1 1050 HR 15-Feb-06 FALSE 10
Mohammed Hadhrami 2 760 HR 15-Feb-06 FALSE 11

In creating query Test2, you have selected two fields the Grade and Salary. What the
query did is that it summarized each Grade, since we chose to sum the Salary field what
the query did is that it looked at Grade 1 and then summed all the salaries under that
grade and then it did the same for Grade 2 and 3. For Grade 1 for example, the
summation for salaries is 1000 + 1200 + 1100 + 1000 + 1050 = 5350 which matches
what the query has reported above as shown in Figure 3-9.

Though MS ACCESS queries are very powerful tools to analyze data, the wizard does
not give the designer the ability to utilize queries to the limit. For this reason, the
discussion on the Wizard will not be explored further and we turn our attention now to
creating the queries using the design view.

This is the expert method of creating queries using the MS ACCESS.

3.2.2 Create query in design view

Let us now create a query using the Design view. Now you are starting your first step
into the depth of the MS ACCESS system. Once you completely master using the queries

MS ACCESS Level-1 36
in MS ACCESS, you will be able to analyze any set of Data quickly and accurately
whether it is small or gigantic, whether you have 100 records or 1,000,000 records.

Ok … Go to the Queries object, double click on Create query in Design view, the Show
Table window will display as shown in Figure 3-10 below.

Figure 3-10

The window has three tabs as shown above, the Tables tab, the Queries tab and the Both
tab.

When the Tables tab is selected then all the table names that were created within this
database will display in the box. If the Queries tab is selected then all the queries that
were created within this database will display in the box, while if the Both tab is selected
then both the tables and the queries that have been created within this database will
display in the box.

What is the importance of this window? … from this window you can select the table or
tables, query or queries or a combination of tables and queries that you want to build the
query you are currently designing on.

More about having more than one table in the query later, but now let us concentrate on
creating our first query using the expert Design view.

So now choose the Employee Info table and click on the Add button, what this does is
that it adds the Employee Info table to the design interface for the query.

Click on the close button to close the Show Table window. Figure 3-11 below shows how
the interface should look like:

MS ACCESS Level-1 37
Figure 3-11

You can see that the Employee Info table is now part of the fabric of the query which
until this point of time, is referred to as Query 1 and its type is a Select query as can be
seen from the tag name showing on the top of the query body.

Now we need to tell the query which fields to get back from the table, for our training
purposes, let’s assume we want the query to bring back the Grade, Salary and the
Martial status. To do that with the left mouse button either drag and drop the Grade field
from the table to the location marked above by the red oval or just double click on the
Grade field in the table.

Go to the Salary field in the table and drag and drop it to the second location to the right
of the red oval above or just double click on it.

Drag the Married field to the third location or just double click on it and MS ACCESS
will add it to the third location automatically.

The screen should look like Figure 3-12 below.

MS ACCESS Level-1 38
Figure 3-12

What the above interface says is that this is a select query that is brining its data from a
Employee Info table and it will only display three fields from the table which are the
Grade field and Salary field and the Married field.

All we need to do now is activate the query to see the results. To do that click on the
execute button and … there it is … your first select query designed and brining you
the data you required … congratulation !

Figure 3-13 below shows how the results should look like.

MS ACCESS Level-1 39
Figure 3-13

To save the query, so that it can be used in the future click on the Save button . In the
window that pops up, give this query a name say Test3 and then click on the Ok button.

If you close the query by clicking on the close button , you will see that it has been
saved under queries and its name is displayed with the other queries as can be seen below
in Figure 3-14.

Figure 3-14

MS ACCESS Level-1 40
3.3 Opening Query in Design View

To open a previously created query in Design view, go to the Queries object, click on the
query you want to open in the Design view. This highlights the query name. Now click

on the Design button , this opens the query in the Design view. You can also
right mouse click on the query name and then click on the Design button from the list
that pops up.

The interface that opens is similar to the one showing below in Figure 3-15.

Figure 3-15

The figure above shows query Test1 that you have designed before opened in the Design
view. From here you can add more fields to the query, remove fields or change the order
of the fields.

To add fields, simply drag a field from the Employee Info table to an empty column
below. You can also double click on the field name in the table and field name will be
inserted automatically to the query.

To delete a field then highlight the column carrying that field. To highlight the column,
move the mouse cursor just above the field name as shown in Figure 3-16 below. The
mouse cursor will change into a down pointing black arrow. Click now to highlight the
whole column as shown in Figure 3-17 below.

MS ACCESS Level-1 41
Figure 3-16

Figure 3-17

To delete the column, just press the delete key and the column vanishes. The query
design should now look as Figure 3-18.

MS ACCESS Level-1 42
Figure 3-18

Let’s add the Grade field again to the query. By now you should be able to do that?
How? Yes that is correct, double click on the Grade field in the table or drag and drop it
into an empty column in the query body.

The query design should look as shown in Figure 3-19 below.

Figure 3-19

MS ACCESS Level-1 43
Try to run the query? How? Yes that is correct, press on the execute button . The
results should be similar to Figure 3-20 below.

Figure 3-20

We call this view for the query the Data Sheet view. If you like you can change the
column locations in the Data Sheet view. Suppose you want to move the Grade field to
be in the middle then do the following: Click on the column header to select the whole
column as shown in Figure 3-21 below.

Figure 3-21

MS ACCESS Level-1 44
Now move the mouse cursor just below the column header, the mouse pointer changes as
shown in Figure 3-22 below.

Figure 3-22

Now drag and drop the Grade column to be between the First Name and Salary
columns. The result should be as below in Figure 3-23.

Figure 3-23

Click on the save button and save the query in its current design.

MS ACCESS Level-1 45
3.4 Queries and Criteria

We turn now to the filters that you can design into the queries that give the queries their
flexibility in digging in specific data from the tables.

To illustrate that open query Test1 in design view. By now you should be able to do that.
The display should look like figure 3-24 below.

Figure 3-24

Now suppose you want to see only the people who are in Grade 1, then you need to type
in 1 in the Criteria field under Grade (In the place surrounded by the red oval in the
figure above. Figure 3-25 shows that).

MS ACCESS Level-1 46
Figure 3-25

Something to notice here, is that the quotes around the 1 have been insterted
automatically by MS ACCESS. The reason for that is that the Grade field is a Text field
and not a numeric field. So MSACCESS is treating the 1 as text and not as a number.

Question: How can you make sure that the Grade field is a Text and not numeric field?
Answer: Go the Tables in the Objects menu, select the Employee Info table and then
click on the design button. This should open the table in the design view as explained
before and the display should look like Figure 3-26.

MS ACCESS Level-1 47
Figure 3-26

As you can see the Grade field is a Text field.


Now close the table and go back to the design view of the Test1 query. Activate the
query by clicking the execute button.
You will notice that the query only displays the records that are of Grade 1 as shown in
Figure 3-27 below.

Figure 3-27

Suppose you want to see all the employees that has the letter e in their first name and are
of Grade 2. To do that go back to the design view of the query. Click on the query then
from the top tool bar click on the design view button . Then change the query design
to look like Figure 3-28 below.

MS ACCESS Level-1 48
Figure 3-28

Notice that in the Criteria for First Name we choose the argument Like “*e*” which
means anything that has an e. (By the way all you need to type in the Criteria *e*,
MSACCESS will convert it into Like “*e*” automatically). In the Criteria for the Grade
we typed 2. Now execute the query. The result should be as shown in Figure 3-29 below.

Figure 3-29

Splendid .. the query returned 3 records that match the criteria. Did you notice that this
resembles applying the Filter on the Employee Info table, that we discussed when we
were learning about the tables. However, there are differences. The first is that the query
can be saved, so you do not need to apply the filter every time you want to check this
particular set of data (i.e employees having an e in their first name and are in grade 2).
The second major difference is while using the table you had to apply the first filter and
then the second one, however now both filters are applied at once and the results are
displayed at one shot…. This is really cool.

MS ACCESS Level-1 49
Save your query and then close it.
Go to the Employees Info table and add a new employee. This new fellow name is Emad
and he is appointed in grade 2. I will leave the salary and the other information for you to
decide on. After you do that, go and run query Test1 again. You will see that Emad is
showing in the query. Have a look at Figure 3-30 below.

Figure 3-30

And this is the beauty of the queries. While the tables are containers of data into which
new data pours. This data storage into the tables and depending on the application used
can happen hourly, daily or weekly. Now the query is a design to extract certain
information from the table. The query works with the data that the table has today and
will work with the data the table will have tomorrow. The rules carried in the query apply
to the table irrespective of the amount of data it is carrying.

Exercise: You as the data analyst need to prepare a report for your manager. The report
should contain the information for all the employees who are in the Engineering and
joined the company before 2007. How can you do that? Simple.

Let’s design a new query, we will call it Test4. The design view of this query should look
like Figure 3-31 below.

Figure 3-31

MS ACCESS Level-1 50
Notice how the date criteria is applied. We use the less than < operator and then the data
is surrounded by the number # symbol. By the way all you need to write in the criteria for
the Date Joined Company field is <1-jan-2007 and MSACCESS, since it recognizes this
field to be a date will change it into the format above.

Save this query and give it the name Test4. To do that click on the Save button, fill in the
name and click on the Ok button.

Now execute the query, the results should be like Figure 3-32 below.

Figure 3-32

Since the data is filtered out quickly, accurately, nice and easy, you can create a report
and present it to your manager. A report can be constructed using MSACCESS, we will
discuss this later on. You can also copy and paste this data on Excel or word document
and create the desired report.

Now you might be thinking that we do not need MSACCESS to do that and finding the
information above is an easy task and can be done simply by looking at the table. Now
that is true because we have only 11 records in the Employee Info table but imagine that
the company you work for is a multinational with 100,000 employees and 20 different
departments. To complicate things further imagine that your manager wants the report in
15 minutes. Without MSACCESS or another database program, you have no hope.
However, the same query that we have designed here, can read the same Employee Info
table that has a 100,000 records and get you the results easily, accurately and quickly…
Bingo … I guess now you realize the power of the database.

3.5 Sorting in Queries

You can control the order of records when the query presents them. In our previous
example, Query Test4, suppose you want the records sorted in order of Salary so the
lowest salary should show as the first record, the second lowest should show as the
second record and so on. To do that go back to the design view of the query. I am sure
you know how to do this by now. Click on the query and then click on the Design view
button.

In the Sort selection under the Salary field, choose Ascending. Figure 3-33 below shows
that.

MS ACCESS Level-1 51
Figure 3-33

Now execute the query, the results should show as Figure 3-34 below.

Figure 3-34.

As you can see, the Salaries are sorted in Ascending order. If you selected the Sort order
to be in Descending order then the records would reverse.
Save and close the query.

Exercise: Suppose your manager is running to a meeting in 5 minutes and he needs a


report showing all the people in the company sorted by the Grade. To whom would he
turn? Of course to you, you are the company data man. How would the query design look
like?

Simple, it should look like Figure 3-35.

MS ACCESS Level-1 52
Figure 3-35

Notice that we added the Ascending criteria in the Sort for the Grade. The results should
look like Figure 3-36.

Figure 3-36

That is a great report sorted by the Grade. When your manager sees it, he says: Oh .. can
you please make it sorted by both the Grade and Salary ? You say alright no problem.
How should the design look like now? Simple.. it should look like Figure 3-37 below.

MS ACCESS Level-1 53
Figure 3-37

Note that we have added Ascending for both Grade and Salary and the results should
look like Figure 3-38 below.

Figure 3-39

This looks neat. The records are sorted by Grade and then by Salary. You show this to
your boss and he say good job. He thinks a little and then he says: Can you please prepare
another report showing grade 3 then 2 then 1 and the Salaries for each grade in ascending
order. You say no problem … The design now should look like Figure 3-40 below.

MS ACCESS Level-1 54
Figure 3-40

Notice that the sort criteria for Grade is Descending while for Salary it is Ascending.
Simple changes in the design, but dramatic changes in the results. Look at Figure 3-41
which shows the results for this query.

Figure 3-41

You think to yourself, when I go back to the manager with this report, I guess he is going
to ask me to prepare another one showing the employees sorted out by their salaries then
by their grades. In order to do that you need to put the Salary field before the Grade
field and then apply the sort.

In order to put the Salary field before the Grade field do the following. Go back to the
design view of the Query and then highlight the Salary Field. You can highlight it by

MS ACCESS Level-1 55
moving the mouse cursor until it is at the top of the column of the Salary field in the
query body. The mouse cursor changes into a black arrow pointing downwards. Figure 3-
42 shows that.

Figure 3-42

Now click on the to highlight and select the Salary Column. Figure 3-43 shows the
display:

Figure 3-43

Move the mouse pointer just above the field name. The mouse pointer changes as shown
in Figure 3-44.

MS ACCESS Level-1 56
Figure 3-44

Drag the Salary column until it is before the Grade column. The design view should look
like Figure 3-45.

Figure 3-45

Make the criteria for the sort for both the Salary and Grade field ascending and run the
query. Results should be like Figure 3-46.

MS ACCESS Level-1 57
Figure 3-46

Now this might not be fully apparent since the grades go hand in hand with the salaries so
let’s introduce a change that would demonstrate the idea. Save the query as Test5 and
close it.
Open the Employee Info table and change the grade for Javier from 2 to 1 and close the
table. Run query Test5 again. Results should like Figure 3-47 below.

Figure 3-47

As you can see both Emad and Javier have the same salary, but Javier is showing before
Emad because he has grade 1 while Emad has grade 2 and we are sorting by Grade too.

MS ACCESS Level-1 58
In order to sort by multiple fields you do not need to have the fields adjacent, they can be
far apart.

The point you need to keep in mind is that MSACCESS applies the sort starting from the
left hand side going to the right hand side. So in this direction any field that comes first
will be sorted first and then by the next one and so on.

3.6 Append Query

The same principles applied to design a Select query are still valid for the Append query.
The Append query has the extra functionality of adding the selected records to another
table.

Let’s assume that the company you work for has acquired another company. Now you
need to add the new employees information to the Employee Info table. You start
thinking, that this would be a tedious task to start adding them one by one. Instead, you
come up with another idea, you decide to ask the person responsible for keeping
employee records from the newly acquired company if he has any sort of a file were he
keeps the employee data.

Luckily, he answers positively. So the two of you examine the structure of the file he uses
to keep the employee’s records.

The table that contains the new employees’ records contains the following fields.

Given Name
Family Name
Grade
Compensation
Section
Date Joined
Married

After some discussion, you determine that those fields map to the corresponding fields in
your original table as follows:

Given Name  First Name


Family Name  Last Name
Grade  Grade
Compensation  Salary
Section  Department
Date Joined  Date Joined Company
Married  Married

MS ACCESS Level-1 59
After having determined how the fields map to each other, we are all set to create the
Append query that will copy this data from the table that is used to keep the records of
the employees in the new company to our Employee Info table.

It is necessary to point out that there are means to bring this table into our database, we
can import, link or even copy this data into our database. MSACCESS has the tools that
enable the analyst to bring this data inside the database. However, discussing these tools
is beyond the scope of this course. MSACCESS Level 2 course discusses these topics in
full details. For our purposes, we consider the data is already imported to the current
database into a table called Employee info2.

Ok .. let’s carry on with our example, now you think to yourself for some time and say, in
addition to adding the records to the table I want to be able to distinguish between the
employees who were originally in the table and the ones that are about to be added. How
do we do that? After some thought, you decide to add a field to the Employee Info table
called OLD_NEW. If this field value is OLD it means the old company and if it contains
NEW then it means the new Company.

How do we add this field? Yes, open the table in the Design view and then add this new
field. Call it OLD_NEW. Look at Figure 3-48 to remind you.

Figure 3-48

Note the added field to the table above. Save the table with this new design. If you open
the table in Data sheet view, this new Field will be empty and will have no records.

We come to the point to create the Append query. Your thought process should be as
follows, we need to prepare a Select query that selects the records we want to append
(add) to our Employee Info table. Where do these records exist? Yes, that is correct.
They are in Employee Info2 table. So we need to design a select query that reads all the
records from the Employee Info2 table.

The design for this query should be as shown in Figure 3-49 below.

MS ACCESS Level-1 60
Figure 3-49

What this query does is that it brings back all the records in the Employee Info2 table.
We now need to append (which means add) those records to Employee Info table. To do
that, we need to change this query into an Append query. From the drop down menu of
the query type, choose Append Query as shown in Figure 3-50 below.

Figure 3-50

Once you choose Append query, the system will ask you for the table you want to
Append the records to. This is shown in Figure 3-51 below.

Figure 3-51

From the pull down menu, choose Employee Info or you can type the table name in and
then click on the OK button.

The moment you do that, you will notice that the query design view has changed as seen
Figure 3-51B below.

MS ACCESS Level-1 61
Figure 3-51B

You can see that now we have an Append To line in the query design (Red Arrow
above). If you look at the Grade and Married fields, you see that MSACCESS already
filled in the Append To line for these two. The reason is that the Employee info and the
Employee info2 tables both have the exact naming for these two fields. So MSACCESS
figures out that these fields should be mapped to each other. Please keep in mind that this
might be correct or it might not and you as the Information Analyst is the judge.

The Append To line under each field, has a drop down menu which contains the field
names of the Employee Info table (The appended to table) and you can choose the
matching fields (i.e the corresponding field to map to). Do that and the design view
should look like Figure 3-52 below.

Figure 3-52

Ok .. until now all sounds good. Before we execute this query, let’s make use of the field
that we added to the Employee Info table. Since all the records that will be appended are

MS ACCESS Level-1 62
from the new company, we want to add the value NEW in the OLD_NEW field. We can
do that by adding to the design as shown in Figure 3-53 below.

Figure 3-53

Note the addition in the red oval above. We wrote “New” and appended this word to the
OLD_NEW field. Note that Expr1: is added automatically by MSACCESS to distinguish
this from a field that is being read from the Employee Info2 table.

Ok, all set. Run the query by clicking on the execute button.

Once you click the button, nothing seems to have happened. Do not press the execute
button again. If you do, this will result in the same records to be appended again to the
table. This is of course unnecessary.

Save the query, call it Test6 and close it. You will see in the general interface of the
database that query Test6 has been added to the other queries and that it has a different
symbol to distinguish it as an Append query rather than as a Select query like all the
other queries. Figure 3-54 shows that.

MS ACCESS Level-1 63
Figure 3-54

To make sure that the query has worked. Open the Employee Info table, the results
should be as shown in Figure 3-55 below.

Figure 3-55

MS ACCESS Level-1 64
The query has worked, we have now 23 records in the table. Notice how the OLD_NEW
field displays NEW for all the new records that we just appended.

To summarize how the Append query works, remember the following points:

(1) Append query takes data from one table or group of tables and adds it to another
table
(2) To design an Append query start by designing a Select query that reflects the
dataset you want to add to another table (all the rules for filters and sorting that
we discussed in the Select query section apply)
(3) After you design the Select query that returns back the records you want to add to
a specific table, run the Select query to make sure it has the data you need
(4) Now, prepare a MAP between the fields that the designed Select query returns
and the fields in the table you want to append this data to
(5) Change the Select query into an Append query
(6) Select the table you want to append the data to
(7) In the Append To line, choose the corresponding fields (the mapping fields).
(8) Execute the Query by hitting the execute button only once
(9) If we are talking about a small number of records to be added then the whole
append operation happens so quickly that you do not feel that anything has
happened
(10) If this is the situation do not feel tempted to hit the execute button again because
this results of the records being appended again to the target table
(11) However, if the number of records is substantial then you will see a small
potentiometer at the bottom end of the screen showing the progress of the
process.
(12) After the process is finished open the target table to make sure that the data has
been added to it

MS ACCESS Level-1 65
3.7 Update Query

The Update query is different from the Append query. While the Append query adds
new data to the table creating new records, the Update query changes the current data in
the table based on certain criteria. It does not add any new records.

Let’s illustrate the use of this query by an example. Please go back to Figure 3-55. You
can see that the OLD_NEW field is showing NEW for the new records, while the old
ones have no value. We want to change that by writing OLD instead of having these
empty values. To do that we need to tell MSACCESS to change all the empty value into
OLD.

The field we want to change is the OLD_NEW field. Let’s select it in the design of the
query. Start by designing a Select query that returns the OLD_NEW field from the
Employee Info table as shown below in Figure 3-56.

Figure 3-56

If you execute this query the result would be as shown in Figure 3-57.

MS ACCESS Level-1 66
Figure 3-57

You can see that the query returns back the records that has NEW as value and the
records that are empty for the OLD_NEW field. Keeping in mind that we only want to
change the records that are empty, we need to filter for the empty ones. The query design
should be as shown below in Figure 3-58.

MS ACCESS Level-1 67
Figure 3-58

Notice that the Criteria is asking MSACCESS to filter for the Null (empty records). Is
Null is the expression you use if you want to filter for a field that contains no data
(empty).

If this query is executed the result should be as shown in Figure 3-59.

Figure 3-59

This query only returns the empty records. What we need to do now is to change this into
an Update query and ask it to update these records into “OLD”.

MS ACCESS Level-1 68
To do that from the query type choose Update query. The design view for the query
changes as shown in Figure 3-60 below.

Figure 3-60

Notice the addition of the Update To line in the design view of the query (Red Arrow).
We now need to tell the query to update those to “OLD”. The design view should look
like Figure 3-61 below.

Figure 3-61

MS ACCESS Level-1 69
What the above query design says is: Update the OLD_NEW field in the Employee Info
table wherever it is empty and has no value into OLD. Run the query by clicking on the
execute button. You might get the feeling that nothing happened but actually the query
has performed just what we asked it to do. To verify that open the Employee Info table.
The results should be as shown in Figure 3-62 below.

Figure 3-62

You can see that all the empty values have been update to OLD while the NEW values
have been kept as they were. If we did not specify the is Null filter, all the records would
have been updated (changed) into OLD.

Save the query and name it Test7 then close it. Notice how it is added to the general
interface of the database with a different icon to distinguish its type from the Select and
Append query types.

To summarize how the Update query works, remember the following points:

(1) Update query locks onto specific data in a table and changes it into other data
(2) To design an Update query start by designing a Select query that selects the fields
you want to change (all the rules for filters and sorting that we discussed in the
Select query section apply)
(3) After you design the Select query that returns the fields you want to change, run
the Select query to make sure it has the data you want to modify
(4) Change the Select query into an Update query
(5) In the Update To field, specify what you want the fields changed into

MS ACCESS Level-1 70
(6) Execute the Query by hitting the execute button
(7) If we are talking about a small number of records to be changed then the whole
update operation happens so quickly that you do not feel that anything has
happened
(8) If this is the situation, do not think that nothing happened. Hitting the execute
Button again does not cause any harm like in the case of the Append query.
(9) However, if the number of records is substantial then you will see a small
potentiometer at the bottom end of the screen showing the progress of the process
(10) After the process is finished open the target table to make sure that the data has
been changed.

3.8 Delete Query

This type of query is used to delete whole records from a table. Please keep in mind that
you cannot use this query to delete a field or fields. This query is used to delete entire
records from the database.

To illustrate the use of this query, let us delete all the new records from the Employee
Info table. To do that start by designing a Select query that shows the records you want to
delete. Figure 3-63 shows how the query might look like in the design view.

Figure 3-63

Note that in the Criteria for the OLD_NEW field, we are filtering for the records with
“new” value. If you run this query it will show all the new records from the Employee
Info table.

From the query type select Delete Query. Figure 3-64 below shows how the design view
for the query looks like.

MS ACCESS Level-1 71
Figure 3-64

Execute the query. You might get the feeling that nothing happened but if you open the
Employee Info table you will see that all the new records have disappeared.

Do you know a short cut to bring them all back?


Yes, that is correct just double click on the append query we previously created and
saved, query Test6. Just double click on it.
You might get the feeling that nothing happened. But if you open the Employee Info
table again, you will see all the new records added again to the table.

To demonstrate the point that the delete query deletes full records design the following
query as shown in Figure 3-65 below.

Figure 3-65

MS ACCESS Level-1 72
Now execute this query. Open the Employee Info table. You will see that all the new
records have disappeared. It is not just the data in the OLD_NEW field that has been
deleted, It is all the records that contained “new” under OLD_NEW.

Now bring the new records back to the table. I am not telling you how ….

To summarize, remember the following points about designing delete queries:

(1) Design a Select query that lists the records you want to delete
(2) Convert the Select query into a Delete query
(3) Execute the query
(4) Keep in mind that the Delete query deletes full records and not single fields

3.9 Queries and Relationships

I have mentioned before that queries can read and manipulate data from multiple tables.
This is what we will be exploring in this section. MSACCESS ability to query data from
multiple tables enables the database analyst to perform powerful queries. It is also
because queries can be designed in this way, simpler databases can be created.

If there is a need to query more than one table, which is the case most of the time, then
we need to link these tables using relationships.

To illustrate further, we will demonstrate that using examples.

The HR manager in the company you work for, and in an initiative to motivate
employees, wants to gather information about the birthdates of the employees’ families.

After he gathered all this information, he comes back to you and asks for help to add this
information to the current employee database.

Figure 3-66 below shows the data HR has gathered.

Figure 3-66

The list goes on an on for all the married people in the company. Now you look at the
data and decide that the best way to store this is by having a separate table called Family

MS ACCESS Level-1 73
in which you will store the family member information along with a reference to the
employee. The best reference to use in order to identify an employee is the Employee ID
field.

The question is why would this be a better design rather than adding this information to
the Employee Info table? The reason is that if this data is to be added to the Employee
Info table then we will start creating redundancies as the Employee First Name, Last
Name, Grade, Salary, ID and all the other information would have to be repeated for each
and every child and spouse which will make the data grow unnecessarily bigger.

Since MSACCESS allows relationships to be created between tables, it enables us to


segregate chunks of data in separate tables. This allows an optimum database to be
designed which is much easier to understand and trace, it also allows the database to be
smaller in size which makes it faster and occupies less space on the hard drives. When we
start talking about databases that carry tens of thousands of records this becomes more
essential.

In the course, Advanced Database Design – MSACCESS Level 3, the above subjects are
discussed in more details.

Let’s go back to our example, design the Family table as shown below in Figure 3-67.

Figure 3-67

Save the table and give it the name Family, when asked for a Primary Key leave the
table without a Primary Key for the time being. Next enter the data as shown in Figure
3-68 below.

MS ACCESS Level-1 74
Figure 3-68

Close the table after completing entering the data.

Suppose your manager asks you to prepare a report that shows each employee name and
his family. A simple query like the ones we have created so far will not work because the
data is stored in two different tables. To do that, follow the following steps.

From the Objects menu select Queries and then double click on Create Query in Design
View. In the Show Table double click on the Employee Info table, this adds the
Employee Info table to the query design. In the Show Table double click on the Family
table, this results in the Family table to be added to the query design. Figure 3-69 below
shows how the query design view should look like.

MS ACCESS Level-1 75
Figure 3-69

As you can see, this query now is hooked to get data from two tables. But there should be
a relationship between these two tables that enables MSACCESS to bring the data in an
orderly manner. These two tables have one key that can link a record from one table to
the other, this key is the Employee ID. If we know the Employee ID, then in the
Employee Info table we can know all the other info, like his name, grade, salary etc. And
from the Family table we can know his spouse and children names.

To link the tables, you need to drag the link field from the first table and drop it on the
link field in the second table (or vice versa). Since the Employee ID field is not showing
in Figure 3-69 in the Employee Info table, scroll down until you find it and then click on
it, drag it and drop it on the Employee ID field in the Family table. This will create the
link. MSACCESS draws a line between the two fields from the two tables to demonstrate
the fact that they are linked. Figure 3-70 shows that.

MS ACCESS Level-1 76
Figure 3-70

Select the fields you want from the two tables. The design view for the query should look
like Figure 3-71 below.

Figure 3-71

Try now to execute the query. SURPRIIIIIIIIZE … it did not work. The ”Type
Mismatch in Expression” message is returned. The reason for that, is the fact that the
fields we chose to link the two tables are not of the same data type. If you open the
Employee Info table, the Employee ID is a numeric field while it is a Text field in the
Family table.

MS ACCESS Level-1 77
To correct that, leave the query as it is. Open the Family table in the Design view,
change the Employee ID field to be a numeric field and then save and close the table.

If now you go back to the query and run it, it will show the results as in Figure 3-72
below.

Figure 3-72

Notice that the query returned back the fields chosen from both tables. Another important
thing to notice, is that the query only returned the fields where we have corresponding
Employee ID in the two tables. If the Employee ID field is present only in one of the
tables, the record would not be returned because it does not have a matching one in the
other table.

You can add filters just as you did with any other query. Suppose you want only to see
the records for the Spouses and not the children, then you need to filter in the
Spouse_Child field for Spouse. The design view of the query should look like figure 3-
73 below.

MS ACCESS Level-1 78
Figure 3-73
Now execute the query. Results should be as shown in Figure 3-74 below.

Figure 3-74

Suppose now you want to see the Spouse that has a double a in his/her name then the
design view of the query should look like figure 3-75 below.

MS ACCESS Level-1 79
Figure 3-75

Execute the query and you would see that Katherine disappears from the results because
all Martha, Angelina and Jalal has double a in their names.

Suppose your manager asks you to prepare a report where you show the names for all the
employees in the company, however, if that employee has a spouse and or children, then
we need the spouse and children name to be displayed too.

To do that go back to the query and click on the line linking the two tables until it
becomes thick as shown in Figure 3-76 below.

MS ACCESS Level-1 80
Figure 3-76

Right mouse click on this thick line and select Join Properties from the list that pops up.
The window shown in Figure 3-77 will appear:

Figure 3-77

This window shows the two fields that are linking the two tables and the kind of
relationship between them. The current relationship is defined by option number 1 which
only shows the records where the linking fields are present in both tables.

For this new report, we need all the records from the Employee Info to be displayed and
only those from the Family table when the linking fields are equal. For that purpose set
the relationship to Option 2 and click on the OK button.

Notice how the line linking the two tables has changed, as shown in Figure 3-78 below.

MS ACCESS Level-1 81
Figure 3-78

The results of the query should be as shown in Figure 3-79 below.

Figure 3-79

MS ACCESS Level-1 82
3.10 Group by and aggregate functions

We turn now into a very interesting topic that adds one more tool in your hand when
designing queries; the aggregate functions.

To demonstrate the capabilities of the aggregate functions we will take some examples.

Suppose your manager comes to you and says he wants to now the number of people in
each grade.

To do that you can open the table, sort the table by grade in Ascending order and then
check how many records you have in each grade. It might be quick here, but if you have a
company with lots of grades and hundreds of employees, it will take a very long time to
do that.

The aggregate functions are a quick and easy way to do that. You can do that by
designing a query as below in Figure 3-80.

Figure 3-80

Execute this query, the result should be similar to the one in Figure 3-81 below.

MS ACCESS Level-1 83
Figure 3-81

If only we can instruct MSACCESS to group the grades so we have 1,2,3 & 4 and instead
of having all the corresponding records on the left for the Employee ID, if we can ask
MSACCESS to count those records while grouping the grades … well .. there is a way to
do that and we will explain it below.

Go back to the design view of the query and then click on the Group By button .
Notice the change that occurs for the query design view as shown in figure 3-82 below.

MS ACCESS Level-1 84
Figure 3-82

Did you notice the new line introduced? Yes, it is the Total line indicated by the red
arrow above.

What we want to do is group by Grade and count the Employee ID. So leave the total
line at Group By under Grade and then change it to Count (choose Count from the pull
down menu) under Employee ID. The query design should like Figure 3-83 below.

Figure 3-83

MS ACCESS Level-1 85
Execute the query and look at the results. They should look like figure 3-84 below.

Figure 3-84

What the results say is that we have 9 employees in grade 1, 8 in grade 2, 4 in grade 3
and 2 in grade 4. This query could run fast and produce accurate results if we had
thousands of records in the table.

Suppose we want to know the Sum of salaries of the employees in each grade. This also
is simple. The design of this query should be as shown in Figure 3-85 below.

Figure 3-85

As you can see we have added the Salary field to the query body and we have chosen to
Sum it out. This query groups the records by the grade, counts the employee IDs for each
group and sums the salaries. The result when executing the query should look like Figure
3-86 below.

MS ACCESS Level-1 86
Figure 3-86

For example, you can quickly see that the sum of all the salaries of the 4 employees in
grade 4 is 3,180$. Let’s make it more interesting, say you want to see the Minimum and
Maximum salary for each grade. The design of the query should look like Figure 3-87
below.

Figure 3-87

The Salary field is introduced three times above. In one occurrence, it is summed. When
you group by the grade, this means that all the records that are having the same Grade
should show once, since we have more than one Salary for a single Grade we need to tell
MSACCESS what to do with them. In the first time, we are telling it to Sum them. In the
second time we tell it to bring the Minimum value from them and in the third we tell it to
bring the Maximum value. Execute the query, the results should be the same as Figure 3-
88 below.

MS ACCESS Level-1 87
Figure 3-88

We can quickly see that the minimum Salary in grade 1 and 3 are 500, which might
indicate that we have an issue in the Salary structure. Also you can see that the
maximum of grade 3 is less than grade 1 another indication of an issue.

One question, if you want to do the above mathematical calculations for the 23 records in
Employee Info table? How much time would you need? Try it and find out.

In MSACCESS we need less than a minute to get the same information. Now imagine if
the table contained a 1,000 records. You can still get the needed info in less than a minute
using MSACCESS. Very powerful.

Let’s take another example. Suppose you needed to prepare a report that shows the name
of employee and the number of children he has.

The design view for such a query should look like the one showing in Figure 3-89.

Figure 3-89

MS ACCESS Level-1 88
Let’s analyze how the above query is structured. First of all, we know that the data we are
seeking is available in two tables; Employee Info and Family, so both of them has to be
present in the design. Second, since we added two tables, we need to link them, so we
link them using the field that can identify records on both tables which in this case is the
Employee ID field.

We now select the fields we need, First Name and Last Name from the Employee Info
table. This is the employee name. And we select Spouse_Child and Name from the
Family Table. Notice that in the Spouse_Child field we are filtering for the Child
because we do not want to include the Spouses in our count. We group by all the fields
mentioned above, so we would have one record for the First Name, Family Name and
Spouse_Child which represent the children for a single employee and then we count the
child name to get the number of children.

The result of running this query should be as below in Figure 3-90.

Figure 3-90

If you need to hide any field from the final query result, you can do that using the Show
line in the query Design view. All you need to do is uncheck the field that you do not
want to show. Look at the design of the query below in Figure 3-91.

MS ACCESS Level-1 89
Figure 3-91

Notice how the Spouse_Child field Show line is unchecked (red arrow above). When
you run this query the, the results would be as shown in Figure 3-92 below.

Figure 3-92

The results are exactly as the query before, but the Spouse_Child field is hidden and is
not displayed. Keep in mind that we still can filter by this field and if we are grouping
using this field, then it will still affect the results. The only difference is that it does not
show in the final result.

MS ACCESS Level-1 90
4.0 Introduction to Forms
Forms are MSACCESS objects that enable you to prepare professional applications.
Forms can act as your windows and doorways to tables. Professional databases are
designed in such a way that nice looking forms act as doorways through which data entry
personnel enter data to the tables. Data entry people are not allowed direct access to the
tables because they might accidentally delete data or change the table design.

Giving them access to Forms linked to parts of a table, are a much better solution. Forms
also can control what can be seen. A form can display specific fields from a table, while
another Form can display another set of fields from the same table. By giving different
accesses to the two forms, control can be established to who sees what.

Forms can act as data entry points to tables and they can also act as information display
windows which display data from a table or a query based on a table or multiple tables.

From the Objects menu, in the general data interface, click on Forms. Figure 4-1 shows
that.

Figure 4-1

As you can see, there are two ways to create forms exactly as there are two ways to create
queries. You can either use the Wizard or the Design View.

4.1 Creating Forms using the Wizard

To create a Form using the wizard, double click on the Create form by using wizard in
the general database interface. Figure 4-2 shows the window that appears.

MS ACCESS Level-1 91
Figure 4-2

This interface is similar to the one we previously used while creating a query using the
wizard. From the Tables/Queries pull down menu select the table or the query you want
to base the Form on.

The fields from that table or query will be displayed in the Available Fields box. You
can select the fields that you want to be displayed in the form. For the purpose of this
exercise, let’s choose all of the fields from the Employee Info table. Click on to
select all the fields. Notice that all the fields move to the Selected Fields box. Figure 4-3
shows the result.

MS ACCESS Level-1 92
Figure 4-3

Click the Next button. The Form wizard asks about the layout of the report as shown in
Figure 4-4 below.

Figure 4-4

MS ACCESS Level-1 93
Clicking on each of the options above results in the layout to change to show you how the
final Form will look like. Click on each option, to see how the layout looks like. Do not
worry if you do not understand how the final report will look like at this point of time,
you will soon be designing Forms in all layouts and shapes.

For our exercise purposes, choose the Columnar layout. Click on the Next button. The
form wizard now asks you to pick up the style. Figure 4-5 shows that.

Figure 4-5

Click on each one of those styles. They are high touch looking and will give your
application a nice professional look. I will choose the Ricepaper, you can choose which
ever one that you like better. It will not affect the illustration of this example.

Click the Next button once you choose the style you like. Figure 4-6 below shows the
window that will appear.

MS ACCESS Level-1 94
Figure 4-6

Give the Form a name. Call it TestForm1 and then click on the Finish button. The form
will open and the first record of the Employee Info table will be displayed. Figure 4-7
shows the result.

MS ACCESS Level-1 95
Figure 4-7

This Form is a door way to the table, you can perform all the functions you could do on
the table. It is exactly as if you have opened the table, but the interface is much nicer, you
can see each record at a time. You can also do the following things:
1- Go to the next record
2- Go to the last record
3- Go to the previous record
4- Go to the first record
5- Add a new record
6- Select a record using the record selector
7- Delete a record, by clicking on the record selector to select it and then hitting the
Delete key
8- Change the data in any of the fields above

4.2 Editing the data in the table using the Form

Scroll through the records until you reach the 5th record for Sandra Bolok. Now change
the Salary to 650. Figure 4-8 shows that.

MS ACCESS Level-1 96
Figure 4-8

Notice that the moment you start changing any field, the record selector switches to the
data entry mode and the pen sign is shown. Look above in the red circle. The data is
never saved until you move to another record, so you need to click on the move to next
record or previous record for example to get the change stored. This is exactly what we
previously saw when we discussed the tables.

If you open the Employee Info table you will see that the data has been changed on the
table too.

4.3 Adding a new Record to the Table using the Form

To add a new record, click on the add New Record button , an empty form shows as
seen in Figure 4-9.

MS ACCESS Level-1 97
Figure 4-9

Note that the total number of records now is 24. Enter the data that you want. Notice the
record selector changes to the data entry mode while you are entering the data. Again the
data is not saved until you move to another record. I have added the data as Figure 4-10
below shows.

Figure 4-10

Notice the data is still not saved, we can tell that from the pen sign on the record selector.
By clicking on the Next Record button, the record gets stored and the total number of

MS ACCESS Level-1 98
records jumps from 24 to 25. Figure 4-11 below shows the result after clicking on the
Next record button.

Figure 4-11

Since the record that we just entered was the last in the table, hitting the Go to next
record stores the previous one and moves to the end of the table again showing an empty
record where we can fill new data.

Go back to the previous record, you will find the record we have just entered. It is now
part of the table and the total number of records shows 24. If you open the table, you will
see the new record that we have just entered in the table.

4.4 Other Form Layouts

If you remember, in Figure 4-4 we had more than one choice for the Form layout.
Design a new Form based on Employee Info Table. This time choose the Tabular layout
for the Form and save it under TestForm2.

If you choose the same RicePaper style, the Form will look something like Figure 4-12
below.

MS ACCESS Level-1 99
Figure 4-12

The Tabular layout is similar to a table. You can see all the records instead of a single
one. You can update the data. You can enter new records. You can delete records. And
you also can Filter like you did in the table.

Now create a new Form. Call it TestForm3 and this time choose Datasheet for the
layout. The form should look like Figure 4-13.

MS ACCESS Level-1 100


Figure 4-13

The form looks exactly like a table. All the things you could do from filtering, adding,
deleting and modifying data you can do here.

Create a new form. Call it TestForm4. Base it on the Justified layout. If you choose the
RicePaper style, the form should like Figure 4-14 below.

Figure 4-14

As you can see, the justified layout resembles the Columnar layout. However, the
arrangement of the fields is different. Here, you can add new records, you can delete a

MS ACCESS Level-1 101


record by selecting it using the record selector and clicking on the Delete button, you can
modify the data and you can also filter. All the things we could do on the table we can do
on the forms linked to those tables.

Notice that the form has visibility to all the table records and this is why we can see that
the total number of records are 24. Now suppose you want to filter for the employees that
their first name starts with an a. Then in the First Name field on the form right mouse
click, a list will appear. In the Filter For, type in a* and press enter. Figure 4-15 below
shows the result.

Figure 4-15

Notice that the number of records are now only 4 and notice that beside the number 4
(Filtered) is displayed to indicate that the form is now only showing filtered data and has
no visibility to the full table.

Click on the Next Record button, the result is shown in Figure 4-16 below.

Figure 4-16
Notice now that record 2 is also an employee that starts with an A. The same thing
applies for records 3 & 4.

To remove the filter from the form, in any place inside the form right mouse click. A
menu will pop up, choose Remove Filter/Sort and the form goes back to its original state
showing all the 24 records.

You can also sort by a specific field. Suppose you want to sort the employees according
to their salaries in descending order. Do the following. In the Salary field, right mouse
click, from the menu that pops up choose Sort Descending.

MS ACCESS Level-1 102


As you go through the records one after the other, you will see that with each record you
step to, the salaries are actually decreasing until you reach the smallest salary on the 24 th
record.

4.5 Creating Forms in Design View

MS ACCESS professional users prefer to use the Design View for designing forms.
Design View gives more flexibility and has more tools to create professional looking
forms.

Some of MSACCESS users use the wizard to create the form and then they open it in the
Design View to create the changes they need.

How can we design a From to display the records in the Employee Info table?

The following example will take us through that.

In the general database interface, select the Forms object. Double click on Create form
in Design view. An empty form is opened as shown in Figure 4-17 below.

Figure 4-17

The form needs to be linked to the Employee Info table. To do that double click on the
square in the upper left corner (red circle above). The window shown in Figure 4-18
below opens.

MS ACCESS Level-1 103


Figure 4-18

From here, you can set various properties for the form. Those different properties will be
discussed in the higher level courses to for MSACCESS.

To link the Form to the Employee Info table, select the Employee Info from the pull
down menu for the Record Source. This sets the Record Source to the Employee Info
table. In other words it links both the Form and the table.

The moment you select the Record Source, another window appears which contains the
fields of the selected record source. Figure 4-19 shows that.

Figure 4-19

Select a field that you want to appear on the Form, say the First Name field, click on the
field and then drag and drop on the empty form. Figure 4-20 below shows how the field
is built into the form.

MS ACCESS Level-1 104


Figure 4-20

You can choose more fields to build into the Form and you can arrange them on the form
the way you like.

Design the form to look like Figure 4-21 below.

Figure 4-21

After you put a field on the form, to move it around the form to the location you want do
the following. Click on the field or on its label on the form, move the mouse pointer tell it
touches the edge of the field or its label (The mouse pointer changes into a hand), click
and drag the field to the location you want.

As you might have noticed, moving the field around results in the label moving with it.
Also moving the label results in the field to move with it. To move the field or the label

MS ACCESS Level-1 105


without moving the other then you need to do the following. Click on the field, you will
notice that black squares surround the edge of the field and also a black square appears on
the field label. Look at Figure 4-22 that illustrates this.

Figure 4-22

The big black square on the edge (indicated by red arrows above), enables you to move
the field or the label without moving the other. Click on the big black square and drag
and drop the field or its label to the required location. Change the design of the form to
look like Figure 4-23 below.

Figure 4-23

You might have noticed that there is a gap between the field and its label indicated by the
red arrow above. To reduce this gap, you can do it individually for the fields above, or
you can do it for the 3 fields in one shot.

MS ACCESS Level-1 106


To reduce this gap, select the Grade field then press the Shift key and while pressing the
Shift key select the other two, Salary and Department. All the 3 fields become outlined
as shown in Figure 4-24 below.

Figure 4-24

Move the mouse pointer till it reaches the location indicated by the red arrow above. On
the left edge of the grade box there are three outline black filled squares. Move the mouse
pointer to the middle one (red arrow above), the pointer should change into two arrows
. After that click and pull the edge of the box to the left. Notice that the box does not
move, it gets expanded. (Remember if we want to move the box either we pick the corner
which has the biggest black filled square or we move the pointer inside until it changes
into a hand and then we move the field around). Since at the beginning you have selected
the 3 fields, the three of them will get expanded together. Look at Figure 4-25 below.

Figure 4-25

MS ACCESS Level-1 107


To activate the form, click on the View button . Figure 4-26 below shows the active
form.

Figure 4-26

On this form, you can perform all the functions as within the table. You can select a
record and delete it, you can store a new record, you can modify the fields and you can
also filter. You can sort ascending or descending and you can browse through the records.
Notice how the form is giving a restricted view to the table as not all the fields are
showing.

Suppose now you want to add another field to the form. Say the Married field. You need
to get back to the Form Design view before you can add the field. To do that, click on the
Design View button . The form changes back to the design view, from the field list
you can now drag the married field and drop it on the form in the location you want.
Look at the design of the form now as shown in Figure 4-27.

MS ACCESS Level-1 108


Figure 4-27

To change the caption of the form (red oval above), right mouse click on the square in the
above left corner of the form (pink circle) and choose Properties from the list. The Form
properties shows. In the caption, write the caption you want. For example, Employee
Information. Figure 4-28 shows that.

Figure 4-28

Activate the form, notice how the caption is showing on the form top bar. Figure 4-29
shows that.

MS ACCESS Level-1 109


Figure 4-29

Note the caption above.

From the Form properties, you can turn on and off the:
(1) Record selectors
(2) Navigation buttons
(3) Dividing lines

Figure 4-30 shows what these are on the form.

Figure 4-30

MS ACCESS Level-1 110


Turn the above three off and notice how the form will look like. Figure 4-31 below shows
that.

Figure 4-31

The form looks much nicer and more professional. However, there is no way now to
move to the next record. Maybe it is better to keep the navigation buttons after all. In the
next level of the MSACCESS course, we will show you how you can navigate by adding
professional looking buttons rather than using the navigation buttons of the MS Access
forms.

MS ACCESS Level-1 111


5.0 Introduction to Reports
Reports are MS ACCESS objects that enable you to create professional reports for
printing.

The reports are another means of presenting the data, but their main use is to convert the
data into a format that is suitable for printing.

The reports can be based on tables or queries.

MSACCESS provides the designer with a wizard to create reports quickly and easily. It
provides also a Design view for the reports which gives more control on the report
design.

5.1 Creating a report by using the wizard

From the MSACCESS general database interface click on Reports from the Objects
menu. Double click on Create report by using wizard. The Report Wizard opens as
shown in Figure 5-1.

Figure 5-1

This resembles the wizard we previously used when creating a query or a from. Select the
table or query you want to base your report on. The fields of that table or query will be
displayed in the Available Fields box. Select the fields you want to appear in the report
by clicking on the Select Field button . If you press the Select All Fields button

MS ACCESS Level-1 112


then all the fields will be selected. The selected fields will move to the Selected
Fields box. You can move fields back by clicking on the Remove and Remove All
button . Select all the fields and then click the Next button.

The report Wizard then asks if you desire to create any groups. We will come back to this
in a moment, click Next. The Wizard asks if you want to sort by any field, select the
OLD_NEW, then the First Name and then the Last Name as shown in Figure 5-2
below.

Figure 5-2

Click the Next button. The Wizard asks about the desired lay out. Keep on Tabular as
shown in Figure 5-3 below.

MS ACCESS Level-1 113


Figure 5-3

We will examine the other layouts in a moment. Notice that from here, you can also
select the page orientation, Portrait or Landscape. Keep the Adjust the field width so all
fields fit on a page checked. Click the Next button.

The Wizard asks about the report style. MSACCESS provides you with a few report
styles that are truly professional in look. Select the Style that you like. Figure 5-4 below
shows the selection screen.

MS ACCESS Level-1 114


Figure 5-4

Click Next. Give the report a name, for example Employee Info Report as shown in
Figure 5-5 below.

Figure 5-5

MS ACCESS Level-1 115


Click the Finish button to see your report. Figure 5-6 below shows how the report looks
like.

Figure 5-6

Notice that the report is sorted by the OLD_NEW, First Name, Last Name fields just as
we instructed the Wizard to create the report.

Close the report.

To illustrate what the other report layouts are, this how the report would look like if we
choose the different layouts.

If we choose the Columnar layout then the report would look like Figure 5-7 below
shows.

MS ACCESS Level-1 116


Figure 5-7

And for the Justified layout, the report would look like Figure 5-8 below shows.

MS ACCESS Level-1 117


Figure 5-8

These layouts resemble the layouts we stumbled upon while exploring the different
layouts found in the Form Wizard.

5.2 Creating Groups

You can create logical groups for the results that the report will display. For example,
suppose you want to create a report that groups the results by the Grades, or by the OLD
NEW field, or even by both fields. MSACCESS will sort the records and report them
back based on the field you select for the group.

Let’s take the following example. Create a report using the wizard based on the
Employee Info table. Select all the records for the report. The report will ask about the
grouping as shown in Figure 5-9 below.

MS ACCESS Level-1 118


Figure 5-9

We will select the OLD_NEW field, to be our group field. So select the field and click
on the Select button . Figure 5-10 below shows how the report Wizard looks like after
selecting the group field.

Figure 5-10

MS ACCESS Level-1 119


Notice how the OLD_NEW field is shown in blue to emphasize the fact that it is a group
field.

Click the Next button. Select the sort fields as we previously did, but this time choose
only the First Name and the Last Name as shown in Figure 5-11 below.

Figure 5-11

Click the Next button. MSACCESS now asks you to choose the layout. Examine the
layout selection form as shown in Figure 5-12 below.

MS ACCESS Level-1 120


Figure 5-12

As you can see the layouts are entirely different from the ones we saw when there were
no groups.

Keep the selection on the Stepped and click on the Next button. Select the report style,
for this example, select the Corporate style (you can select any other style you want).
Click on the Next button. Give the report a name, say Employee Info Report 2 and then
click the Finish button. Figure 5-13 below shows part of the resultant report.

MS ACCESS Level-1 121


Figure 5-13

Notice that the report has been constructed based on the groups. We have the NEW
group and the OLD group. Within these two groups the records have been reported
sorted ascending by the First Name and then the Last Name.

Let’s create another report. Do exactly the same steps for constructing the above report
but this time choose Grade to be the grouping field. Name the report Employee Info
Report 3 and save it. Figure 5-14 shows how the report would look like.

MS ACCESS Level-1 122


Figure 5-14

This time, the report is based on grouping the records by the Grades, the records for each
group are reported next sorted by the first name and last name.

Let’s make things more interesting, this time create the same report and choose
OLD_NEW and GRADE for the grouping. The Report Wizard should like Figure 5-15
after selecting the groups.

MS ACCESS Level-1 123


Figure 5-15

Now continue the same steps. The report should look like Figure 5-16 below.

MS ACCESS Level-1 124


Figure 5-16

As you can see, the report is composed of two groups. The OLD_NEW and then the
Grades. An important thing to mention, is that the report can cover more than one page.
The report we have right now, covers two pages. To move between pages use the
navigation buttons on the report .

MS ACCESS Level-1 125


This time, and to get more appreciation of the groups, create the same report but create
the OLD_NEW to be a sub group of the Grade i.e when selecting the groups select the
Grade field and then the OLD_New field.

The groups should be as shown in Figure 5-17 below.

Figure 5-17

The report should look like Figure 5-18 below.

MS ACCESS Level-1 126


Figure 5-18

The difference between the two reports is clear. In this example, the main groups are the
grades and the New and OLD groups are sub groups within the main groups.

MS ACCESS Level-1 127


Now let’s explore the different layouts available for the reports when we use groups.
The first layout is the Stepped layout that we have been using in all the examples above.

To illustrate the different layouts, we will use the same Employee Info Report 2, but
with different layouts.

The Block layout is shown in Figure 5-19 below.

Figure 5-19

MS ACCESS Level-1 128


The Outline-1 layout is shown in Figure 5-20.

Figure 5-20

MS ACCESS Level-1 129


The Outline 2 layout is shown in Figure 5-21 below.

Figure 5-21

5.3 Modifying report design

Open Employee Info Report 2, by double clicking on it from the general MSACCESS
interface. Figure 5-22 shows the top part of the report.

Figure 5-22

MS ACCESS Level-1 130


Notice how the labels for the First Name and the Last Name fields are not showing in
Full (red circle). Same goes for the labels of other fields too. Suppose we need also to
change the Employee Info Report 2 (red arrow in Figure 5-22) to say something else,
like HR Report – Confidential – Employee Information.

We can do that and lot’s of other things by opening the report in the Design view. Open
the report in the Design view by clicking on the Design view button . Figure 5-23
below shows the design view for the report.

Figure 5-23

The design view is showing the following sections:

(1) Report header – Whatever goes into this section will appear only once on the
header of the first page of the report
(2) Page Header – Whatever goes into this section will appear on the top of every
page of the report
(3) OLD_NEW header – While designing reports there will be a header for every
group that we create. In this case the OLD_New group. Whatever goes into this
section will appear at the beginning of every different group of the OLD_NEW
field.
(4) Detail – What ever goes into this section will detail the records data that belong to
the Group or Sub Group.
(5) Page Footer – Whatever goes into this section will appear at the bottom of every
page of the report.

MS ACCESS Level-1 131


(6) Report Footer – Whatever goes into this section will appear at the last page
towards the end of the report. (In this report this section is empty).

Let’s change the report header to read HR Report – Confidential – Employee


Information. You can do that by double clicking on the label field in the design view
and then type in the header above. You can also change the font, color, increase or
decrease the font size, make it italic or bold, change the background color utilizing the
bars on the top . You can also increase
the label size itself, by clicking on it and then enlarging it by dragging it from one of the
black squares that show on the edges.

Say we design it as shown in Figure 5-24.

Figure 5-24

We can make more space for the Page Header Section by enlarging it. To do that, move
the mouse to the end of the Page Header Section, i.e the top pf the OLD_NEW Header
section, the mouse cursor changes shape as shown in Figure 5-25.

MS ACCESS Level-1 132


Page 5-25

Click on the left mouse button and drag the section down to enlarge it as shown in Figure
5-26.

Figure 5-26

See the extra space created (red arrow). Since we have more space let’s make some
cosmetic changes to the field labels. You can resize any label by clicking on it. It will be
selected and the black squares on its edges will show. Click on any of the black squares
and drag to resize the label. Let’s change the shapes and sizes till they look as shown in
figure 5-27 below.

MS ACCESS Level-1 133


Figure 5-27

An excellent way to move the labels up and down is to select them, and then move them
by clicking on the arrows rather than using the mouse. This way they move on the grid
with every press on the arrow buttons in the required direction.

Move the thick blue line by selecting it and then use the arrows.

Notice that when the wizard created the report, it aligned the fields widths (in the Detail
section) and the labels widths (in the page header section). Keeping them aligned in this
way gives the report a smooth look.

Look at the salary label and field. We want now to reduce the width of both of them still
keeping the width for both of them the same. To do that, select the Salary label then
press the Shift key and while pressing the Shift key select the Salary field so both of
them is selected at the same time as shown in Figure 5-28.

MS ACCESS Level-1 134


Figure 5-28

Click on the middle black square (marked in red above) and while clicking reduce the
size of the Salary label. You will notice that the Salary field size is also reducing in the
size. This way we make sure that both of them remain aligned. Figure 5-29 shows the
result.

Figure 5-29

Next let’s move the department label and field next to Salary. To do that select both of
them using the Shift key and then using the arrows move them to the left. Since both of
them are selected they will move together.

MS ACCESS Level-1 135


Using the techniques you learned, redesign the form to look like Figure 5-30 below.

Figure 5-30

Run the report in its current design, it looks much better than before. To run the report
you need to click on the Preview button . Figure 5-31 shows how the report looks
like.

MS ACCESS Level-1 136


Figure 5-31

Let’s add one more touch, let’s center the grades, salaries and EmployeeIDs exactly
under the labels. To do that, you need to go back to the Design view. Click on the Design
view button . The report design view will appear. Select the EmployeeID, Salary
and Grade fields using the Shift key. This is shown in Figure 5-32.

MS ACCESS Level-1 137


Figure 5-32

Click on the Center Align button . Run the report now, the result is much neater as
shown in Figure 5-34.

Figure 5-34

MS ACCESS Level-1 138


5.4 Creating a report in design view

The report wizard is a good tool to create reports. However, MSACCESS Professionals
prefer to start building a report from scratch using the design view.

Let’s create a report in the design view. In the general MSACCESS Interface, click on
the Reports Object and then double click on Create report in Design view. An empty
report design interface opens as shown in Figure 5-35.

Figure 5-35

As we did when designing the form, we need to link this empty report to its data source.
The data source can be a table or a query. To do that, double click on the square in the top
left corner (red circle above). Or you can right mouse click the same square and then
choose Properties from the menu that appears.

The properties dialogue box appears as shown in Figure 5-36 below.

MS ACCESS Level-1 139


Figure 5-36

Click on the Data tab, Figure 5-37 shows how the properties would look like.

Figure 5-37

From the pull down menu for the Record Source property, choose the Employee Info
table. After you do that, a box called Employee Info with all the fields of the table pops
up as shown in Figure 5-38.

Figure 5-38

MS ACCESS Level-1 140


The report is now linked to the Employee Info table. Note that the empty report has 3
sections; Page Header, Detail and Page Footer. Let’s create a report that shows the
First Name, Last Name, Grade and Salary.

To do that drag and drop those fields from the Employee Info fields to the Detail section
of the report.

Figure 5-39 shows how the design looks like.

Figure 5-39

Run this report. It looks like Figure 5-40 below.

MS ACCESS Level-1 141


Figure 5-40

The reason the report looks like this is because what ever you put in the detail section is
going to be repeated with every record. This is why the labels repeat with every record.
The big spaces between the records are there because of the spaces we are keeping in the
detail section. Those spaces are highlighted below in Figure 5-41.

Figure 5-41

MS ACCESS Level-1 142


Let’s now reduce those spaces. To do that, select all the fields and labels (use the Shift
key) and move them up (use the arrows after selecting them), then resize the detail
section just to fit from below (move the mouse just to the bottom of the detail section till
the mouse pointer changes and then pull it up to resize it) as shown in Figure 5-42.

Figure 5-42

Run the report now and notice how the spaces disappear. Figure 5-43 shows that.

Figure 5-43

Still it does not look nice. Go back to the design view and redesign it as shown in Figure
5-44.

MS ACCESS Level-1 143


Figure 5-44

Run this version of the report. Figure 5-45 shows how it looks like.

Figure 5-45

It is nicer but not that neat. Redesign it now to look like Figure 5-46.

MS ACCESS Level-1 144


Figure 5-46

You might have noticed that the field and its label move together. When you move one of
them the other one moves. To move one of them independent of the other you need to
grab it from the big square that appears on the upper left corner when selected.

Run the above report. The result is as shown in Figure 5-47.

Figure 5-47

This is the neatest we got so far. To make it even neater, let’s remove the labels from the
Detail and put them in the Page Header section so that they appear only once on the top
of each page.

MS ACCESS Level-1 145


To do that, delete the labels from the Detail section. Select the label you want to delete
and then press the Delete key, the label disappears. Now let’s create the labels in the
Page Header section. To do that, we need to activate the Tool Box menu. Click on the
Toolbox button . This toggles the tool box on, if you click on it again it toggles it off.

The Tool Box is shown in Figure 5-48.

Figure 5-48

This Tool Box contains various tools that you can use to create professional reports.
Click on the Label button in the Tool Box (red circle).

Move the mouse pointer inside the report design view, notice how the pointer changes to
the letter A to denote that the label tool is activated. Move the pointer to the Page
Header section, click on the left mouse button and while continuing to click draw a
rectangle. This rectangle is going to be your label. Once you finish drawing the rectangle,
remove your finger from the left mouse button, notice how a pointer cursor starts
blinking inside the rectangle.

Type in First Name and when finished, click with the left mouse button outside the label
area. Notice how the label is now placed on the report.

It is very important to point out that labels are merely text on the report. It is not linked in
any way to a data source. Whatever, you fill in it will be displayed. If you place a label in
the detail section, it will be displayed with every record that is read from the data source.

Create other labels for Last Name, Grade and Salary and resize them, move them around
to have the neat design as shown in Figure 5-49.

MS ACCESS Level-1 146


Figure 5-49

Run this report. The result is shown in Figure 5-50.

Figure 5-50

Go back to the Design view, center the Salary and Grade. Also make the labels bold.
From the Tool Box draw a line under the labels in the Page Header section. The design
should look as Figure 5-51.

MS ACCESS Level-1 147


Figure 5-51

Run the report, the result should look like Figure 5-52 below.

Figure 5-52

What happens if we put a line under the fields in the Detail section?
Remember that what ever we put in the Detail section gets repeated for every record.
Change the design as shown in Figure 5-53.

MS ACCESS Level-1 148


Figure 5-53

Notice that we have added a red line in the Detail section under the fields and we have
changed the line in the Page Header section into green. To change the line color simply
select it and then click on the Change Color button and then select the color you
want.

Figure 5-54 shows how the report looks like.

Figure 5-54
The report looks pretty neat now.

If you have been following the same instructions since the beginning, you will notice that
this report has only one page. To introduce the concept of the report header and footer
that we touched on before, let’s make this report a multi page report. We have two ways

MS ACCESS Level-1 149


to do that, either we increase the number of records in the Employee Info table which
will take some time to enter data.

The easiest is to increase the spaces between the records on the report. To do that, open
the report in Design view and then increase the space allocated for the Detail section by
dragging its lower edge below. Figure 5-55 below shows that.

Figure 5-55

Notice the extra space that we introduced (Red arrow). Run the report, it should look like
Figure 5-56.

Figure 5-56

MS ACCESS Level-1 150


If you have introduced enough space, the report will have 3 pages. If it does not have 3
pages, then go back again to the design view and increase the space further.

We want to write something at the top of the first page of the report and something else at
the end of the report. The right places where such writing should go to is into the Report
Header and Report Footer sections. Go back to the design view of the report, you will
notice that report header and footer sections are not there.

In order to introduce them, right mouse click on any of the section tabs (blue rectangles)
as shown in figure 5-57 below.

Figure 5-57

A menu will pop up. Click on Report Heade/Footer. Notice how the report Design view
changed as shown in Figure 5-58 below.

Figure 5-58

Notice how two new sections have been introduced: The Report Header section and the
Report Footer section.

MS ACCESS Level-1 151


Redesign the report as shown below in Figure 5-59 (use the label from the Tool Box to
introduce this change and resize the section to suit the purpose).

Figure 5-59

Run the report. The first page of the report should be as shown in Figure 5-60 below.

MS ACCESS Level-1 152


Figure 5-60

The last page of the report should look like Figure 5-61 below.

Figure 5-61

While the 2nd (and any other middle pages if the report has more than 3 pages) will have
none of those. It is only appearing at the report start and the report end.

Redesign the report as showing in Figure 5-62 below (Remove all unnecessary spaces).

MS ACCESS Level-1 153


Figure 5-62

Run the report. It should look like Figure 5-63 below.

Figure 5-63

MS ACCESS Level-1 154


Notice how the report header and footer are on the same page because the report has only
a single page.

5.5 Sorting an Grouping

We can sort the report by clicking on the Sorting and Grouping button . Click on this
button and the sorting and grouping window will appear as shown in Figure 5-64 below.

Figure 5-64
From the Filed/Expression select a field to sort by, say the Grade field and then choose
Ascending in the Sort Order. The window should look like Figure 5-65 below.

Figure 5-65

Run the report. It should look like Figure 5-66 below.

MS ACCESS Level-1 155


Figure 5-66

Notice how the records are now sorted by the Grade. Let’s now sort by the Grade and
First Name. The Sorting and Grouping window should be set as shown in Figure 5-67
below.

Figure 5-67

The report should now look like Figure 5-68 below.

MS ACCESS Level-1 156


Figure 5-68

The report is now sorted by Grade, then by First Name.

To create a Group, select the field you want to group by and then set the Group Header
property to Yes (red circle) as shown in Figure 5-69 below.

Figure 5-69

Notice the symbol placed to the left of the Grade field (Blue Circle) to indicate that this
field is now a group. Have an extra look at the design view, you will notice that there is a
new section created called Grade Header as shown in Figure 5-70.

MS ACCESS Level-1 157


Figure 5-70
Run the report. Figure 5-71 shows how it looks like. Notice the gaps that are now
showing at the beginning of each group (blue arrows).

Figure 5-71

This Gap is the space in the Grade Header section. Change the design of the report as
shown in Figure 5-72 below. (Use the label tool from the Tool Box).

MS ACCESS Level-1 158


Figure 5-72

Run the report. It should look like Figure 5-73 below.

Figure 5-73

MS ACCESS Level-1 159


Let’s add an interesting thing now. Suppose we want the report to say beginning of
Group -1 as the beginning of grade 1 and then beginning of group -2 at the beginning of
grade 2 and so on. To do that, we have two ways:

Way number 1 is to use the Text Box tool from the Tool Box. Figure 5-73B shows the
Text Box tool (red circle).

Figure 5-73B

Click on this tool and then add a text box to the design of the report as shown in figure 5-
74.

Figure 5-74

Notice that MSACCESS automatically adds a label to the text box, in this case it is
text13. Delete that label by click on the label and then pressing the Delete button.

Notice that the text box has the word Unbound inside. The meaning of this is that the text
box is still not connected to any field. There is a big difference between labels and text
boxes. Labels just display whatever you right into them, while text boxes have the ability
to connect to a data field in a table or a query and read data from them. This table or
query is the record source that we set for the report.

MS ACCESS Level-1 160


To link the text box to a field, double click on the text box to show its properties or right
mouse click on it and then choose Properties from the menu. The Text Box properties
window will be displayed as shown in Figure 5-75 below.

Figure 5-75

Click on the Data tab. From the pull down menu of the Control Source choose Grade.
The control source is the field that will be feeding the text box with data. This is shown in
Figure 5-76 below.

Figure 5-76

The design view of the report should look like Figure 5-77.

MS ACCESS Level-1 161


Figure 5-77

Run the report now. It should look like Figure 5-78 below.

Figure 5-78

Notice how the newly introduced text box is reading the correct Grade from the table at
the beginning of each group. Change the design of the report to look like Figure 5-79
below.

MS ACCESS Level-1 162


Figure 5-79

What we have done is that we have changed the label by Typing in Grade. Then the text
box we have reformatted it to have a yellow background and bold font.

The report should look like Figure 5-80 below.

Figure 5-80

Since we are mentioning the grade at the beginning of each Group, there is no need to
repeat for every record in the group. Change the report design to match the design in
Figure 5-81.

MS ACCESS Level-1 163


Figure 5-81

And the report should look like Figure 5-82 below.

Figure 5-82

This report is much nicer.

We mentioned that there is second method to add the text box that is linked to the Grade
field in the table. The other method is much easier and resemble the same step that we did

MS ACCESS Level-1 164


at the beginning of this report design. From the field list of the Employee Info table
(Which is the record source for this report) shown in Figure 5-83 below, drag the Grade
field to the required location. MSACCESS will add it and create a label automatically.
Now do the same thing, delete the label and redesign the text box to resemble the one we
created. (This is another quicker method).

Figure 5-83

If the you close the field list you can easily open it by clicking on the Field List button
.

MS ACCESS Level-1 165

You might also like