Professional Documents
Culture Documents
Unit 4
Unit 4
4.1 Introduction
4.2 Objective
4.3 Creating tables
4.4 Creating reports
4.5 Creating queries
4.6 Working with forms
4.7 Entering records in the table
4.8 Working with relationships in MS Access
4.9 Using primary and secondary keys in table
4.10 Importing and exporting data from and to excel
4.11 Summary
4.12 Answers/Solutions
4.13 Further Reading
1
4.1 INTRODUCTION
Database is used as backend in software applications, where as the front end is some
application through which user interacts with the database (backend). For example,
the login windows are front end where we supply user id and password, and the
storage house of such authorized user id's and passwords are stored is a database. To
understand data base with real life example, Let us we consider tap in kitchen as front
end, thus the water tank on the terrace/roof can be considered as database ie backend
and here the subject of database is water, being subject oriented also. The data in the
database can be accessed more easily being available at the single point and there is
no need to update the data at multiple points if some change is required in a data
value, thus data redundancy (Unnecessary repetition of data) problem is also taken
care of. Redundancy leads to inconsistency which makes data erroneous. For example
if an employee is working in multiple departments in an organization, and his
residential address is changed, then all the departments should update his address in
his personal information record. By mistake if one of the departments has not
changed his record as per the new address, in that case address information is not
reliable for communication. Database is solutions to such kind of data related
problems.
2
How to start MS Access
Figure 1
3
4.2 OBJECTIVE
The objective of this unit is to enable the user to use database in efficient manner and
to make user well versed with different types of jobs , which can be done using data
base. In the end of the unit user will learn to
• Create tables
• Create reports
• Create queries
• Use forms
• Entering records in the table
• Working with relationships
• Using primary and secondary keys in table
• Importing and exporting data from and to excel
The steps to create a table are given below. Figures 2 to Figure 4 explain the
procedure to open a database.
4
Figure 4
Figure 4
Figure 4
5
There are three ways to create table
Create table in Design View : Figure 5 to Figure 15 explains the creation of table in
design view.
Figure 6
Figure 6
6
Figure 9
In the given below diagram click on the X on the right top to close
Figure 9
Figure 9
7
Figure 13
Figure 13
Figure 13
Figure 13
8
Figure 15
Figure 15
Figure 16
9
Figure 18
Figure 18
10
Figure 21
Figure 21
Figure 21 11
Figure 22
You can also create table directly. Double click on create table by entering data
option and you will get a table having ten fields by default, which can be further
increased by using right click of mouse on any field's head and choose insert column
option.
Some controls (graphical objects) are used to make link between a report and its
record source. These graphical objects can be labels, text boxes or lines. Text boxes
are used to display the value of respective field from the table or query where as the
labels are used to show the name of the field.
Creating a report
There are two ways to create reports. First is by using design view and another is
using wizard. By using wizard one can create a standard report, which can be
customized in design view. A wizard asks you questions and creates a report
based on your answers
Customizing a report
12
Record source Change the tables and queries that a report is based on.
Sorting and grouping data You can sort data in ascending or descending order.
You can also group records on one or more fields, and display subtotals and grand
totals on a report.
Report window You can add or remove Maximize and Minimize buttons,
change the title bar text, and other Report window elements.
Sections You can add, remove, hide, or resize the header, footer, and details
sections of a report. You can also set section properties to control the appearance
and printing of a report.
Controls You can move, resize, or set the font properties of a control. You can
also add controls to display calculated values, totals, current date and time, and
other useful information on a report.
The pictorial representatin of the procedure is shown through Figure 23 to Figure 34.
13
Figure 23
Figure 26
Figure 26
Figure 26
14
Figure 29
Figure 29
15
Figure 29
Figure 32
Figure 32
16
Figure 32
Figure 34
Figure 34
17
We can verify the details shown in the above report (Figure 34), with the table given
below (Figure 35)
Figure 35
SELECT QUERIES
The most common type of query is select query. It is used to retrieve data from single or
multiple tables. The retrieved records are displayed in a datasheet. The select query is
also used for aggregate functions like sum, count, average, and other types of totals. It
can also be used to group records.
PARAMETER QUERIES
Such queries are more interactive. At the time of execution, parameters will be asked
from the user to set criteria for retrieving particular records only as per the need.
We can design the query to get more than one value from the user. For example user can
be asked to supply to values of data and then display those records having date of joining
between these two dates.
18
Parametric queries are also useful when used with respect to forms or reports also. For
example, an interactive query can ask the user to supply the value of month to display the
report of that particular month only.
CROSSTAB QUERIES
Crosstab queries are used for easier analysis of data. Such queries are used to calculate
sum, average, count, and such kind of totals for data which is grouped by two information
patterns, one down the left side of the datasheet and another across the top.
ACTION QUERIES
If we use an action query to remove records of fail students then after the
execution of query, resultant table will be
19
corresponding tables from the main database of students, instead of retyping the
same details again.
• Make-Table Queries By the help of make table query, we can create new
tables from other tables. There are situations when we want to gather data from
other single or multiple tables to make a single consolidated table, there we make
use of the make table query. Make table queries are also helpful to export data to
other MS Access objects like queries, forms and reports with the help of a table
created for such activities.
SQL QUERIES
Structure Query Language (SQL) queries are used to manage relational databases like
MS Access. The queries are created by the help of SQL statements. A query can be
created in the design view. Design view is a window which shows the design of MS
Access objects like tables, forms, queries, reports and macros etc. Design view helps the
use to create new database objects and also to modify the design of existing objects. Also,
when we create a query in design view, access constructs the SQL equivalent statements
automatically w.r.t such designed queries. Generally most of the properties in the
property sheet in query Design view have equivalent clauses and options which are
available in the SQL view also. SQL-View can also be used to create a customized SQL-
query. However, after changes, the query might not be displayed the way it was
previously in Design view.
Also, the design grid (grid used to design a query) is having limitations when we have to
design some specific SQL queries having sub-queries and relational algebra related
functionalities. Also there are situations when we want to communicate to an ODBC
database server instead of executing the queries through Microsoft Jet database engine to
process the data, where design grid is again having its limitations. Other situation where
the design grid is not the appropriate choice for the user is, the data definition language
(DDL) and union queries which are used to create and alter the objects in the database
and to combine the result of two or more select queries respectively.
For sub-queries we have to enter the SQL in the Field row or the Criteria row of the
query design grid. Sub query is a query which is embedded within a SQL query.
20
VII. Select create query by using wizard
VIII. Select table or query to make query from tables/queries Combo box
IX. Select the fields from Available Fields List box to Selected Fields List box one
by one using, command button or select all available fields by clicking on
command button.
X. Select the type of query you want i.e. detailed or summary
XI. Give title to query
XII. Select one of the given below options
a. Open the query to view information
b. Modify the query design
Figure 38 Figure 38
Figure 38
21
Figure 43
Figure 43
Figure 43
Figure 43 Figure 43
22
Figure 46
Figure 46
Figure 46
23
In the table given below the ID field is not shown in the query result.
Figure 47
24
4.6 WORKING WITH FORMS
Form is a data base object which is used as front end application (Application through
which user interacts with database).
A form is a type of a database object that is primarily used to enter or display data in a
database. You can also use a form as a switchboard that opens other forms and reports in
the database, or as a custom dialog box that accepts user input and carries out an action
based on the input.
Generally forms are bound to one or more tables and queries in a database.
Most forms are bound to one or more tables and queries in the database. A form may or
may not contain all the field s of the tables or queries with which it is associated. The
record source of a form refers to the fields in the tables and queries with which the form
are bound.
A bound form stores or retrieves data form the associated record source. The form also
contains other components like title, date, time, and page number, which is stored in the
form’s design like
• Graphic elements, such as lines and rectangles, are stored in the form's design.
• Data comes from the fields in the underlying record source.
• A calculation comes from an expression, which is stored in the form's design.
• Descriptive text is stored in the form's design.
Graphical objects are used to make a link between form and record source. Such
graphical objects are called controls e.g. command button, text box, check box, scroll
bar etc. Text box is used as the most common type of control to display and enter
data. Labels are used to display description text. Command buttons are used to invoke
some action associated with some procedure or data element.
CREATING A FORM
We can create a form either in design view or by using form wizard. It is more
convenient to create a form using wizard and then customize it as per the specific needs
in the design view. A form can be bound to a table or query.
Record source Change the tables and queries that a form is based on.
25
Controlling and assisting the user You can set form properties to allow or prevent users
from adding, deleting, or editing records displayed in a form. You can also add custom
Help to a form to assist your users with using the form.
Form window You can add or remove Maximize and Minimize buttons, short cut
menus, and other Form window elements.
Sections You can add, remove, hide, or resize the header, footer, and details sections of a
form. You can also set section properties to control the appearance and printing of a
form.
Controls You can move, resize, or set the font properties of a control. You can also add
controls to display calculated values, totals, current date and time, and other useful
information on a form.
26
STEPS TO CREATE A FORM
Figure 49
Figure 49
27
Figure 52
Figure 52
Figure 52
28
Figure 55
Figure 55
Figure 55
29
Figure 58
Figure 58
Figure 58
30
Figure 60
Figure 60
If we go back to table object and click on the table tblStudent, we can view the newly
added record through form, available in the table, shown in the Figure 61.
Figure 61
31
Figure 62 shows the procedure to make changes in the form by opening the form in
design view. Figure 62 shows the effect of changes in the form design.
To open a form in design view, select the form and click on the design view button
shown by red circle in the Figure 62. We have added form title, date and time on the
form.
Figure 62
32
Figure 63
33
4.7 ENTERING RECORDS IN THE TABLE
There are multiple ways to enter records in a table. One can enter records directly in
the table by opening the table using following steps
Figure 64
Figure 65
34
• Click on the first field of the record pointed by new record pointer (*), Figure 66
Figure 61
One to One
Relationship between Rollno and student i.e. one student has only one Rollno. In a
one-to-one relationship, each record in Table A can have only one matching record in
Table B, and each record in Table B can have only one matching record in Table A.
This type of relationship is not common, because most information related in this way
would be in one table. You might use a one-to-one relationship to divide a table with
many fields, to isolate part of a table for security reasons, or to store information that
applies only to a subset of the main table. For example, you might want to create a
table to track employees participating in a fundraising soccer game. Each soccer
player in the Soccer Players table has one matching record in the Employees table.
One to Many
Relationship between Manager and employees i.e. one manager can have many
employees in his department. Also, a one-to-many relationship is the most common
type of relationship. In a one-to-many relationship, a record in Table A can have
many matching records in Table B, but a record in Table B has only one matching
record in Table A.
35
Many to Many
Relationship between subjects and students i.e. many subjects can be chosen by many
students. In a many-to-many relationship, a record in Table A can have many
matching records in Table B, and a record in Table B can have many matching
records in Table A. This type of relationship is only possible by defining a third table
(called a junction table) whose primary key consists of two fields— the foreign keys(
It is a primary key of another table. It is used to relate tables) from both Tables A and
B. A many-to-many relationship is really two one-to-many relationships with a
third table. For example, the Orders table and the Products table have a many-to-
many relationship that's defined by creating two one-to-many relationships to the
Order Details table. One order can have many products, and each product can appear
on many orders.
Relationships play very important role to bring the information from different sources at
a common place. After we make relationships, we can make a form, query or report to
display the information from different tables.
Points to remember
• When you create a relationship between tables, the related fields don't have to
have the same names.
• However, related fields must have the same data type unless the Primary key
field is an AutoNumber field.
• You can match an AutoNumber field with a Number field only if the FieldSize
property of both of the matching fields is the same.
• For example, you can match an AutoNumber field and a Number field if the
FieldSize property of both fields is Long Integer. Even when both matching
fields are Number fields, they must have the same FieldSize property setting.
I. Close any tables you have open. You can't create or modify relationships
between open tables.
II. Press F11 to switch to the Database window (A window, which appears when
we open and Access database or an Access project. It displays shortcuts for
creating new database objects and opening existing objects)
III. Click Relationships on the database toolbar, Figure 67.
Figure 62
36
IV. If you haven't yet defined any relationships in your database, the Show Table
dialog box is automatically displayed.
If you need to add the tables you want to relate and the Show Table dialog box
isn't displayed, click Show Table on the toolbar.
V. Double-click the names of the tables you want to relate, and then close the
Show Table dialog box. To create a relationship between a table and itself, add
that table twice.
VI. Drag the field that you want to relate from one table to the related field in the
other table.
To drag multiple fields, press the CTRL key, click each field, and then drag them.
In most cases, you drag the primary key field (which is displayed in bold text)
from one table to a similar field (often with the same name) called the foreign key
in the other table.
VII. The Edit Relationships dialog box is displayed. Check the field names
displayed in the two columns to ensure they are correct. You can change them if
necessary. Set the relationship options if necessary.
VIII. Click the Create button to create the relationship.
IX. Repeat steps 5 through 8 for each pair of tables you want to relate.
When you close the Relationships window (A window in which we view, create,
and modify relationships between tables and queries), Microsoft Access asks if
you want to save the layout. Whether you save the layout or not, the relationships
you create are saved in the database.
Note You can create relationships using queries as well as tables. However, referential
integrity (Rules that we follow to preserve the defined relationships between tables when
we enter or delete records) isn't enforced with queries.
Let us take a database (IGNOU), having tables tblAddresses, tblStudent as shown below
Figure 68 37
Now we click on relationships icon on the database toolbar. Show table dialog box is
automatically displayed as shown below
Figure 69
Select table to make relationship and click on add button or just double click on the tables
one by one.
Figure 70
Drag the field you want to relate from one to another table to the related field in the other
table (To drag multiple fields, press the CTRL key, click each field, and then drag them.
In our case click on rollno in table tblAddresses and drag the rollno field and release the
mouse button on ID field of table tblStudent.
38
Figure 71
Figure 72
39
Check your progress 2
40
4.9 USING PRIMARY AND SECONDARY KEYS IN
TABLE
Primary key is used to identify a record uniquely in a table. Primary key prevents
duplicate values in a table for the attribute/column/field which is designated as primary
key. We can not leave any value blank (Null) in the column which is designated as
primary key. A powerful database like Microsoft access is having ability to quickly find
and gather the required information from multiple tables using queries, forms and reports.
To make it happen, each table should include a field or group of fields which identifies
each record uniquely in the table.
We should be careful to assign primary keys in the table. There is always only one
primary key in the table which can be a simple or composite in nature. For example let’s
we consider a table as given below, Figure 73.
Figure 73
In this table ID is taken as primary key. No two records will be having same value of ID
field. Thus we can uniquely find a record on the basis of any one value of ID field.
• Name can not be considered as primary key, because two students can have same
name. If this happens then uniqueness property is violated. Same is case with
Course, Address, Phone, Result, UniversityName. Also, we should consider
future changes in the database. Suppose, if we assign name as a primary key and a
new student is admitted in the course and his name is also Arun, in that case
access will not allow that new record to be added in the table.
41
• Another important point is if multiple fields are having ability to be primary key,
in that case we should select a field which is having small size and ease of access.
In the above table ID and Rollno both fields are having ability to be primary key,
because no two students will be having same value of Rollno. But if choice is
given, one should choose ID as primary key being small in data size.
• Also, in a table if multiple fields are having uniqueness property they are called
candidate keys, and only one is selected as primary key. The rest of the keys are
called alternate keys.
• Also there may be situations when no single field is having ability to preserve
uniqueness. In that case two or more fields are joined together to preserve
uniqueness property to find a record uniquely on the basis of unique value
combination of such keys. This type of joint key called composite or concatenated
key. For example let us consider the following table.
In the above table neither Name nor Surname is having uniqueness property. But
if we join Name and Surname we can preserve the uniqueness property. Thus if
we make primary key by the combination of Name and Surname, that will be
named as composite key. But there may be a situation when even, this
combination fail i.e. if two students are having same Name and Surname also. In
that case go for AutoNumber primary keys, explained ahead.
When a single field is having ability to preserve uniqueness property for data
values contained in that field and also there is no Null value in the field, it is
called single field primary key. For example : Rollno, University Enrollment No,
Pancard No, Bank Account No, Social Security Number (SSN) etc.
42
In situations where you can't guarantee the uniqueness of any single field, you
may be able to designate two or more fields as the primary key. The most
common situation where this arises is in the table used to relate two other tables in
a many-to-many relationship. For example, an Order Details table can relate the
Orders and Products tables. Its primary key consists of two fields: OrderID and
ProductID. The Order Details table can list many products and many orders, but
each product can only be listed once per order, so combining the OrderID and
ProductID fields produces an appropriate primary key.
OrderID ProductID
10001 P101
10002 P102 Each product can be listed
10003 P101 only once per order.
10001 P103
10002 P104
10001 P105
Also, if you feel difficulty to decide the combinations of fields to make primary
key, it also always a good decision to add an AutoNumber, a field data type that
automatically stores a unique number for each record as it's added to a table.
Three kinds of numbers can be generated: sequential, random, and Replication
ID.) field and designate it as as primary key
43
4.10 IMPORTING AND EXPORTING DATA FROM AND
TO EXCEL
Import or link data from a spreadsheet
To use Import utility to import data from spreadsheet to access database, we should
ensure that in the spreadsheet data is arranged in an Microsoft Access compatible format
(in an appropriate tabular format) i.e. spreadsheet has the same type of data in each
respective field/column and the same fields in each row.
I. Open a database, or switch to the Database window for the open database.
II. Do one of the following:
a. To import a spreadsheet, on the File menu, point to Get External Data,
and then click Import.
b. To link a spreadsheet, on the File menu, point to Get External Data,
and then click Link Tables.
III. In the Import (or Link) dialog box, in the Files of type box, select Microsoft
Excel (*.xls).
IV. Click the arrow to the right of the Look in box, select the drive and folder
where the spreadsheet file is located, and then double-click its icon.
V. Follow the directions in the Import Spreadsheet Wizard dialog boxes. If you
are importing from a Microsoft Excel version 5.0 or later workbook, then you
can import from one worksheet within a workbook. You cannot import from
any other multiple-spreadsheet files, such as Microsoft Excel version 4.0
workbooks. To import from these files, you must first save each spreadsheet as
an individual file.
44
Figure 74
Figure 75
45
Figure 76
Figure 77
Figure 78
46
Figure 79
Figure 80
Figure 81
47
Figure 82
Figure 83
Figure 84 48
Exporting to Microsoft Excel or another spreadsheet program.
There are three ways you can use Microsoft Access data with Microsoft Excel or another
spreadsheet program:
Figure 85
Figure 86
49
Figure 87
Figure 88
Figure 89
50
Figure 90
Figure 91
Figure 92
51
Notes
• You can import or link all the data from a spreadsheet, or just the data from a
named range of cells. Although you normally create a new table in Microsoft
Access for the data, you can also append the data to an existing table as long as
your spreadsheet column headings match the table's field names.
• Access attempts to assign the appropriate data type to imported fields, but you
should check your fields to make sure that they are set to the data type you want.
For example, in an Access database, a phone number or postal code field might be
imported as a Number field, but should be changed to a Text field in Microsoft
Access because it is unlikely that you will perform any calculations on these types
of fields. You should also check and set field properties, such as formatting, as
necessary.
4.11 SUMMARY
In this unit, students have learnt to create a database and to store data in tables in an
organized manner. Basic concepts of database are also discussed in this unit, to make
students aware of database terminology also. The different objects of MS Access
database like tables, reports, queries and forms are also covered from importance,
relevance to their creation procedures. Primary and secondary keys usage is also
52
discussed with illustrations to give in depth understanding of such objects. The
techniques to import as well to export data from MS Access as also covered with
pictorial illustrations to make students through with the use of such techniques.
Relationships which are very important and significant features of database is also
covered with an easy step by step learning technique.
4.12 ANSWERS/SOLUTIONS
Check your progress 1:
1. Table
2. Table
3. Cell
4. Relational
5. Information
6. Reports
7. Effective
8. Tables, query
9. Queries.
10. Report.
Solution 2:
Crosstab queries are used for easier analysis of data. Such queries are used to calculate
sum, average, count, and such kind of totals for data which is grouped by two information
patterns, one down the left side of the datasheet and another across the top.
1. Form
2. Data
3. Switchboard
4. Queries
5. Fields
6. Directly
7. Forms.
8. One-To-One
9. Relationship
10. Fields
53
Solution 2:
Relationship between Manager and employees i.e. one manager can have many
employees in his department. Also, a one-to-many relationship is the most common
type of relationship. In a one-to-many relationship, a record in Table A can have
many matching records in Table B, but a record in Table B has only one matching
record in Table A.
1. Primary key
2. Blank
3. Import ,compatible
4. Three
5. Export.
6. Excel
7. Import
8. Appropriate
9. Properties
10. Secondary key
Solution 2:
54