unit 2
unit 2
• Tables: This object class allows a database designer to create the data tables with
their respective fieldnames, data types and properties.
• Queries: This object class is meant to create the SQL compatible query statement
with or without the help of Graphic User Interface (GUI) to define tables, store data
and retrieve both data and information.
Forms: This object class allows the designer to create an appropriate user
interface to formally interact with the back end database, defined by the tables
and queries.
• Reports: This object class is used to create various reports, the source of
information content of which is based on tables, queries or both. Such reports are
designed in Access according to the requirement of end-user.
• Pages: This object class is meant to create Data Access Pages, which can be
posted on a Web site of an organisation using Internet or sent via e-mail to someone
of the organisation’s network.
• Modules: These are the foundations of any application and allow the designer to
create a set of programming instructions, called functions or sub-routines that can be
used throughout the application
Capabilities of MS Access
When a new database is created from the scratch, there is complete control over the
database objects, their properties and the relationships. In order to create a new
database without the help of database wizard (that is an automated process in
Access), the following steps are required:
(i) Open Access Window to choose blank Access database and click OK
button.
(ii) Access responds by displaying File New Database dialog box, which
prompts the designer to enter a file name and a location for the database.
This must be followed by clicking Create button.
(iii) If the task pane is not open, choose File from menu bar and click at new
to open the task pane to create a new database.
A new table appears in Datasheet View. You can start entering data right away, but
you should add some fields first.
5. When selecting a field type, select the smallest or shortest field that is
required for your data. For example, don’t choose Large Number if you only
need to store a 2-digit number.
Type a name for the field.
To change the name of a field header, double-click the field header and type the field
name you want to use.
See the table at the end of this lesson for more information about data types.
Date & Time Birthday: April 7, 1969 Stores dates, times, or both.
Query
A query is a request for data results, and for action on data. You can use a query to
answer a simple question, to perform calculations, to combine data from different
tables, or even to add, change, or delete table data.
As tables grow in size they can have hundreds of thousands of records, which
makes it impossible for the user to pick out specific records from that table.
With a query you can apply a filter to the table's data, so that you only get the
information that you want.
Queries that you use to retrieve data from a table or to make calculations are
called select queries.
Queries that add, change, or delete data are called action queries.
You can also use a query to supply data for a form or report.
In a well-designed database, the data that you want to present by using a form
or report is often located in several different tables.
The tricky part of queries is that you must understand how to construct one
before you can actually use them.
Create Select Query
If you want to review data from only certain fields in a table, or review data from
multiple tables simultaneously or maybe just see the databased on certain criteria,
you can use the Select query. Let us now look into a simple example in which we
will create a simple query which will retrieve information from tblEmployees table.
Open the database and click on the Create tab.
In the tblEmployees table, double-click all those fields which you want to see as
result of the query. Add these fields to the query design grid as shown in the
following screenshot.
Query criteria helps you to retrieve specific items from an Access database. If an
item matches with all the criteria you enter, it appears in the query results. When you
want to limit the results of a query based on the values in a field, you use query
criteria.
A query criterion is an expression that Access compares to query field values
to determine whether to include the record that contains each value.
Some criteria are simple, and use basic operators and constants. Others are
complex, and use functions, special operators, and include field references.
To add some criteria to a query, you must open the query in the Design View.
You then identify the fields for which you want to specify criteria.
Example
Let’s look at a simple example in which we will use criteria in a query. First open your
Access database and then go to the Create tab and click on Query Design.
In the Tables tab on Show Table dialog, double-click on the tblEmployees table and
then close the dialog box.
Let us now add some field to the query grid such as EmployeeID, FirstName,
LastName, JobTitle and Email as shown in the following screenshot.
Let us now run your query and you will see only these fields as query result.
If you want to see only those whose JobTitle are Marketing Coordinator then you will
need to add the criteria for that. Let’s go to the Query Design again and in Criteria
row of JobTitle enter Marketing Coordinator.
Let us now run your query again and you will see that only Job title of Marketing
Coordinators are retrieved.
If you want to add criteria for multiple fields, just add the criteria in multiple fields. Let
us say we want to retrieve data only for “Marketing Coordinator” and “Accounting
Assistant”; we can specify the OR row operator as shown in the following screenshot
−
Let us now run your query again and you will see the following results.
If you need to use the functionality of the AND operator, then you have to specify the
other condition in the Criteria row. Let us say we want to retrieve all Accounting
Assistants but only those Marketing Coordinator titles with “Pollard” as last name.
Let us now run your query again and you will see the following results.
Action Queries
In MS Access and other DBMS systems, queries can do a lot more than just
displaying data, but they can actually perform various actions on the data in your
database.
Action queries are queries that can add, change, or delete multiple records at
one time.
The added benefit is that you can preview the query results in Access before
you run it.
Microsoft Access provides 4 different types of Action Queries −
o Append
o Update
o Delete
o Make-table
An action query cannot be undone. You should consider making a backup of
any tables that you will update by using an update query.
Create an Append Query
You can use an Append Query to retrieve data from one or more tables and add that
data to another table. Let us create a new table in which we will add data from
the tblEmployees table. This will be temporary table for demo purpose.
Let us call it TempEmployees and this contains the fields as shown in the following
screenshot.
Now let us go back to Query design and select the Append button.
In the Query Type, select the Append option button. This will display the following
dialog box.
Select the table name from the drop-down list and click Ok.
In the Query grid, you can see that in the Append To row all the field are selected by
default except Address1. This because that Address1 field is not available in
the TempEmployee table. So, we need to select the field from the drop-down list.
Let us look into the Address field.
Let us now run your query and you will see the following confirmation message.
When you open the TempEmployee table, you will see all the data is added from
the tblEmployees to the TempEmployee table.
Create Queries
Create an Update Query
You can use an Update Query to change the data in your tables, and you can use an
update query to enter criteria to specify which rows should be updated. An update
query provides you an opportunity to review the updated data before you perform the
update. Let us go to the Create tab again and click Query Design.
On the Design tab, in the Query Type group, click Update and double-click on the
field in which you want to update the value. Let us say we want to update the
FirstName of “Rex” to “Max”.
In the Update row of the Design grid, enter the updated value and in Criteria row
add the original value which you want to be updated and run the query. This will
display the confirmation message.
Click Yes and go to Datasheet View and you will see the first record — FirstName is
updated to “Max” now.
On the Design tab, in the Query Type group, click Delete and double-click on
the EmployeeID.
In the Criteria row of the Design Grid, type 11. Here we want to delete an employee
whose EmployeeID is 11.
Let us now run the query. This query will display the confirmation message.
Click Yes and go to your Datasheet View and you will see that the specified
employee record is deleted now.
Click Yes and you will see a new table created in the navigation pane.
Parameter Queries
The best part about queries is that you can save and run the same query again and
again, but when you run the same query again and again by only changing the
criteria then you might consider the query to accept parameters.
If you frequently want to run variations of a particular query, consider using a
parameter query
Parameter query retrieves information in an interactive manner prompting the
end user to supply criteria before the query is run.
You can also specify what type of data a parameter should accept.
You can set the data type for any parameter, but it is especially important to
set the data type for numeric, currency, or date/time data.
When you specify the data type that a parameter should accept, users see a
more helpful error message if they enter the wrong type of data, such as
entering text when currency is expected.
If a parameter is set to accept text data, any input is interpreted as text, and
no error message is displayed.
Example
Let us now take a look at a simple example by creating a parameter query. Let us
open your database and select Query Design in the Create table tab.
Double-click on the tblProjects and close the Show dialog box.
Select the field you want to see as a query result as shown in the following
screenshot.
In the query design grid, in the Criteria row of the ProjectStart column, type [Enter a
project start data]. The string [Enter a project start data] is your parameter
prompt. The square brackets indicate that you want the query to ask for input, and
the text is Enter a project start data is the parameter prompt displays.
Let us now run your query and you will see the following prompt.
Click OK to confirm.
As a result, you will see the details of the project which started on 2/1/2007. Let us
go to the Design View and run the query again.
Enter the date as in the above screenshot and click Ok. You will now see the details
of the project which started on 5/1/2008.
The best part about queries is that you can save and run the same query again and
again, but when you run the same query again and again by only changing the
criteria then you might consider the query to accept parameters.
If you frequently want to run variations of a particular query, consider using a
parameter query
Parameter query retrieves information in an interactive manner prompting the
end user to supply criteria before the query is run.
You can also specify what type of data a parameter should accept.
You can set the data type for any parameter, but it is especially important to
set the data type for numeric, currency, or date/time data.
When you specify the data type that a parameter should accept, users see a
more helpful error message if they enter the wrong type of data, such as
entering text when currency is expected.
If a parameter is set to accept text data, any input is interpreted as text, and
no error message is displayed.
Example
Let us now take a look at a simple example by creating a parameter query. Let us
open your database and select Query Design in the Create table tab.
In the query design grid, in the Criteria row of the ProjectStart column, type [Enter a
project start data]. The string [Enter a project start data] is your parameter
prompt. The square brackets indicate that you want the query to ask for input, and
the text is Enter a project start data is the parameter prompt displays.
Let us now run your query and you will see the following prompt.
Click OK to confirm.
As a result, you will see the details of the project which started on 2/1/2007. Let us
go to the Design View and run the query again.
Enter the date as in the above screenshot and click Ok. You will now see the details
of the project which started on 5/1/2008.
Create Relationships
To create relationships between related tables. One of the goals of good database
design is to remove data redundancy.
To achieve that goal, you divide your data into many subject-based tables so
that each fact is represented only once.
To do this, all the common fields which are related to each other are placed in
one table.
To do this step correctly, you must first understand the relationship between
your tables, and then specify these relationships in your Access database.
Why Create Table Relationships?
MS Access uses table relationships to join tables when you need to use them in a
database object. There are several reasons why you should create table
relationships before you create other database objects, such as forms, queries,
macros, and reports.
To work with records from more than one table, you often must create a query
that joins the tables.
The query works by matching the values in the primary key field of the first
table with a foreign key field in the second table.
When you design a form or report, MS Access uses the information it gathers
from the table relationships you have already defined to present you with
informed choices and to prepopulate property settings with appropriate default
values.
When you design a database, you divide your information into tables, each of
which has a primary key and then add foreign keys to related tables that
reference those primary keys.
These foreign key-primary key pairings form the basis for table relationships
and multi-table queries.
Let us now add another table into your database and name it tblHRData using Table
Design as shown in the following screenshot.
One-To-One Relationship
Let us now understand One-to-One Relationship in MS Access. This relationship is
used to relate one record from one table to one and only one record in another table.
Let us now go to the Database Tools tab.
To create a relationship between these two tables, use the mouse, and click and
hold the EmployeeID field from tblEmployees and drag and drop that field on the
field we want to relate by hovering the mouse right
over EmployeeID from tblHRData. When you release your mouse button, Access
will then open the following window −
The above window relates EmployeeID of tblEmployees to EmployeeID of
tblHRData. Let us now click on the Create button and now these two tables are
related.
The relationship is now saved automatically and there's no real need to click on the
Save button. Now that we have the most basic of relationships created, let us now
go to the table side to see what has happened with this relationship.
Here, on the left-hand side of each and every record, you will see a little plus sign by
default. When you create a relationship, Access will automatically add a sub-
datasheet to that table.
Let us click on the plus sign and you will see the information that is related to this
record is on the tblHRData table.
Click on the Save icon and open tblHRData and you will see that the data we have
entered is already here.
One-To-Many Relationship
The vast majority of your relationships will more than likely be this one to many
relationships where one record from a table has the potential to be related to many
records in another table.
The process to create one-to-many relationship is exactly the same as for creating a
one-to-one relationship.
Let us first clear the layout by clicking on the Clear Layout option on the Design
tab.
We will first add another table tblTasks as shown in the following screenshot.
Click on the Save icon and enter tblTasks as the table name and go to
the Relationship view.
Add tblProjects and tblTasks and close the Show Table dialog box.
We can run through the same process once again to relate these tables. Click and
hold ProjectID from tblProjects and drag that all the way over to the ProjectID from
tblTasks. Further, a relationships window pops up when you release the mouse.
Click the Create button. We now have a very simple relationship created.
Many-To-Many Relationship
et us understand Many-to-Many Relationship. To represent a many-tomany
relationship, you must create a third table, often called a junction table, that breaks
down the many-to-many relationship into two one-to-many relationships. To do so,
we also need to add a junction table. Let us first add another table tblAuthers.
Let us now create a many-to-many relationship. We have more than one author
working on more than one project and vice versa. As you know, we have an Author
field in tblProjects so, we have created a table for it. We do not need this field any
more.
Select the Author field and press the delete button and you will see the following
message.
Click Yes. We will now have to create a junction table. This junction table have two
foreign keys in it as shown in the following screenshot.
These foreign key fields will be the primary keys from the two tables that were linked
together — tblAuthers and tblProjects.
To create a composite key in Access, select both these fields and from the table
tools design tab, you can click directly on that primary key and that will mark not
one but both of these fields.
The combination of these two fields is the tables’ unique identifier. Let us now
save this table as tblAuthorJunction.
The last step in bringing the many-to-many relationships together is to go back to
that relationships view and create those relationships by clicking on Show Table.
Select the above three highlighted tables and click on the Add button and then close
this dialog box.
Click and drag the AuthorID field from tblAuthors and place it on top of
the tblAuthorJunction table AuthorID.
The relationship you’re creating is the one that Access will consider as a one-to-
many relationship. We will also enforce referential integrity. Let us now turn
on Cascade Update and click on the Create button as in the above screenshot.
Let us now hold the ProjectID, drag and drop it right on top
of ProjectID from tblAuthorJunction.
We will Enforce Referential Integrity and Cascade Update Related Fields.
Wildcards
Wildcards are special characters that can stand in for unknown characters in a text
value and are handy for locating multiple items with similar, but not identical data.
Wildcards can also help with getting databased on a specified pattern match.
Access supports two sets of wildcard characters because it supports two standards
for Structured Query Language.
ANSI-89
ANSI-92
As a rule, you use the ANSI-89 wildcards when you run queries and find-and-
replace operations against Access databases such as *.mdb and *.accdb files.
You use the ANSI-92 wildcards when you run queries against Access projects —
Access files connected to Microsoft SQL Server databases. Access projects use the
ANSI-92 standard because SQL Server uses that standard.
ANSI-89 Wildcard Characters
The following table lists out characters supported by ANSI-89 −
* Matches any number of characters. You can wh* finds what, white, and
use the asterisk (*) anywhere in a character why, but not awhile or
string. watch.
? Matches any single alphabetic character. B?ll finds ball, bell, and
bill.
[] Matches any single character within the B[ae]ll finds ball and bell,
brackets. but not bill.
! Matches any character not in the brackets. b[!ae]ll finds bill and bull,
but not ball or bell.
- Matches any one of a range of characters. b[a-c]d finds bad, bbd, and
You must specify the range in ascending bcd.
order (A to Z, not Z to A).
# Matches any single numeric character. 1#3S finds 103, 113, and
123.
% Matches any number of characters. It can be wh% finds what, white, and
used as the first or last character in the why, but not awhile or
character string. watch.
_ Matches any single alphabetic character. B_ll finds ball, bell, and bill.
[] Matches any single character within the B[ae]ll finds ball and bell,
brackets. but not bill.
^ Matches any character not in the brackets. b[^ae]ll finds bill and bull,
but not ball or bell.
- Matches any one of a range of characters. b[a-c]d finds bad, bbd, and
You must specify the range in ascending bcd.
order (A to Z, not Z to A).
Example
Let us now look at a simple example of using these wildcard characters by opening
the query design.
Add the highlighted tables and close the Show Table dialog box.
Add the fields in the query grid which you want to see as a query result.
Run your query.
Let us again go to the query design and add prompt for project name.
Now run your query and let us assume that you don’t know the exact project name,
but you know that the project name contains the words “potion”. Click Ok.
The above step does not generate any result. That is because Access is looking for
an exact match in the project name field. It is looking for that project which has the
word potion in its name.
If you want it so that the users can enter wildcards to replace unknown characters,
then you need to adjust the criteria and include the operator like.
When you run the query, users can use wildcards to replace any number of
characters.
Let us assume that you know that the word potion appears somewhere in the title
but you are not exactly sure where.
You can add an * to replace any number of characters before the word potion and
then add another * after the. Click Ok.
It is always good for the users to know the wildcards they can enter. But there are
users who may not know the wildcards they can and cannot enter. In that case, you
can enter the wildcards yourself.
In this situation, in-between the Like operator and our parameter prompt, we can
add those wildcards and now there's a very specific way we have to write this. After
the word like in quotation marks, enter the wildcard that we are using. In this case,
we have used “*” to replace any number of characters. We will now add this to the
parameter. To do so, we need ampersand (&) symbol and a space. We will now
repeat this step and add another ampersand (&) because we're joining that wildcard
to whatever criteria is entered in for that project name by the user and then “*” in
quotes.
Let us now run our query again. Enter the word potion in the prompt without any
wildcards.
The query will now track those wildcards on either side of whatever is entered here.
We need to simply type potion and press enter.
We now get the results we were looking for and the results will be the same
regardless of what the users enter.
Let us say we want to find every project with the word “the” in the title. Then, you
need to just type the word and Enter or click Ok.
With this query, it becomes easier to search for projects with the word “the” in their
names. The 2nd level of results may also include the project names with the word
“mother” where “the” is part of the word.
Joins
A database is a collection of tables of data that allow logical relationships to each
other. You use relationships to connect tables by fields that they have in common. A
table can be part of any number of relationships, but each relationship always has
exactly two tables. In a query, a relationship is represented by a Join.
What is Join
A join specifies how to combine records from two or more tables in a database.
Conceptually, a join is very similar to a table relationship. In fact, joins are to queries
what relationships are to tables.
The following are the two basic types of joins that we will be discussing in this
chapter −
The most common type of join is an inner join which is also the default join
type in Microsoft Access.
Inner Join will display only the rows or records where the joined fields from
both tables are equal.
This join type looks at those common fields and the data contained within. It
only displays the matches.
Outer Join
Let us now understand what an Outer Join is −
An outer join displays all rows from one table and only those rows or records
from the other table where the joined fields are equal.
In other words, an outer join shows all rows from one table and only the
corresponding rows from the other table.
You can choose the table that will display all rows.
You can create a Left Outer Join which will include all the rows from the first
table.
You can create a Right Outer Join that will include all the rows from the
second table.
Let us now go to the Create tab and create a query from the Design View.
Select tblProjects and tblTasks and close the Show Table dialog box as in the
following screenshot.
Access will display the left table name and the right table name in Join Properties
dialog.
The left column name and the right column name and the first radio button is
to only include rows where the join fields from both tables are equal and that is
the inner join and that is what is selected by default when creating
relationships, when creating a join in the query, but, you can change it.
We have two other options as well; we can include all records
from tblProjects, and only those records from tblTasks where the joined
fields are equal and this one is Left Outer Join.
We have a third option, include all records from tblTasks and only those
records from tblProjects where the joined fields are equal and this one is Right
Outer Join.
Those are the different types of joins you can create easily from the Design View. Let
us select the second option, which is Left Outer Join, and click Ok.
Let us now look into the other steps –
When you look at the relationship line, you will see a little arrow pointing
towards ProjectID in tblTasks. When you run this query, you will see the following
results.
As you can see that it is showing us every single project name, whether or not it has
a related task. You will also see a bunch of null fields. All of this will be blank
because there is no related information in tblTasks, where these fields come from.
Let us now go to the Design View again and double-click on the relationship line.
In the Join Properties dialog box, select the third option which is for the Right Outer
Join and click Ok.
Now look at our relationship line. You will see that a little arrow is now pointing
towards ProjectID in tblProjects. When you run this query you will see the following
results.
Self-Joins
Self-join is another type of Join. A Self-join relates matching fields from within the
same table. For example, look at the employee's table with a supervisor field, which
references the same kind of number stored in another field within the same table —
the employee ID.
If we wanted to know who Kaitlin Rasmussen's supervisor is, we will have to take the
number stored in that supervisor field and look it up within the exact same table in
that employee ID field in order to know that Charity Hendricks is the supervisor.
This table is not the ideal structure for a relational database because it's not
normalized.
If we have a situation where we want to create a query that just lists the employee
names alongside the names of their supervisors, there is no easy way we can query
that unless we create a Self-join.
To see a Self-join, create a table with the following fields and enter some data.
Here we want to create a list again with the first name of the employee and then the
name of the supervisor. Let us create a query from the query design view.
Now, add tblEmployees tables.
Close this dialog box.
Now, add the first name and last name for our employees.
We now need a way to create a relationship between this table and itself. To do that,
we need to open the show table dialog box and add tblEmployees one more time.
We have created another copy of the same table in this query view. Now, we need to
create Self-join. To do that, click on Supervisor in tblEmployees table and hold the
mouse button and drop it right on top of the EmployeeID in that copied table
— tblEmployees_1. Then, add the first name and last name from that copied table.
Let us now run your query and you will see the following results.
It displays the names of the employees along side the names of their supervisors.
And, this is how you create a Self-join in Microsoft Access.
To create a new split form by using the Split Form tool, in the Navigation
Pane, click the table or query that contains the data, and then on
the Create tab, click More Forms, and then click Split Form.
Access creates the form and you can make design changes to the form.
For example, you can adjust the size of the text boxes to fit the data, if
necessary. For more information on working with a split form, see the
article on creating a split form.
A multiple item form, also known as a continuous form, and is useful if you
want a form that displays multiple records but is more customizable than
a datasheet, you can use the Multiple Items tool.
1. In the Navigation Pane, click the table or query that contains the data
you want to see on your form.
2. On the Create tab and click More Forms > Multiple Items.
Access creates the form and displays it in Layout view. In Layout view,
you can make design changes to the form while it is displaying data. For
example, you can adjust the size of the text boxes to fit the data. For
more details, see Create a form by using the Multiple Items tool.
Access creates the form, adds the Navigation Control to it, and displays
the form in Layout view. For more information, see Create a navigation
form.
Financial Accounting Reports can be generated in MS access
easily. Ledger is one of the financial reports through which
accounting transactions are shown in a particular account, during
a given period of time.
A ledger contains eight columns as follows
Dr Ledger Account Cr
3. Select 'Design View' and 'Query L' from the combo control.
4. Click 'OK'.
6. Right click at any part of the report design and choose Report,
Page Header and Footer. With this, the Access responds by
providing two more sections- Page Header and Pager Footer.
9. Select the total control of all the six fields by clicking at each,
while keeping the 'Shift key' pressed. Right click at the 'Select
Label Control' and choose 'Cut'. Now, place the mouse at the Page
Header Section and 'Paste' these controls.
12. Select the 'Text Controls' and 'Amount Field' and modify their
properties.
In this article, we will outline what payroll accounting is, what's included in payroll
accounting, examples of this concept and how to start payroll accounting.
Payroll accounting consists of filing and tracking employee compensation data like
money withheld from each pay check and taxes and benefits the employee receives.
Payroll accountants use financial journal entries to summarize an organization's
transactions and total cash flow. Payroll entries fall under the scope of a general
ledger that sorts all financial information. Once all payroll information is documented
on an employee, human resources can pull this data and send it to their manager to
add it to their performance evaluation.
Payroll accountants need to employ their hard skills to accurately insert relevant
information into the ledger. All expenses, liabilities and assets must be recorded for
financial and compliance purposes. Here is a full list of what you should add when
documenting employee compensation:
You should account for all earnings that an employee made during the fiscal year.
This includes the annual salary, additional wages and overtime pay if applicable. If
your employer offers overtime pay, check with your manager and human resources
to ensure it's been approved for a particular employee. You should also confirm
which employees received a commission on deals they closed for the organization.
Tax figures that are withheld from an employees' salary must be recorded
separately. An employee can determine how much they want to withhold when they
fill out their W-4 form on the first day of employment.
Some examples of taxes withheld from an employee's paycheck and cost employers
include:
Payroll accountants must include the cost of an employer to administer health and
dental benefits, give paid holidays, retirement and worker's compensation if needed.
All the information gathered here can help you budget and find out if you can expand
the benefits offered to employees.
This way you can generate each month payroll details records and
can edit or review later.
You can print pay sheets, pay slips for individual employers.
Open Ms Access.
Add following details or your details what you pay to your staff
monthly.
Step 2: Create Query for Basic Main Table
Create a salary Slip how you want to give to your staff as below.
Each and every details what you want to show in your staff salary
slip.
Step 5: Create a Form to Enter Data