Access Notes
Access Notes
MICROSOFT
ACCESS
Lectures and Assignments
By Sarah Ahmed
Table of Contents
UNDERSTANDING MICROSOFT ACCESS.......................................................................................................2
What is Microsoft Access?.......................................................................................................................2
Creating Tables........................................................................................................................................2
Assignment 1...........................................................................................................................................9
Importing Data......................................................................................................................................10
What is a Query?...................................................................................................................................11
Calculated Queries:............................................................................................................................14
FORMS...................................................................................................................................................22
1
UNDERSTANDING MICROSOFT ACCESS
What is Microsoft Access?
Access is an example of a database management system (DBMS), which is a computer application
or program that is used to store, manage, and retrieve data in computer files. It is generally
associated with large volumes of data or with files where selective retrieval is desired.
There is a hierarchy of components which constitute the collection of data maintained by any
DBMS: Database
Record
Field
Value
A database is a system for collecting, organizing and retrieving information; databases are
particularly good at working with complex sets of related information.
A database system such as MS Access facilitates a task-driven approach, encouraging you to decide
the most effective way to collect, process and present information.
The database is composed of records, which are structured collections of closely related data. The
nature of the relationship among the data in a record will depend on the purpose of the database. In
a human resources database, each record will contain information about one employee. In a
bibliographic database, a record would correspond to a book, journal article, or other written
work. The structure of a record is determined by the set of fields from which it is composed. Each
field is a place where data with a particular meaning is kept. A human resources database record
would include fields for name, address, social security number, date of employment, salary, and
other information. A bibliographic database record would include fields for a work’s title, authors,
publisher, date of publication, and other information. The content of each field in a record is its
value—the specific text, number, date, or other information stored in that field of that record. There
are different types of databases, but we will be looking only at relational databases, since that is
the type of database managed by Access. In a relational database, there is an additional level in the
hierarchy of data organization: The records of the database are organized into tables, each of which
expresses a particular relation among the data (hence the name relational database).
Creating Tables
Table Views
An Access table has two views: Design view and Datasheet view.
To open a table in Datasheet View:
Locate the table in the navigation pane and double-click
Or locate the table in the navigation pane and choose Right-click > Open
To open in Design View:
2
Locate the table in the navigation pane and choose Right-click > Design View
To switch between views when a table is already open:
Choose Home > Views > View
Tables store data. All other Access objects work with this data. In a relational database data may be
distributed between several related tables.
You need to configure all the tables in which data are stored; we recommend you use Design View
for this as it gives access to all properties.
3
Creating an Input Mask
An Input Mask limits the amount and type of information that can be entered in a field. You have probably already
seen an example of an input mask on an ordinary paper form—the type of form that wants you to write down your
phone number or social security number a certain way and thus provides you with a guide like (___) ___ -_____ or
____-_________. Look familiar? That’s an input mask, pure and simple. There are two ways to create an input
mask:
Use the Input Mask Wizard to create the input mask for you (the fast and easy way). The only problem
withthe Input Mask Wizard is that it can only help you create input masks for phone numbers, social
security numbers, Zip Codes, and date and time fields.
Create the input mask yourself by typing a series of characters in the Input Mask box (the hard way).If
you want to use this method, refer to Table below: Input Mask Characters to see what you have to enter in
order to create an input mask.
In this lesson you will learn how to use the Input Mask Wizard to add an input mask to a field.
[Link] sure the table is displayed in Design view and click the field you want to create an input mask for.
[Link] the Input Mask box in the Field Properties area (the bottom half of the window).
The Build button appears next to the Input Mask box.
Other Ways to Create an Input Mask: Enter characters directly into the Input Mask box in the Field Properties
area.
[Link] the Build button to start the Input Wizard.
The first step of the Input Mask Wizard appears. All you need to do here is select the input mask you want to
choose.
If you want to try an input mask to see how it works, click the input mask you want to use and then type some
sample text in the “Try It” box.
4. Select an input mask from the predefined list and click Next.
Here you can customize the input mask to your specifications.
5. Make modifications to the characters in the Input Mask box, if desired. Click the Placeholder.
Decimal Places:
To work around this problem, manually set the Field Size property of the column to Double or to Decimal.
1. Click the DataSheet tab. In the Views group, click the arrow under View, and then click Design View.
4
Note If the table has not already been saved, you will be prompted to save the table with a table name that
you provide.
2. Locate the column that you want to change, and then set the Field Size property of the column to Double or
to Decimal.
Note If you set the Field Size property of the column to Decimal, you must also set the Scale property of
the column. When you set the Scale property, you specify the maximum number of digits that can be stored
to the right of the decimal separator.
5
Creating Relationships:
Open the relationships window by choosing Database Tools > Relationships > Relationships.
1. The first time you open this window the Show Table dialogue will present a list of available
tables. To open this dialogue on future occasions select Relationship Tools > Design >
Relationships > Show Table.
6
2. Add the tables for which you want to define a relationship (select and Add – use CTRL for
multiple selections); Close the dialogue when you’ve added them.
3. Identify the two fields that will make the relationship and then drag one onto the other,
ensuring you position the pointer accurately.
Table 2: “tutorid” is repeated in Modules table. This is an example of One to many relationship.
Many-to-one
Many-to-many
One-to-one
Enforcing referential Integrity:
The Edit Relationships dialogue opens, with the tables and linked fields pre-entered. Choose
how you wish to enforce integrity. The options are:
Join Type
7
This dialogue allows you to configure the join type, which determines whether records are
displayed from the primary table when no matching records are available in the related table.
The default is to display only matching records (an inner join). The alternative (an outer join) is
more usually configured within a query
8
Assignment 1
NAME: _______________
ID: ___________________
I. What is a database?
II. Identify differences between Excel and Access. Which is better for data management and
analytical tools?
III. There are four types of objects Tables, Queries, Forms and Reports in Access. State the
functions of each object.
IV. What is the difference between Foreign Key and Primary Key?
V. Create a table on Access with following fields and 10 records:
i. ID: 16u00_ _ _(Primary Key)
ii. NAME: Use Text(Write 10 names of your class fellows)
iii. Age: Use validation rule since all students have age less than 20.
iv. City: (using Lookup wizard; Lahore, Islamabad and Karachi
9
Importing Data
Data can be imported into Access if it is in a recognisable format. Common formats include other
Access databases, Excel, CSV and other text formats.
Importing is carried out using a Wizard and although different choices need to be made for
different types of data file, the process is similar. When importing data you can create a new table
or append data to an existing one. Appending can be problematic as the field order and data types
must match exactly.
Access tables
1 Select External Data > Import & Link > Access. An import dialogue opens
3 Make sure the Import option is selected rather than the Link option, then choose OK.
4 A full list of all the tables will be presented. Choose one or more to import – use CTRL to select
more than one – and select OK to import the data.
Excel
When importing from Excel using the Wizard, a view of the data is provided to help ensure you
make the correct choices. You have the option to append the data to an existing table, but for this to
be successful the field order and data types must match exactly.
1 Select External Data > Import & Link > Excel, choose Browse and locate the Excel file.
2 Make sure the Import (or Append) option is selected rather than the Link option – to Append,
you must specify a table – then choose OK.
3 The worksheets available are listed – select the one you need and check the data, then choose
Next.
4 In the next few steps you must make the choices that are appropriate to your data – whether the
list has column headings, which field is a primary key, data types, etc. It’s usually OK just to accept
the defaults. A table name is then suggested, which you can accept or change.
5 Finally, you may opt to save the import settings for future use. This is useful if you regularly
import data from the same spreadsheet.
10
What is a Query?
A query is an Access object used to view, analyze, or modify data. The query design determines the
fields and records you see and the sort order. Additionally, queries allow you to join two or more
related tables, concatenate text fields, summarize data, create calculated fields, and append,
update, or delete records based on specified criteria. Only a query's design is stored in the
database. When the query is run, it retrieves data as specified by the query design and returns it in
the form of a dataset or recordset which is a real-time representation of the underlying tables.
Query results are often used as the basis for reports, displayed in forms, or used to create other
queries.
There are two main categories of query types in Access -- Select and Action queries. A select query
allows you to join related tables and choose the fields and records to display. Select queries choose
records from one or more tables in the database (or attached tables from other database files)
according to criteria you specify. A select query can later be converted to an action query, if
desired.
Queries
display records/fields from 1 or more tables or queries in a dataset (real-time
representation of the data); changes made to the dataset will also change the underlying
data source
can be used to select a subset of records according to your criteria
are a permanent object in the database
allow you to choose the fields you wish to view, select, or sort by
can be used to join related tables together and display the results in a single
datasheet, e.g., employees and orders.
allow calculations to be performed; e.g., you can create a new field to display the
result of multiplying two fields together or concatenate one or more text fields.
result set may be used as the data source for forms, reports, or additional queries.
allow “mass” actions to multiple records at once through an Action Query; these include
Update,
o Delete, Append and Make Table queries.
can be used to find and display duplicate records (Find Duplicates), records that do not
have a match in a related table (Find Unmatched) or Crosstab Queries (cross tabulations
that correlate data with two types of information, e.g., sales by date and country).
Query Views
There are different query views as indicated above. Design View displays the
query grid where you can create and modify your query. Datasheet View shows
the record set that is a result of your query design and in most cases data may be
modified through this view. When you create a query, Access constructs the
equivalent SQL (Structured Query Language) statements. If you are familiar with
SQL, you can edit or create your query directly using the SQL view.
Query Design
As with other Access objects, you can either create the query in Design View or
use the Simple Query Wizard. Note: The Simple Query Wizard only allows you to
choose a table and fields. If you wish to add criteria or sort, you will need to go to
the Design View.
11
To create a query in Design View using the query grid:
A Crosstab query calculates sum, average, count, or other type of total for data that you wish to
group by more than one field. The data result can be grouped by columns and rows. The wizard
will help you decide which columns to use for row headings and which to use for column headings
and also the type of calculations you wish to perform.
Selecting Fields
All the fields contained in the table are displayed in the field list.
To add a field to a query, drag the field name from the list to a cell
in the Field row of the query grid. To add more than one field at a
time, hold down the CTRL key and click the fields you want to add,
then drag the group to a cell in the Field row. To add all fields at
once, double-click on the "*".
Sort/Show
Records in a query can be sorted in ascending or descending
order. To sort, place the field name in a cell, click on the Sort row
then choose Ascending or Descending. If you specify a sort for
more than one field, the sort order will be from left to right as the
columns are listed.
The Show checkbox allows you to add fields to the grid that you wish to sort or select by, but not
display them in the query's output.
12
Wildcards for partial matching
Access allows the use of wildcards that represent one or more characters when specifying criteria.
When using wildcards, the expression must be preceded by the keyword Like.
The asterisk symbol * matches 1 or more characters:
Like “ch*” would return any names that begin with Ch such as Charles and Charlotte.
Like “*.[Link]” would return any email addresses that end with .[Link].
Like “*Theory*” would return ‘Quantum Theory for Beginners” and “Thermodynamics Theory”.
13
Valid Access expressions can use the following:
AND vs OR
Use of And allows you to specify two or more criteria for selection. By using And, you are
able to specify that any record returned in the query dataset meets all criteria specified. For
example, if you specify criteria for state as “Virginia” and specify “Jones” in the last name
field, the records that are returned will be for those persons whose last name is Jones and
who also live in Virginia.
To enter criteria statements for multiple fields, place all criteria statements on the same line. And
may be used in a single field in conjunction with a range of values, e.g., >= 2 and <= 6, or in
conjunction with Not, e.g., Not CA and Not TX which would return records containing any state
except those two.
Note: Access assumes that each criteria row under the first is an Or statement. Therefore, if
you're looking for records that meet one criterion or another criterion, either 1) place the
criteria on different lines or 2) enter multiple values on the same line and separate them by
“or”.
Calculated Queries:
The common arithmetic operators can be included (+ - * / ^)
Brackets can be used to control the order of precedence or avoid ambiguity
Numbers can be used but must not be enclosed in quotes
Label: [Field +-*/ [Field
Name] Name]
14
Examples:
Total cost: [Cost] * [Quantity] * 10%
Actual price: [Price] – [Discount]
Concatenation
Any number of fields can be joined together (concatenated) by separating their field names with an
ampersand (&). New text enclosed in speech marks can also be joined.
Spaces are not added when joining fields, so you may need to add the space character (enclosed in
speech marks) into the formula.
Examples:
"Telephone Number: " & [TelNumber]
[FirstName] & " " & [LastName]
Left([FirstName],1) & " " & [LastName]
Note: Concatenation can be used with both text and numbers.
[First Name] &" "& [Last Name] Jane Doe
[Last Name] &", "& [First Name] Doe, Jane
[City] & ", " & [State] & " " & [Zipcode] Fort Collins, CO 80523
[Unit Price] * [Quantity] Multiples the values in the Unit Price field times
the values in the Quantity field
Ordered in: DatePart("q",[Order Date]) In a field captioned "Ordered in", the DatePart
function is used to categorize orders by quarter,
using the Order Date field.
2 Identify the field containing values you need to count (total, average etc) –
3 Determine the field(s) which identify the groups of similar records – in this example we want to
count the number of students in each module. Add to the grid.
4 Enable the Totals row by selecting Design > Show/Hide > Totals.
5 In this new Total row, select the required function for the appropriate field.
Tip: Use as few fields as possible, and only apply a function on one field.
Total Row
When in Datasheet view, Access provides a quick way to display totals, averages etc at the bottom
of the fields. If you need to do this frequently, consider developing a report for this purpose
instead(see later).
1 In datasheet view select Home > Records > Totals to enable the extra row.
2 Select the appropriate function from the drop-down menu at the bottom of the relevant field. The
options will depend on the data type.
15
Parameter Queries
Filtering criteria are normally saved within a query, but sometimes it is useful to only specify the
criteria when the query is run. For example, a query that returns information related to a student
could be designed to prompt for a last name when it is run, then only return results that match the
entered name.
16
Assignment 5
BBA/BSc I
ICT I
VI. Concatenated Field in a query: Combine Pet’s name and the owner’s name to find the
full name of the pet.
Click in a blank column: Full Name: [PetType] & " " & [PetName]
VII. Calculated Field in a Query: Calculate the total cost incurred by each owner.
Assume total cost is equal to number of limbs multiplied by the total Vet Bills. Also, sum
the Total cost incurred.
VIII. Specifying criteria in a query:
a. Display what all Cats like to eat.
b. Display all cats who have limbs greater than 4.
c. Display the names of the pets whose dates of birth is between 1/1/1990 and
2/23/1994.
(Hint: between #1/1/1990# And #2/23/1994#)
IX. Creating a Parameter Query: In the criteria box type [Enter a PetName]
X. Create a crosstab query to show the number of boy and girl pets by date of birth. Format
the date of birth field so that the pets are grouped by year of birth. For formatting in a new
column: (Yearof birth: Format([PetDOB],"yyyy"
17
Aggregate Functions in Queries
You can perform calculations on groups of records using aggregate functions found under the
Totals button. This includes Sum, Avg, Min, Max, Count, StDev, Var, etc. Totals provides
summary information for all records in a table.
When you click on the Totals button, Access displays Group By in the Total row for any fields in
the design grid. You create totals by replacing Group By with a function. Note: you must group the
records by at least one field in order to apply an aggregate function.
Top Values
To view only the top values by either percent or number of values, click on
the Return button on the Query Tools, Design tab.. The Top Values
property is used in conjunction with a sorted field. For example, you might
want to return the top 10 values or the top 25 percent of all values in a field.
Note: if you sort a field in descending value and apply this property, the values
at the top of the list will actually be the lowest values.
For example:
would return "large" if the value in the Qty field is greater than 10. Otherwise, it would return "small".
18
Microsoft Access
FORMS
1. Create a new basic form based on the Pet table, which resembles the example below:
Close the form, saving it with the name Basic Pet Details.
Create a new query based on the Pet and PetType tables and add the following fields:
19
Microsoft Access
2. Create a new form based on your query using the AutoForm Columnar wizard. Modify the
form so that is resembles the form below:
Feel free to experiment with the formatting and layout of the form
Save the form with the name Pet Details and close it.
20
Microsoft Access
3. Create a basic form based on the PetType table using the Form tool on the ribbon.
21
Microsoft Access
4. Create a form and subform combination based on the PetType and Pettables to resemble
the example shown below:
The AllowAdditions property of the subform has been set to No, so no new records can be added
Save both forms with sensible names and close them down.
GROUPED REPORT
5. Create a report based on the Pet table using the Report Wizard as
follows:
Fields to include - PetName, Gender, PetDOB, VetBills
Grouping - Gender
Sorting - PetName (ascending)
Summary - Average VetBills
Layout - Landscape, Stepped
Style - Office
Title - Average Pet Costs By Gender
Your report should resemble the example shown below:
22
Microsoft Access
23