Вы находитесь на странице: 1из 53

Microsoft Access 2007 Tutorial

(Part I)
CIS*1000*DE
What’s a Database?
• A Database is a collection of related 
items/facts/data arranged in a specific structure.
• Some examples of databases include:
– Dictionaries
– Phonebooks or Telephone Directories
– The Periodic Table of Elements
– TV Guide 
– The Sears Christmas Wishbook
– Leonard Maltin's Movie Encyclopedia:                      Career
Profiles of More Than 2,000                             Actors and 
Filmmakers, Past and Present 
Why Use Databases?

• Businesses use databases to keep track of their 
inventories, clientele, employees, sales, expenses, 
and to create and manage online stores, etc.
• Libraries use databases to keep track of their books, 
periodicals, journal articles, and borrowed materials.
• Universities use databases to keep track of student 
fees, registration, transcripts, course information, 
etc.
• The Government uses databases to keep                           
track of YOU (think census statistics).
Data Organization

Field A basic fact or data element such as a


name or number.
Record A set of related fields.

Table A group of related records.

Database A collection of one or more related tables.


Data Organization cont’d

Table Names

Students Enrollment Courses


Student ID Student ID Course Synonym
First Name Course Synonym Course Code
Last Name Semester Professor’s Name
Email Address Credit Weight
Program

Field Names
Data Organization cont’d

Students
• Recall that a record is a set of related                   
fields. Student ID
• Each record contains the same fields                        
First Name
but with different information.
Last Name
– For example, different students will have                       
different names, student ID #’s, etc… Email Address
• Each table usually contains a field,                            
Program
called the Primary Key, that uniquely                              
identifies each Record.
• Multiple Tables can be linked together                          
by using a common field.
Data Organization cont’d

• Primary Keys: Student ID, Course Synonym


• Common Fields: Student ID, Course Synonym

Students Enrollment Courses


Student ID Student ID Course Synonym
First Name Course Synonym Course Code
Last Name Semester Professor’s Name
Email Address Credit Weight
Program

Relationship Lines
Microsoft Access
• Microsoft Access is an application that 
allows us to create small databases.

• In this lab we’re going to create a simple 
database that contains two tables:
1. Students
2. Cities

• But first…
Open Microsoft Access 2007

START Double click on 
the ICON on 
desktop
PROGRAMS OR
Microsoft Access 2007

Microsoft Access 2007
Getting Started
• Select Blank Database
• Create the database as “Test”
Saving your 

Database
• To save your 
document, simply click 
on the MS 2007 logo in 
the top left‐hand corner 
[        ] and the menu bar 
that you see on your 
right here will drop down 
giving you various 
options, including saving.
Saving your Database cont’d
• When you save a database in MS 
Access 2007, it automatically 
saves with “.accdb” as its 
extension.

• However, you can also save your 
database so that it’s readable by 
earlier versions of MS Access 
(2000, 2002 – 2003) by simply 
going to “Save As” and choosing 
to save the database in a different 
format from a previous version of 
MS Access.
*** A Cautionary Note ***

• There is an inherent danger with working off a floppy disk or a 


USB key with a MS Access database.
• When a file is opened with Access, a bunch of stuff from the file is 
unpacked and is NOT put back until the file is closed.
• Meaning, IF working from a floppy disk or a USB key, DO NOT
eject your disk or USB key until you close MS Access first. 
• In fact, working off floppy disks & USB keys should be avoided all 
together.
• Instead, work off the computer and when you’ve finished your 
work and closed down MS Access, copy your database to a floppy 
disk or USB key.
• And as always, make backups of all your work.
Getting Help in MS Access 2007
• To get help in Access 
2007, look for the help 
icon [      ] in the top 
right‐hand corner of the 
screen and click on it.
• You will be presented 
with a window that 
looks like the one here 
on the right.
Getting Started cont’d
When you first create a  Data Type
new database, you will 
View
Select a type of  The kind of data
start off in the Datasheet  that is stored in this
view for the table
View of a new table field.

Field

Record
Saving  Your Table
Switching to Design View
• To save our table,
– Click on the Office Button         and select Save
– Save the table as “Students”

• Switch into the Design View: 
– Click on the View                                                            
Button and select                                                      
Design View
Data Types
Before we add fields to our table, let’s learn a little bit about the different types of data.

•Text: Short text  (up to 255 characters)


•Memo: Lengthy text   (up to 65,636 characters)
•Number: Numbers can have different sizes that can 
store larger or smaller numbers. Take a look at 
the bottom left of the window screen for the 
tab called “General”. Click on the “Field Size”
option and scroll down to view the different 
number types that you can use. For example:
Integer: ‐32,768 to 32,767 (no decimals)
Decimal: Includes 28 decimal points.
More Data Types
• Date/Time: Date & time values for the years 100 to 
9999

• Currency: Currency values  (includes 4 decimal places)

• Yes/No: A true or false value (i.e., a boolean value)

• AutoNumber: A unique number (sequential or random) 
that MS Access assigns to every new record.

• There are a few others that you can look at yourself.
Adding Fields to the Table in Design View

• Create the following fields:
– Student ID (AutoNumber, increment)
– Name (text) Right-click on
the “Student ID”
– Age (number)
field and select
– Enroll Date (date/time short date*) “Primary Key”
– City (text)

* See the next


slide for how
to do this!
Date / Time Fields
• On the previous slide, we had a 
Date / Time field as the 4th field 
in our table. We want the 
format of that enroll date to be 
a “short date”. 
• To do this…
– Click on “Enroll Date” field
– Now click on “Format” and…
– Select “Short Date” from the list
Primary Keys
• A Primary Key is a value that uniquely identifies each 
record in a table (ex. Student ID).

• In the table that we just created the Primary Key is 
the first field in the table (Originally named “ID”, we 
have since renamed it to “Student ID”).  

• This means that for each new record MS Access will 
automatically create a unique number for the 
Primary Key (in this case, Student ID).
Datasheet View
• Save the table
– Click on the Office Button         
– Select Save

• Switch into the Datasheet view: 
– Click on the View Button
– Select  Datasheet View
• This is the Datasheet view.  It allows you to see the 
data that is currently in the table 
– (in this case none, because we just made the table).
• Create 5 different records for our table.
– Notice how the AutoNumber field fills itself in.
Datasheet View cont’d

• Here is some sample data for the “Students" 
table:

Make sure to save your table once you’ve 
finished entering all the data
Creating a 2nd Table
• Create a new table
– Go to the Create tab
– Select Table
– *Make sure you are in Design View 

• In Design View, add the following fields: Right-click on the


– City (text) “City” field and select
– Country (text) “Primary Key”
– Population (number long integer)
Entering Data to the 2nd Table

• Save this table as “Cities” and enter the Datasheet view.


• Create 5 different records for the 2nd table and save.
Make sure that the cities that appeared on our first table 
(Students) appear on this table as well.
Table Relationships
• A relationship in a database is a logical link between 
two tables.
• For example, in our 1st table, “Students”, we have a 
field called “City” that also appears in our 2nd table, 
“Cities”. 
• This is a common field. 
• It stands to reason, then, that if we know a particular 
student’s hometown city (taken from the “Students”
table), then we should be able to access the 
corresponding information about that city located in 
the “Cities” Table (assuming there’s a record 
representing that particular city).
Table Relationships cont’d
• When viewing a table, go to the Datasheet tab (not 
datasheet view) and select Relationships
• A list of all Tables and Queries pops up.
– Double‐click on “Students” & on “Cities”
– Click “Close”
• This screen displays relationships                              
between the tables .
• Click and hold onto the “City” field in the “Students”
table and then drag it onto the “City” field in the 
“Cities” table.
• A window will pop up on screen that looks like this...
Table Relationships cont’d

Click on “Create” ….

… & remember to SAVE!

Relationship Line

“City” is the Common Field.


Queries
• What’s a Query?
– Queries are questions 
– A Query selects a subset of data from one or more tables 
based on criteria that you give it.  
• Now that we have two linked tables we can create 
some queries to retrieve data from the tables.
• There are 6 kinds of queries: Select, Update, Insert, 
Make‐Table, Crosstab, and Delete.  
– For this class we will only cover Select queries.
• A Select query retrieves data from one or more 
tables, but doesn’t change anything.
Query Example
• Click on the Create tab and select Query Design
• Add the “Students” and “Cities” tables
Query Example cont’d
• Now drag the fields that you want to 
include in your query to the bottom section 
of the screen.

• For our example, add                                            
the “Student ID” field                                                 
and the “Name” field                                                      
from the “Students”
table. 
Query Example cont’d

• Save the query as “Query1”.

• Switch to Datasheet View

• View the results of the query.
Adding A Criteria to our Query

• We will now add a                                               
criteria to the                                                 
“Student ID” field.

• Add a criteria (such as >2 for Student ID)
• Save the query
• Switch to Datasheet View
• Note that only students                                         
who have a Student ID                                           
greater than 2 are displayed.
Query – User Input
• Sometimes you want to be able to change the criteria of a 
query.
– For example, we want to allow the user to only view information for a 
particular student that they specify
– As well, we want to allow the user to specify any particular student by 
typing in his or her name
• To do this we use Square Brackets: [   ]
• Switch back to the Design View and…
– Delete the “>2” criteria for Student ID field
– Add “[Enter Name]” as the criteria for the Name field and SAVE
Query – User Input cont’d

• Switch to Datasheet View
• Enter the name of one of the students                           
and click OK 
• Note that the text inside the square                            
brackets [Enter Name] is the text                                                   
that’s being displayed 
• Also note that only that particular                                                
student and his or her ID # is                                  
being shown.
• This is because the query only returns                          
the information for the particular                              
student that the user specified.
Queries – Linking Tables
• Switch to Design View
• Don’t forget to delete the criteria from the previous example
• Add the “City” and “Population” fields from the “Cities” table to 
the query by dragging them down
• SAVE and view the results by switching to the Datasheet View.
More than       Criteria
• You can have more than one specified criteria in a query!
• To illustrate, switch back to Design View
• Set the criteria for Student ID field to >2 and…
• Set the criteria for the Population field to >=100000
• SAVE and switch to the Datasheet View
More than        Criteria cont’d
NEW
• Compare these 
results with 
query example 
we did where the 
only criteria was 
for student ID  OLD
(>2).

• Notice the 
difference?
as Criteria

• Text can also be used as suitable criteria
• Switch back to Design View
• Delete the two criteria from the previous example and…
• Set the criteria for City field to Like “Toronto”
• SAVE and switch to the Datasheet View
Note how only the information regarding Guelph is displayed
as Criteria cont’d
• We can also use the wildcard character ‘*’ as part of the text
• Switch back to Design View
• Change the criteria for City field from: Like “Toronto”
to: Like “To*”
• SAVE and switch to the Datasheet View
Note how the result is the same! This is because the wildcard 
character ‘*’ substitutes for any and all possible combinations.
Logical Operators used in Criteria
• When we talk about logical operators, we’re 
referring to the operations of:
‘AND’ ‘OR’
• The ‘AND’ operator can be used to create a criteria 
that must satisfy 2 or more conditions.
• For example, we could list all birthdays between 
August 3rd, 1981 & May 25th, 1983 by using the 
following criteria: 
>=8/3/1981 And <=5/25/1983
Logical Operators used in Criteria cont’d

• The ‘OR’ operator can be used to create a criteria 
that must satisfy at least 1 of 2 (or more) conditions.
• For example, we could list all the people named John 
and all people named Christina by using the following 
criteria:
“John” Or “Christina”

• Let’s do an example…
Logical Operators used in Criteria cont’d
• Switch back to Design View 
• Delete the old criteria from the previous example and…
• Set the criteria for Population field to >10,000 And <100,000

• SAVE and
take a look at
the results in
Datasheet
View.
Logical Operators used in Criteria cont’d
• Switch back to Design View 
• Slightly modify the criteria from the previous example by…
• …setting the criteria for Population field to <10,000 Or >100,000

• SAVE and
take a look at
the results in
Datasheet
View.
Query ‐ Sorting
• Switch back to Design View 
• Delete the criteria from the previous example.
• We are now going to sort the output of the query so 
that it displays in ascending order according to Name
• SAVE and take a look at the                                         
results in Datasheet View.

Note that we can sort the                                       
query’s output in either ascending or descending order 
according to any of the fields.
Query – Showing Relevant Fields

• Just because a particular field is included in a 
query, doesn’t mean that you have to display that 
field when the query performs its function.
• For example, we’re going to modify our query so 
that the “Name” field is not being displayed in the 
Datasheet View.
• Note  we want to make sure that even though 
“Name” is not being displayed, we still want the 
query to sort in ascending order according to the 
“Name” field. 
Relevant Fields cont’d
• Switch back to Design View 
• Remove the checkmark for “Show” under the 
“Name” field.
• SAVE and take a
look at the results
in Datasheet
View.
The Importance of Relationships
• Recall The “Students” & “Cities” tables are 
linked through the common field: “City”
• Without having specified this relationship 
between the two tables, we would have gotten 
completely different results for our query.
• For example, instead of showing the specific city 
and population for each of the different students, 
it would have displayed every single city and its 
corresponding population for every single 
student.
Relationship Importance cont’d


Relational Databases

• This brings us to the idea of a relational database.
• A relational database is a type of database that 
stores information in tables that are related to 
each other through common fields.
• This is extremely useful because it allows us to 
organize our data visually in a kind of Concept Map 
that can make our database easier to understand.
• As well, having our information divided up into 
different, but related, tables, lets us view the 
information in any number of different ways.
Introduction to Functions

• Functions can be used in queries.
• For example, we’re going to use the AVG 
function to determine the average 
population of the 5 cities in the “Cities” table.

• Create a new query in Design View.
• Add the “Cities” table.
• Add the “Population” field.
• Now, save the query as “Average Population”
Introduction to Functions cont’d

• Click on the Totals button 
– This can be found on the Design tab
• This will cause a new row called “Total”
to show up in the query design area.
• Click and scroll down to find                                   
the “Avg” function.
• SAVE & switch into the Datasheet View
That’s all for this tutorial!

• As usual, if you have questions come to one 
of the Zoe’s or one of the TA’s office hours 
or post your ‘query’ (hehe, get it? query?) to 
the online conferences.

• Next Access Tutorial: 
– Functions and Forms and Reports (oh my!)

Вам также может понравиться