Upload
trinhanh
View
245
Download
0
Embed Size (px)
Citation preview
John W. Jacobs Technology Center
450 Exton Square Parkway
Exton, PA 19341
610.280.2666
www.ccls.org
Facebook.com/ChesterCountyLibrary
Introduction to
Microsoft Access 2007
1
Introduction to Microsoft Access
What is Microsoft Access?
Access is tool for storing and retrieving data. Unlike Word or Excel, Access does very little on
its own.
Information management tool
Stores data for reference, reporting and analysis
Access is a relational database
What is Data?
A collection of facts
Three types of data that we will be using tonight:
o Text
o Numeric (Number)
o Date/Time
How is Data Organized?
Data is organized into tables
o A table is a collection of information, for example a table of CD’s might include:
CD Name
CD Artist
CD Song Title
o Information in tables can be related for example
Rooms Table (i.e. Living Room, Kitchen, Bedroom)
Inventory Table (i.e. Television, Game Console, Jewelry)
The Television is in the Living Room
o Tables look like an Excel Spreadsheet
How is Data Manipulated?
Forms
Reports
Queries
Let’s Get Started Locate the icon for Access (it looks like a key on a maroon background) and double click. You will see
the following screen:
2
Microsoft has provided several templates to make the use of access easier. For example: Assets,
Contacts, Issues, Events, etc. If your needs are such that you can use the one of the templates, you will
find that they are well done with all of the databases, forms, reports, etc. ready for you to simply enter
data.
Instead, we are going to start with a blank database, so please click on the “Blank Database” Icon at the
top:
3
We will need to enter a name for this database:
Type: HouseholdInventory and click the create button. Notice how the default view looks a lot like an
Excel spreadsheet – rows down the side and columns (although without labels) across the top.
Access opens with the assumption that you want to start with a table, which makes sense since Access
wants to help you manipulate data and without data you have nothing to manipulate.
4
Time for a quick segue into efficiencies in tables and one of the underlying principles of data contained in
a database that of Normalization. Basically normalization is the elimination of redundancy in a table.
Let’s look at a simple example. Picture two people with a few favorite colors.
Person Favorite Color
Jane BROWN
Jane GREEN
Bill BLUE
Bill RED
Bill GREEN
Where is the duplicate information? Jane is repeated twice, Bill three times, and the color Green twice -
although in the sense of normalization GREEN is not subject to normalization since it is tied to the person
rather than the actual color.
Normalizing the information would cause us to create two tables, a table of people and a table of colors.
We would then create a relationship between the tables. For example:
Person Favorite Color
Jane BROWN
GREEN
Bill BLUE
RED
GREEN
The table “Favorite Color” is related to the “Person” table. Now instead of five rows with Jane and Bill
repeating, we have two rows – one for Jane one for Bill – and five rows in the favorite color table. What
are the advantages of doing this? If we change a person’s name (for example, Bill becomes William) we
only have to make the change in one place whereas in the previous example it would have to be changed
twice. Imagine the number of changes if instead of a favorite color Bill was creating a table of all the
books he’s read. Without normalization, if Bill changed his name to William you would have to change
hundreds of records – and what if you made a spelling error?
5
We are going to change the way we create a table, so right click on the tab titled “Table1” and on the drop
down, click “Design View”:
We will have to save the table first, so enter a table name of Assets and click OK.
6
Although not required, every table row should have some kind of unique identifier. Since we cannot
force the elimination of duplicate data (i.e. what if you have two TVs in the bedroom?) Access provides a
way to make each row unique by starting each table with a default column titled ID. This column’s value
will start with zero and Access will automatically increment it each time a new row is added.
So let’s all press the down arrow (you can also use the mouse to position to the next row on this screen)
so that we can define and title the various columns that we want in our database: Here is what we want to
create and the following is the screen shot showing the fields filled in:
Field Name DataType Description
ID AutoNumber
AssetName Text Asset Name
Room Text Room Location
Brand Text Brand Name
SerialNumber Text Serial Number
OriginalCost Number Original Cost
ReplacementCost Number Replacement Cost (optional)
A few things to note, the column labeled Description is optional, but it is important and a good habit to
get into, so please put this information into your table.
7
This is how our screens should appear.
Now let’s save the table by right clicking on the tab “Assets” and select Save. Then right click again on
the tab and select close.
We can actually enter data into the table now. The screen will look a lot like an Excel spreadsheet.
Double click on the Assets Table (if you want to follow along) and enter data. Note that the ID field
automatically incremented by 1.
8
We are going to create one more table and title it “Rooms”. Click on “Create” in the toolbar, click on
Table and change to “Design View”, name the table “Rooms” and add two fields: ID with a datatype of
Autonumber (automatically created for you) and Room with a datatype of Text. It should look like the
following:
Save and close the table and now let’s create a form! From the main screen insure that the table Assets is
highlighted, click on Create and then click the Form Icon:
9
Access has automatically created a form for us with all of the fields from the table displayed on the form.
Notice that every field (or column) in the table has now been put onto a form, a title has been inserted in
front of the field and you are now ready to save the form and enter data. The view you are looking at is
called “Layout View” and will not support data entry. Instead this view is used to make adjustments to
the form to make it usable for data entry. So let’s do some changes.
First thing we are going to do is to delete the ID field from the form. Why? It’s really not needed since
Access will increment the value regardless of whether it is showing up on the form and Access prevents
us from entering data anyway.
This is your first foray into a field called “GUI Design”. GUI is Graphical User Interface. In this case we
made the decision to make the form easier to understand by eliminating a field that was not required and
wouldn’t let us enter data anyway.
10
Notice as well that Access put labels next to the fields that are the same as the column titles. We can
easily change that by double clicking inside the field and changing the text to something that may be a bit
easier to read for example on the ReplacementCost field, we can add the words “(optional)”.
Note that when we change the title that all of the fields realign themselves.
Now let’s save the table by right clicking on the “Assets” tab (the form name) and select save.
And now its time to enter some data. Again, right click on the “Assets” tab and select “Form View”.
11
12
Before we actually enter data let’s look at the form. Note on the bottom of the form the line that begins
“Record”. Immediately next to that label is a number which indicates the number of records in the table
and the record number associated with the data displayed on the form (more on this later). Next to that is
a couple of direction buttons (previous, last, next), a filter selection and a search box. The last two (filter
and search) are inoperable until there is actual data in the table.
So let’s input some data. Use the following information to enter five records into the table:
Asset Room Brand Serial
Number
Purchase
Date
Original
Cost
Replacement
Cost
TV Living
Room
LG 1234ABC 12/25/2012 699.00
Couch Living
Room
Raymour &
Flanigan
09/15/2011 1299.00 1299.00
HTC Phone Den HTC 435AP987TH 08/12/2011 99.00 399.00
TV Master
Bedroom
SONY 436A89B3 06/03/2010 899.00 899.00
iPad Den Apple 765HG12-98 11/13/2012 899.00 899.00
Gateway
Laptop
Den Gateway GW87931Z 04/15/2012 499.00 499.00
When finished our screen should look like the following. Notice at the bottom of the screen we now have
Records 7 of 7. If we press the previous record button all the navigation buttons will now show: first,
previous, next, last and new (indicated by an arrow pointing right with a yellow asterisk). Notice also that
the Search feature will now work. Type TV into the Search box and notice that Access automatically
positions you to the first record that contains TV. Notice also that it didn’t matter what field TV was in.
Try it with Raymour.
13
Now let’s create a report, but first, lets close the form. This can be done by right clicking on the tab
“Assets” and selecting Close or by clicking on the x to the right of the Assets tab (on the far right side of
the form).
When you close the form, Access will ask you if you want to save the form, be sure to answer Yes and
then name the form “Asset DataEntry”
We are going to use the built in wizard to create our report.
Make sure that the table Assets is highlighted then let’s click on Create once again and select Report
Wizard.
14
Once we click on the report wizard Access asks us which fields from the highlighted table we want on the
report. You can actually change the table by clicking the drop down arrow in the field “Tables/Queries”
and select a different table, but for our purposes we will only be using the Assets Table so we are good.
We add fields to our report by highlighting the field in the column Available Fields and clicking on the
button pointing to the right or by double clicking on the field name. If we wanted all the fields we would
simply click on the button with the double arrows.
Below that is a button pointing to the left which will remove a highlighted field on the right and if you
were to click the double arrow pointing to the left, all fields would be removed from the report.
15
For now, let’s pick AssetName, Room, Brand, Serial Number, PurchaseDate and Original Cost and click
Next.
Once we have picked the fields and clicked Next, the wizard wants to know if we would like to group the
data on the report. What this means is that all records with the same grouping (for example by room) will
be listed together, regardless of the order in which they were entered.
Lets group by Room. Highlight the field “Room” and click the arrow pointing right.
16
17
Notice that the field Room is now located above the other table fields. Click Next.
The wizard will now allow us to sort the records. For example alphabetically by Asset Name.
Click on the down arrow in the highlighted box and select AssetName and click Next.
18
The wizard now wants us to select a layout and an orientation. If you click each of the radio buttons the
wizard will show you how the report will look if printed. Encourage the students to do this.
If you feel that there is too much information to fit on a portrait page (8.5 inches wide by 11 inches tall)
you can select landscape mode (11 inches wide by 8.5 inches tall). You can also stipulate that the field
widths be adjusted so that all the fields fit on a single page. Sometimes this will result in truncation of
data so be careful.
We can now pick a style. Again, clicking on each style will show you the fonts, the font sizes and the
colors used for each style. For now let’s leave the style alone and click next.
19
20
Now the wizard wants us to title the report so let’s call this report “Household Inventory” and click
Finish.
Hmmm doesn’t look so good does it? The purchase date field is showing ##/##/#### and the cost field
isn’t showing anything but a #. When Access prints a # sign, that indicates that the contents of the field
exceeds the size of the place holder on the report. Let’s fix that by making some room on the right side of
the report so that we can expand the fields.
21
Right click on the “Household Inventory” tab and select Layout View.
We can now begin to adjust the report. Let’s start with the Asset Name by clicking on the title
“AssetName”. If we move the mouse over the dotted lines below the heading, the pointer will turn into a
double pointed arrow. This may take some finessing since you have to be right over the dotted line on the
left so we’ll pause until everyone has the pointer on the dotted line.
Now, with the mouse showing a double pointed arrow, hold the left mouse button down and move the
mouse to the left, adjusting the size of the field.
22
While you are moving the mouse you should see something like the following:
Now repeat the process for the Brand field and the SerialNumber field.
23
That’s starting to look better, we have some room to play with on the left side of the report. Now we are
going to expand the size of the PurchaseDate and the OriginalCost fields. Now instead of moving the
cursor to the left, move the cursor to the right. Repeat for the Original cost field. Wait a second, look at
the first field on the report. Shouldn’t that be bigger as well? Lets increase the size of it too.
That looks great!
24
Now let’s switch back to Report View.
We now have a finalized report which is quite readable and can be printed to be sent to your insurance
agent or kept in a strong box.
But we aren’t done yet! Remember that table we created called Rooms? Let’s explore how that can be
used to make this database truly a relational database.
We are not going to create an input form for this table since it wll only have a few records in it. We’ll use
the default input method provided by Access.
25
Use the following to populate the Rooms Table:
Room
Living Room
Den
Master Bedroom
Dining Room
Bedroom 1
Bedroom 2
When complete you should have the following:
26
Now close the table. Note the numbers along the left side of the table do not have to be the same value as
mine. Close the data input screen.
Double click on the Assets Table to open it and switch to Design View. Add a new field titled RoomID
with a datatype of Number.
27
When you’ve finished the table design should look like the following.
Now save the changes (Left mouse click on the Rooms tab and select close – then answer YES to save the
changes).
Now we will create a relationship between the Assets Table and the Rooms Table. Click on Database
Tools and select Relationships:
28
Highlight the Assets Table (single left mouse click) and click Add. Do the same for the Rooms Table and
click Close.
This is what your screen should look like now.
29
Using the mouse, move the cursor over the bottom of the Assets box until it changes to a double arrow,
then hold the left mouse button down and drag the size of the box to reveal the other fields in the table.
Move your mouse pointer until it rests on top of the field “RoomID” in the Assets Table. Hold the left
mouse button down and move the mouse pointer until it resides on the field ID in the Rooms Table then
let the mouse button up. Click the “Create” button.
30
We now have a relationship between the Assets Table and the Rooms Table. This will allow us to build a
query that retrieves information from both tables with one query. Information that will always be related.
In other words if we put the Room number into the Assets Table, Access will retrieve the room name
from the Rooms Table based upon the room number in the Assets Table.
Now let’s create a query to demonstrate this principle. A query is simply a way of retreiving data from a
table or tables. We start by clicking on Create then click on Query Wizard.
31
We are going to start our query with the Asset Table since it contains the information that we want to see.
We want all the fields from the Assets Table except the Room field and the RoomID Field we created
previously.
Next we will select the Rooms Table and the Room Field:
32
Access then wants to know if we want a summary of the data or each record that will be retrieved. We
want all the detail records, so click Next:
The default name assigned by Access is fine so let’s click on Finish.
33
Uh oh … we didn’t retrieve any records! Although we have built a relationship between the Assets Table
and the Rooms Table, we did not put any values in the Asset Tables that completed the relationship.
Let’s open the Asset Table by double clicking on the Asset table name and using the following data, let’s
put some additional information in the table. In order to see the proper results of the query we just built
we have to add tell Access exactly how each record in the Assets Table is related to a record in the Rooms
Table. The relationship that we built previously simply tells how the two tables are related. Now we have
to tell Access how each record is related. The column we will be populating is the RoomID field from the
table below:
Asset Room Brand Serial
Number
Purchase
Date
Original
Cost
Replacement
Cost
RoomID
TV Living
Room
LG 1234ABC 12/25/2012 699.00 1
Couch Living
Room
Raymour
& Flanigan
09/15/2011 1299.00 1299.00 1
HTC Phone Den HTC 435AP987T
H
08/12/2011 99.00 399.00 3
TV Master
Bedroom
SONY 436A89B3 06/03/2010 899.00 899.00 5
iPad Den Apple 765HG12-98 11/13/2012 899.00 899.00 3
Gateway
Laptop
Den Gateway GW87931Z 04/15/2012 499.00 499.00 3
One you have completed populating the fields, the table should look like this:
34
Now click on the Assets Query Tab, but notice that we still have no records. Although we have made
changes, Access is a bit slow on realizing how that affects views that are already open.
Click on the Refresh button on the Ribbon:
35
That’s better! We now have the information populated from the Rooms Table. Let’s do a quick exercise
to show you exactly how a relationship works.
Let’s open the Rooms Table and change record number 3 from Den to Office:
36
Now let’s go back to the Assets Query and click Refresh. Notice that every record that used to have
“Den” in the Room name is now “Office”. This is one of the most important features of Access. When
you build a relational database, you only have to make a change to one record but the change is populated
throughout the database.
Now let’s build a form using the new query. Close the Assets Query tab, the Assets Table tab, and the
Rooms Table Tab. Then highlight the Assets Table on the left, click Create and Click Form.
37
Access creates a default form using all of the fields from the table (just like previously) but we don’t want
all the fields so let’s switch to Design View.
Click anywhere in the space below the form to unselect the selected items (Access selects all the fields by
default).
38
39
Now let’s delete the ID field, the Room Field, and the RoomID field by simply clicking anywhere inside
each field and pressing the Delete key. Next we are going to add a list box to the form and we are going
to use the Rooms Table to populate the list box.
A list box is nothing more than a control on a form that contains a list of values from which you want the
user to choose. In this case we want all the records from the Rooms Table to fill the list box.
Click on the list box control on the Ribbon and position your cursor immediately below the Replacement
Cost field and click again. The list box form will be created and the wizard will guide you through how
to populate the list.
40
We want to draw data from a table, but we could have used the Wizard to simply type in the values from
which we wanted the user to choose. This is helpful when the values rarely change. Click Next.
We next select the table used to populate the list box:
41
Select All the available fields (use the >> button to put every field in the Selected Fields Box).
We are not going to sort the fields, but we could select a field from the Rooms Table to sort, for example
we could sort alphabetically by room name. Click Next.
42
The next screen shows how the values from the records will be displayed in the list box control, notice the
“Hide key column (recommended)” check box is checked. This is so that the record id box will not be
displayed in the list box. However, the record id from the Rooms Table will be stored in the Asset table
automatically creating the record level relationship between the Assets Table and the Rooms Table.
However, Access doesn’t know where to store the record id so we have to tell it. So select “Store that
value in this field” and click on RoomID:
43
Let’s name the label “Room” to make it easier and then click Finish.
The default size of the list box control may be a bit small, so let’s increase the width just a bit. Click
inside the Room box so that the sizing bars are shown. Grab the right side bar and drag it to the right just
a bit:
44
Now let’s switch to Form View to see the results. Using our navigating buttons across the bottom of the
form we can see that as we cycle through the records, the highlighted room name changes. Now let’s add
a new record.
We can add a new record by clicking on the add record button at the bottom of the form or by navigating
to the last record and trying to navigate past the last record.
45
Let’s put in our dining room outfit and be sure to select “Dining Room” in the Room Field. When you’ve
finished, press tab or Enter to navigate to another blank record. Using the navigation keys go back one
record and review your entry.
Just to reinforce how relationships work, double click on the Rooms Table and change the description of
Dining Room to Dining Area.
46
Open the Assets1 form (double click the form name) and notice that Dining Room has changed to Dining
Area. Navigate to the last record and notice the change there as well.
One final thing to do is to modify our report so that the changes we made for relationships is reflected on
the form. Double click the Household Inventory Report and change to Design View.
47
Now right click anywhere below the report footer line and select Properties.
On the right hand side of the screen click the down arrow next to Record Source and select “Assets
Query”.
48
Now change back to Report View:
And now our report will reflect any changes we make in the Rooms Table.