Download Access 2002 Prepared by Angela Dejak November 2005 Access

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

SQL wikipedia , lookup

IMDb wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft Access wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Concurrency control wikipedia , lookup

Functional Database Model wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Database wikipedia , lookup

Ingres (database) wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Versant Object Database wikipedia , lookup

ContactPoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Access 2002
Prepared by Angela Dejak
November 2005
Access 2002
Part 1 - Getting Started
TOPICS TO BE COVERED
What is a database?
Starting Access
Creating a Database Using Design and Datasheet Views
Create a database
Create a table
Define fields in a table
Open a table and add records to a table
Preview and print the contents of a table
Close a database and quit Access
Open a database
Use a form to view data
Create a custom report
Creating and Running Queries
Use Microsoft Access Help
Design a database to eliminate redundancy
Prepared by Angela Dejak
2
Table of Contents
Introduction ......................................................................................................................................5
What is a Database? .............................................................................................................5
Starting Access and Creating a New Database ................................................................................6
To Start Access ....................................................................................................................6
Creating a Table ...................................................................................................................6
To Create a Table .................................................................................................................7
To Save a Table ...................................................................................................................8
Adding Records to a Table...............................................................................................................8
To Add Records to a Table ..................................................................................................8
Closing the Table and Quitting Access ............................................................................................9
Opening a Database .........................................................................................................................9
Correcting Errors in the Data ...........................................................................................................9
Previewing and Printing a Table ....................................................................................................10
To Preview .........................................................................................................................10
To Change the Orientation .................................................................................................10
To Print a Table .................................................................................................................10
Creating Additional Tables ............................................................................................................11
Using a Form to View Data ...........................................................................................................11
Creating a Form .................................................................................................................11
To Close and Save a Form .................................................................................................11
To Open a Form .................................................................................................................12
Creating a Report ...........................................................................................................................12
To Create a Report .............................................................................................................13
Exercise on Creating the Report .......................................................................................13
Using Text Data in Criteria ............................................................................................................15
Using Wildcards.................................................................................................................16
To Use Criteria for a Field Not included in the Results.....................................................17
Using Comparison Operators .............................................................................................17
Prepared by Angela Dejak
3
Using Compound Criteria ..................................................................................................17
Using Compound Criterion Involving AND ......................................................................18
Using Compound Criterion Involving OR .........................................................................18
Sorting Data in a Query......................................................................................................19
Sort on Multiple Keys ........................................................................................................19
Omitting Duplicates ...........................................................................................................19
To Use a Computed Field in a Query.................................................................................20
To Calculate Statistics........................................................................................................20
Saving a Query ...................................................................................................................20
Joining Tables ................................................................................................................................21
Using Microsoft Access Help ........................................................................................................22
Designing A Database ....................................................................................................................23
Prepared by Angela Dejak
4
Introduction
What is a Database?
A database is a collection of data related to a particular topic or purpose. The data is organized
in a manner that allows access, retrieval and use of that data. Information in a database is
organized into fields, or categories, such as customer name, city or phone number. A key field is
a field that contains unique information for each record, such as a student number for students or
an ID number for staff. A group of related fields, such as all the information on a particular
customer is called a record. A collection of related records is called a table.
Access is a relational database which means that more than one table can share information and
these tables are linked by a common field.
Tables are the most important objects in an Access database because they contain all the data
within the database. A database could also contain six other objects:- queries, forms, reports,
pages, macros and modules. Data can be entered and edited in only four of these objects:tables, queries, forms and pages, however, no matter how the data is entered, it is physically
stored in a table object. Data can be printed from a table, query, form, page or report. The
macro and modules provide additional database productivity and automation features, however,
all objects are stored in one database file.
A database management system, such as Access, allows you to use a computer to:
create a database,
add, change and delete data in the database,
sort the data in the database,
retrieve data in the database, and
create forms and reports using the data in the database.
In Access, a database consists of a collection of tables and the one we will be working with
consists of two tables. The Client table contains information about the clients. The Technician
table contains information about the technicians to whom these clients are assigned.
Prepared by Angela Dejak
5
Starting Access and Creating a New Database
To Start Access
1.
2.
3.
Put a formatted disc in the A drive and open the Microsoft Suite, or
Click Start, Access,
Select the File, New and from the right side of your screen, under the New From
Template tab, select General Templates
4.
You now have to Save the file to the A drive and provide a name for the database. In this
case we will be calling the database Pilotech Services
The Pilotech Services database is now created and the database window is displayed on the
desktop.
The Database window, is a special window that allows you to access easily and rapidly a variety
of objects such as tables, queries, forms and reports. To do so, you will use the various
components of the window.
Creating a Table
An Access database consists of a collection of tables. Once the database is created, you must
create each of the tables within it. In this project you will create both the Client and Technician
tables as shown on the next page.
To create a table, you describe the structure of the table by describing the fields within the table.
For each field you indicate the following:
a)
b)
c)
Field name - Each field must have a unique name. In the client table, the field names are
Client number, Name etc
Data Type - Shows the type of data that can be stored in the field. Some fields contain
numbers, text or currency.
Description - Allows you to enter a detailed description of the field.
You also need to indicate which field or fields make up the primary key (the unique identifier)
for the table.
With the information below showing the structure and the data for the Client table you are ready
to now start creating the table.
Prepared by Angela Dejak
6
To Create a Table
1.
2.
Click on the New button in the Pilotech Services database
Click Design View, OK
Start typing the information in the field name, Data type and description as per the information
below You now have to select a Primary Key and the logical one to choose is the Client
Number. Right click on the field and select Primary Key. . When the information is entered,
the last step is to save the table within the database.
A - Structure for the Client Table
To Save a Table
1.
2.
3.
Click the Save icon on the toolbar
Type Client as the table name, OK
Close the client table window
There are two steps to creating a table, first we created the structure and saved the table. The
second step is to add the records to the table.
Prepared by Angela Dejak
7
Adding Records to a Table
To Add Records to a Table
1.
Right click the Client table, the following shortcut menu appears
2.
Select
Open
on the shortcut menu
The table window displays the Datasheet view for the Client table. The record selector is
positioned on the first record. Start typing the information and use the tab key to move to the
next column. When all the client information is added we will close the database.
B - Data for the Client Table
Prepared by Angela Dejak
8
Closing the Table and Quitting Access
When you are finished working with a table it is a good idea to close it. This helps to keep the
screen clear and prevents you from making accidental changes to the data in the table. If you are
no longer working in the database, you should close the database as well.
1.
2.
3.
Click the close button on the Client Table
Click the close button on the Pilotech Services database
Click the close button for Access
Opening a Database
To start working with any of the tables in the database, the database must be open. To open an
existing database,
1.
2.
3.
Click the Start button
Click My Recent Documents. (Find your database file)
Click on the name of the file to open it.
The Client table will be displayed and if you were to add more data to the table you could access
the table by right clicking on the client and opening the table.
Correcting Errors in the Data
Check your entries to ensure they are correct. Mistakes can be corrected by using either the
backspace key (if you see the mistake before pressing the tab key) or by clicking in the cell with
the error and make the necessary changes.
If you have added a record in error. Select the record by clicking in the record selector area of
the record in question, and press the DELETE button, or right click and select delete record.
Prepared by Angela Dejak
9
Previewing and Printing a Table
When working with a database, you will often need to print a copy of the table contents. The
Client table is wider than the screen so you will need to change it to Landscape orientation in
order to have all the fields printed. A good way to do this is by previewing what the table would
look like.
To Preview
1.
2.
Right click the Client
Select Print Preview from the shortcut menu
It will only display up to the column with the Postal Codes. To change the orientation.
To Change the Orientation
1.
2.
3.
4.
Open the file then click on File on the menu bar
Select Page Setup
Click on the Page tab
Change orientation to Landscape, OK
To Print a Table
While in Preview, check that the table will be printed with all fields visible and when satisfied,
1.
2.
Click the Print icon, or
File, Print
Prepared by Angela Dejak
10
Creating Additional Tables
Most databases consist of more than one table. The above process needs to be repeated to create
the second table for this exercise.
To Create an Additional Table
Make sure the Pilotech Services database is open.
1.
2.
3.
Click on the New button
Select Design View
Click OK
Start adding data to the table and remember to select Tech Number as the Primary key. When
finished adding the data, save the table and call it Technician.
Using a Form to View Data
In creating tables, you have used Datasheet view (data on the screen displayed as a table). You
may also use Form view which allows you to see a single record at a time. With datasheet view,
you can see multiple records at the same time, however, if the table has many fields you cannot
see all the fields at the same time. With form view, you see one record, you see all the fields.
Creating a Form
You must first create a form to use Form view.
1.
2.
3.
Open the Pilotech Services database
Select the Client table
Click on the New Object:AutoForm icon (This is on the Menu Bar)
The form is now created for the first client
listed.
To Close and Save a Form
1.
2.
3.
Click on the Close button (x), a dialogue box appears
Click Yes (The name of the table becomes the name of the form automatically)
Click OK
Now that you have saved the form you may use it any time by opening it. This is similar to
opening a table. With the Pilotech Services database open do the following:
Prepared by Angela Dejak
11
To Open a Form
1.
2.
3.
Click on the Forms tab
Right click on Client
Select Open
You can now select records in the form by using the navigation buttons to move between records.
To be able to view the data in Datasheet view,
1.
2.
Click the view icon on the tool bar, as seen below
Select Datasheet View
You can now see all the information and the record selector will be on the record you were in
when in Form view. You may close the window.
Creating a Report
We printed a table earlier by using the Print button. The data in the report was presented in an
organized manner, however, it was not very flexible. It included all the fields and you were not
able to change any of the fields, eg the title.
Now we will create a report with a page header containing the title, which appears at the top of
each page. The detail line, which are the lines printed for each record, contain only those fields
you specify.
Prepared by Angela Dejak
12
To Create a Report
1.
2.
3.
4.
5.
Click the tables tab
Select the Client table
Click the New Object: AutoForm
button
Click on Report
Select Report Wizard from the dialogue box and click OK
The Report Wizard dialogue box appears and you can follow the instructions to create a report.
To indicate the fields that will be used in the report, click the field in the Available Fields list
box. Click the add (>) button and this will move the field from Available Fields to the Selected
Fields list box. The selected fields will be the ones included in your report.
Exercise on Creating the Report
Create the report with the following features:
1.
The report will have the following fields:
Client Number
Name
Billed
Paid
1.
2.
3.
4.
5.
6.
There will be no grouping, nor need to sort the data
Make sure the Tabular is selected as the layout and the orientation is Portrait
Select Corporate style
Type Billing Summary Report as the new title
Click Preview Report
Click Finish
Your report appears .
Prepared by Angela Dejak
13
What is a Query?
A query is a question represented in a way that Access can understand. To find the answer to a
question, you must first create the query and then you instruct Access to run the query. The query
will perform the necessary steps to obtain the answer.
Creating a New Query
To create a query, entries are made in a special window called a Select Query Window. Let us
create a query using the Pioltech Services database.
1.
2.
3.
4.
5.
6.
7.
Open the Pilotech Services database
Select the Tables tab
Select the Client table
Click on the New Object:AutoForm button arrow
Click Query
Click Design View
Click OK
Maximize the window and then point to the dividing line separating the upper and lower panes
and when the mouse pointer becomes a two-headed arrow, drag down about half way. Now move
the pointer to the lower edge of the Field List box and again drag so that all the fields in the
Client table are visible.
Only the fields that appear in the design grid will be included in the results of the query. To place
the fields in the grid,
a)
b)
Double-click the Client Number field to include in the query
Do the same for the Name and Tech Number fields.
Running the Query
1.
Click on the Run button on the
toolbar
Closing a Query
1.
2.
Click on the Close button
Choose No (we do not wish to save the query as yet)
☺ TIP - To include all fields in a query, double-click on the * (asterisk) in the field list box and
Prepared by Angela Dejak
14
then point to the Run button on the toolbar.
Clearing a Query
1.
2.
Click on Edit
Click on Clear Grid
Access clears the design grid so you can enter your next query.
Entering Criteria
When you use queries, usually you are checking records that satisfy some criterion. You enter
criteria on the criteria row in the design grid below the field name to which the criteria applies.
Listed below are some examples of criteria that are available:
Using Text Data in criteria
Using Wildcards
Using Numeric Data in criteria
Using Comparison Operator in a criterion
Using Compound Criterion Involving AND
Using Compound Criterion Involving OR
Using Text Data in Criteria
To use text data (data in a field whose type is text) in criteria, type the text in the criteria row
below the corresponding field name.
We are now going to query the Client table and display the client number, name, billed and paid
amounts of the client DE76. To do this,
1.
Include the following fields to the Query: Client Number, Name, Billed and Paid.
2.
Type “DE76" as the criteria for the Client Number Field. Your screen should look like
the one displayed on the following page.
Prepared by Angela Dejak
15
3.
Run the query.
4.
After running the query to return to the query window, Click on the Design View button.
NOTE: Before doing a new query, it is recommended that you delete the previous query, or clear
the query grid.
Using Wildcards
Wildcards are symbols that represent any character or combination of characters. There are two
special wildcards in Access. The asterisk (*) represents any collection of characters. Example
Gr* represents Gr followed by any collection of characters. The question mark (?) represents
any individual character. Example, t?m would be the letter T followed by any single character
followed by the letter m, such as Tim or Tom.
a)
b)
c)
d)
Return to the Query1:Select Query window
Click the criteria row under the Name field
Type LIKE Gr*
Run the query
Prepared by Angela Dejak
16
To Use Criteria for a Field Not included in the Results
Sometimes you may choose to have criteria for a field that should not appear in the results of the
query. You add the field to the query then remove the check mark from its Show row in the grid.
1.
2.
3.
4.
Add the City field to the query.
Type Toronto as the criteria for the City Field
Remove the check mark from the Show check box.
Run the query
Using Comparison Operators
Access assumes that the criteria you enter involves equal matches unless you specify otherwise. If
you wish something other than an exact match, you must enter the comparison operator. These
are:> (greater than),
< (less than),
>= (greater than or equal to)
<= (less than or equal to)
NOT (not equal to)
To find all clients whose billed amount is greater than $300
1.
2.
Type >300 as the criterion for the Billed field.
Run the query.
Using Compound Criteria
At times you will have more than one criterion that the data for which you are searching must
satisfy. This is referred to as compound criterion and there are two types.
The AND criterion, each individual criterion must be true in order for the compound criterion to
be true. Example, you would be able to find the clients that have a billed amount greater than
$300 and whose technician is number 12.
The OR criterion, this is true providing either individual criterion is true. Example, you would be
able to find the clients that have billed amount greater than $300 or whose technician is 12.
Prepared by Angela Dejak
17
Using Compound Criterion Involving AND
Use an AND criterion to find those clients whose billed amount is greater than $300 and whose
technician is number 12.
1.
2.
3.
4.
5.
Clear the query window
Include the Client Number, name, billed, paid and tech number fields in the query
Type >300 as the criterion for the billed field
Type 12 as the criterion for the Tech number field.
Run the query
Using Compound Criterion Involving OR
Use an OR criterion to find those clients whose billed amount is more than $300 or whose
technician is 12.
1.
2.
3.
Remove the 12 from Tech number
Type 12 in the or: field (below criteria)
Run the query
☺ Tip
To combine criteria with OR, the criteria must go on separate lines in the Criteria area of the grid.
Prepared by Angela Dejak
18
Sorting Data in a Query
In some queries, the order that the records are displayed does not matter. However, in some
queries the order is very important. To place records in answer to a query in a particular way, you
sort the records. The field or fields on which the records are sorted is called the sort key. If you
are sorting on more than one field, the more important field is called the primary key (major),
while the less important field is called the secondary key (minor). You can then choose the
order, whether ascending or descending.
NOTE: If you specify more than one sort key, the sort key on the left will be the major sort key
and the one on the right will be the minor key.
To sort the cities in the client table in alphabetical order,
1.
2.
3.
4.
Clear the query grid
Include the City field in the design grid
Click on sort and then select ascending
Run the query.
Sort on Multiple Keys
Sort the data by descending billed amount within the technician number. This means the
technician number field is the primary key while the billed field is the secondary key.
Omitting Duplicates
Sometimes when you sort data, duplicates are included. To remove the duplicates,
1.
2.
3.
4.
5.
6.
Right-click the empty field following City
Click properties on the shortcut menu
Click Unique Values and click the down arrow
Select Yes
Close the Query properties sheet
Run the query.
Prepared by Angela Dejak
19
To Use a Computed Field in a Query
1.
2.
3.
4.
5.
6.
7.
Click Edit, Clear Grid
Include the Client Number and Name fields
Right-click the field row in the third column
Click Zoom
Type Outstanding Amount: [Billed] - [Paid]
Click OK
Run query.
To Calculate Statistics
1.
2.
3.
4.
5.
6.
7.
Click Edit, Clear Grid
Right-click the grid
Click Totals on the shortcut menu
Include the billed field
Click on the Total row in the Billed column
Click the arrow and select AVG
Run the query
Saving a Query
After running the query
1.
2.
3.
Click the save button
Give the query a name
It will be saved under the Query tab
Prepared by Angela Dejak
20
Joining Tables
One of the key features that distinguishes database management systems from file systems is the
ability to join tables, which is to create queries that draw data from two or more tables. There are
a few types of joins available, however, the one we will be doing is the natural join.
At present we have one table in our query so we have to add another table to the query.
1.
2.
3.
4.
5.
6.
7.
8.
Right-click any area in the upper pane
Click Show table on the shortcut menu
Click on Technician to select it
Click the add button
Close the Show Table box
Include the Client Number, Name and Tech Number fields from the Client table
Include Last Name and First name fields from the Technician Table
Run the query.
You will notice that a join line appears joining the Tech number in both field lists. The join line
indicates how the tables are related (linked through the matching fields).
NOTE: If you fail to give the matching fields the same name, access will not insert the line.
You can insert it manually by clicking one of the two fields and dragging the mouse to the other
matching field.
Prepared by Angela Dejak
21
Designing A Database
Designing a Database refers to the way data is arranged in the tables and fields. In the examples
we have the design is specified. However, in many cases the databases are large and complex and
the design process can become quite extensive. If you keep one thing in mind this should help
you to design an effective database. Design to remove redundancy. Redundancy means storing
the same information in more than one place.
These are some of the problems that arise when data is stored in multiple records:
1.
2.
3.
Redundancy wastes disc space.
Redundancy makes updating the database more difficult.
A possibility of inconsistent data exists
To resolve this problem, place the redundant information in a separate table. When this occurs,
you need to have one field that is identical in each table.
Designing to omit redundancy will help you to produce good and valid database designs.
Prepared by Angela Dejak
22