MS Access Tutorial
MS Access Tutorial
MS Access Tutorial
A Supplement to
Database Systems: Design, Implementation, and Management
by
Peter Rob and Carlos Coronel
MS-Access-Tutorial
Table of Contents
Section
1.1
1.2
1.2.1
1.3
1.3.1
1.4
1.5
2.1
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
Title
Page
Introduction
Create the Database
Creating the Table Structures
Data Entry
Importing Components from Other
MS Access Databases
Editing the Imported Tables
Tracing a Transaction
Creating Relationships Among Tables
Queries
Editing the Query Output
Parameter Queries
Multiple Table Queries
Querying a Query
Update Queries
Using Update Queries to Manage Transactions
2.1.6
2.1.7
3.1
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.1.6
4.1
4.1.1
5.1
5.1.1
5.1.2
5.1.3
Conclusion
3
3
7
15
18
22
27
30
34
42
47
52
53
55
62
66
71
77
80
93
96
103
106
108
118
124
131
133
134
139
147
MS-Access-Tutorial
MS Access Tutorial
Introduction
After a database has been designed properly it must be implemented and the applications that make the
database useful to the end users must be developed. Microsoft Access is a great tool for prototyping a
databases implementation and its applications. Therefore, we will show you how to
Create the database.
Create and modify tables.
Enter data into the tables.
Import tables.
Define the relationship(s) between the tables.
Create queries.
Create forms.
Create reports.
Link application components with macros.
This tutorial assumes that you know how to perform basic operations in the Microsoft Windows
environment. Therefore, you should know what a folder is, how to maximize or minimize a folder, how
to create a folder, how to select a file, how you maximize and minimize windows, what clicking and
double-clicking indicate, how you create a folder, how you drag, how to use drag and drop, how you
save a file, and so on.
MS-Access-Tutorial
NOTE
If necessary, review the texts Chapters 4 through 6 to ensure that you have a sound basis for database
design work. You can study Appendix A, Designing Databases with Visio Professional: A Tutorial,
if you want to create the database design shown in Figure 1.
Given the database design shown in Figure 1, you are ready to implement it in Microsoft Access. The
first step is to start the MS Access DBMS software, which is part of the Microsoft Office Professional
suite. Use the same routine you use in all Microsoft office components. In other words, to start
Microsoft Access, follow the sequence Start/All Programs/Microsoft Office/Microsoft Access. The
sequence is the same for all MS Office versions. However, you will see slight variations in the labeling
of the MS Office components. For example, Figure 2 shows Office 2003 labels. (And, naturally, your
screen is likely to differ -- in detail -- from the one you see in Figure 2. After all, you may be running a
different version of Windows and you may have a different version of the Windows Office suite.
Nevertheless, the basics remain the same.)
MS-Access-Tutorial
After you have completed the just-described sequence, you will see the screen in Figure 3. (Only a small
portion of the screen is shown.)
MS-Access-Tutorial
After you select the New option shown in Figure 3 the screen will change to show Figure 4. Place the
cursor over the Blank database option. (Note that the cursor changes to the shape of a hand.)
When you select the Blank database option shown in Figure 4, Figure 5 will appear. Note that the
default folder is My Documents and that the default database name is db1.
MS-Access-Tutorial
Select the folder in which you want to store the database. In the example you see in Figure 6, the folder
is named MS-Access-Tutorial and the database name is SaleCo.
Note
The SaleCo database is also found on the Course Technology website for the text. However, that
database includes all the components you will learn about in this tutorial. If you want to copy the
SaleCo database from the website, make sure you place it in a different folder to avoid over-writing
the database components that you will create in this tutorial.
MS-Access-Tutorial
After selecting the folder and database name as shown in Figure 6, click on the Create button to
generate the SaleCo database you see in Figure 7.
MS-Access-Tutorial
You are now ready to type in the first attribute name. (See Figure 1 again to see what attributes are
included in the CUSTOMER table.) Figure 9 shows the entry of the CUST_CODE attribute.
MS-Access-Tutorial
The CUST_CODE values will all consist of a five-character text string, so the Field Properties box
default Field Size of 50 should be reset to 5. (Just mark the 50 and type in the value 5 to get the job
done.)
We suggest that you save your work frequently, so go ahead and save the CUSTOMER table structure.
You will be prompted to enter a table name, so type the CUSTOMER name in the Save As dialog box,
as shown in Figure 10.
10
MS-Access-Tutorial
However, note that your save routine pops up the message box shown in Figure 11.
If you select the Yes button in the PK reminder shown in Figure 11, MS Access will automatically
create a PK attribute for you and that is not what you want. (Remember, you want the CUST_CODE
to be your PK.) If you select Cancel, you will be returned to the table design format without first saving
the table. Since you want to save the table, select No to ensure that the table will be saved, albeit without
a PK. (We will show you how to set the PK later.) After you click on the No button, MS Access will
return you to the screen you first saw in Figure 7, but the new CUSTOMER table will be listed as shown
in Figure 12.
To continue working on the CUSTOMER table, click on the CUSTOMER table name and then select
the Design option to return to the table field entry screen. If you take a look at Figure 13, youll see that
we have entered a CUST_CODE description and that we have decreased the Field Size to 5.
11
MS-Access-Tutorial
As you examine Figure 13, note that the Field Properties box entry for the CUST_CODE indicate that
the Indexed default value is set to Yes (Duplicates OK). Clearly, that is not appropriate if the
CUST_CODE is to be the PK, so lets set the PK. That job is done in two steps:
1. Click in the narrow column just to the left of the CUST_CODE attribute in the Field Name
column. Note that this action inverts the screen for the first field name row. (Figure 14 shows
that the background is black and the lettering is white.)
2. Click on the key symbol on the button bar along the top of the screen to set the PK.
12
MS-Access-Tutorial
The completion of step 2 generates the screen you see in Figure 15. Note that the narrow column to the
left of the Field Name column now shows the key symbol to indicate that the CUST_CODE attribute is
now the PK. (We suggest that you save the table again.)
If you look at the CUST_CODE Field Properties box again, you will see check Figure 16 that the
CUST_CODE Indexed property has been reset to Yes (No Duplicates). Note that this new property
enforces entity integrity, because no duplicate values will be permitted.
Using the procedures you have just learned, go ahead and create the remaining attributes for the SaleCo
databases CUSTOMER table. (Use the ERD in Figure 1 as your guide.) Note that the
CUST_BALANCE is a currency value in Figure 17. Make sure that you use a Text format for all of the
remaining attributes and that you limit the field lengths for CUST_LNAME, CUST_FNAME,
CUST_INITIAL, CUST_AREACODE, and CUST_PHONE to 20, 20, 1, 3, and 8, respectively. (Phone
numbers will be entered with the format 999-9999.) Given the self-describing field names, there is little
need to continue the entry of more detailed descriptions. For example, the CUST_LNAME is clearly a
customers last name, so no further description is necessary.
13
MS-Access-Tutorial
Make sure that you save the table again. You are now ready to enter some data. Incidentally, note that
the CUST_BALANCE default value in Figure 17 is 0. Therefore, the CUST_BALANCE (currency)
value will be shown as 0.00 until you change it.)
14
MS-Access-Tutorial
NOTE
Before you enter any data, you should remember that the parent table entries always precede the
child data entries. In other words, in a 1:M relationship, the 1 sides data entry precedes that of
the M side data entry. For example, if you try to make a data entry into the INVOICE table for a
customer who does not yet exist, you will commit a data integrity violation. You will learn how to
create relationships between tables in Section 1.3 and you will then discover that you will have the
option to have MS Access enforce referential integrity.
15
MS-Access-Tutorial
Suppose you now try to enter the second record. After accepting the 0.00 CUST_BALANCE value, you
tap the Enter key to move the cursor to the second record. Now type in the same 10010 CUST_CODE
value you used for the first record ... and then try to close the CUSTOMER table. Your reward will be
the error message shown in Figure 21, because you violated entity integrity requirements.
16
MS-Access-Tutorial
Click OK to acknowledge the error message. This action will generate the result you see in Figure 22.
(As you can tell, the MS Access window selection is restore down to ensure that all the components
you see in Figure 22 can be shown. If you had selected the maximize window option, you would be
limited to seeing the table and the dialog box.)
If you click on the Yes button shown in Figure 22, Access will return you to the table option screen you
saw in Figure 18. In this case, the second record will not be saved and the table simple retains the first
record as is. If you click on the No button, the cursor returns to the data entry screen. This action lets
you make the necessary correction by typing in a CUST_CODE value other than 10010.
The remaining records are handled just as the first record was. When you are done, the CUSTOMER
table contents should match Figure 23.
17
MS-Access-Tutorial
You are now ready to create the remaining tables (INVOICE, LINE, PRODUCT, and VENDOR) and
their contents. However, rather than wasting your time on such repetitive tasks, we will show you how
to import these items from a database you already have. That database, named Ch07_SaleCo, is used in
the texts Chapter 7, Advanced SQL. This database is available online, so go ahead and download it
now into a folder of your choice. You will learn how to import database components from the
Ch07_SaleCo database to your SaleCo database in the next section 1.2.2.
Next, select the data source. In this example, the Ch07_SaleCo database stored in the student folder will
be used, so move to the folder that contains this database and select the Ch07_SaleCo database as
shown in Figure 25.
18
MS-Access-Tutorial
Now select what objects you want to import and in what format you want them imported. Figure 26
shows that Tables are to be imported. Note that the Options >> have been selected to give you a chance
to make the selections shown in Figure 27.
19
MS-Access-Tutorial
20
MS-Access-Tutorial
As you examine Figure 27, note the following selections:
The INVOICE, LINE, PRODUCT and VENDOR tables have been marked.
The Relationships option has been de-selected no checkmark is shown in the box. (If the box
contains a checkmark, click on it to remove the checkmark. The reason for not including the
relationships is that you should learn how to create such relationships later.)
The Definition and Data option has been selected to ensure that the table structure and all the
data contained in each selected table will be imported.
After you have made the selections shown in Figure 27, click OK to import the tables. The results of
this action are shown in Figure 28.
21
MS-Access-Tutorial
Make the changes illustrated in Figure 30. Note the addition of attributes and their field types. (All
changes in the table structures are made via the Design View.)
22
MS-Access-Tutorial
When you open the INVOICE table to see its contents, you will see that the new attributes are there, but
they do not (yet) contain values. (See Figure 31.)
If you wonder why the new attributes in Figure 31s INVOICE table have $0.00 default values, open the
INVOICE table in its design format and note the selection of the currency format and default value in
the Field Properties box shown in Figure 32.
23
MS-Access-Tutorial
While you have the INVOICE table open, you can change the spacing between the attributes. When you
put the cursor on a boundary between attributes, note that the cursor changes to a two-side arrow as
shown in Figure 33. You can drag the boundary to increase or decrease the column display width, or you
can double-click on any column boundary to change the column display width to whatever size is
required to show the column values.
You can also change the presentation font by selecting the Format/Font option shown in Figure 34.
24
MS-Access-Tutorial
After making the Format/Font selection shown in Figure 34, note the selection of the font, style, and
size in Figure 35. Click OK when you are done with the selection process.
Go ahead and experiment with different formatting options. A good rule to follow is this: If you dont
know what something is or what it does, right-click on it.
Now go ahead and edit the table structures of the remaining tables. Use the database tables shown in
Figure 36 as your guide. Remember, you imported most of the tables from the Ch07_SaleCo database,
so the table values match those in that database at this point. However, while the values match, a careful
examination of Figure 36 shows that we have already made a few changes in the SaleCo database.
25
MS-Access-Tutorial
For example, if you look at the LINE table in Figure 36, note that:
The LINE table now has an additional currency field, LINE_AMOUNT, which is the product of
the LINE_UNITS and LINE_PRICE. (The third record in the LINE table shows that the
LINE_AMOUNT = 2 x $4.99 = $9.98.)
The LINE_AMOUNT data type and field property were both set to currency, thus causing the
dollar signs to precede the amount. You can change all the other fields that store dollar values to
a currency data type at any time.
There is an entry error in the first record of the LINE table the LINE_PRICE is $14.99 and the
LINE_UNITS value is 1, so the LINE_AMOUNT should be 1 x 14.99 = $14.99. (Go ahead and
open the LINE table in its datasheet view and make the correction.)
26
MS-Access-Tutorial
As you can tell by looking at Figure 37, the following activities take place:
1. The customer 10014 (Myron Orlando) made the purchase(s) that produced invoice number 1001.
2. Invoice number 1001 records two line items, for a purchase of one $14.99 saw blade and one
$9.95 claw hammer. Lets assume for simplicitys sake that discounts are not offered unless the
purchase is made by a commercial customer and none of the customers in the CUSTOMER
table are commercial purchasers. Therefore, type the PRODUCT tables PROD_PRICE values
into the LINE table as LINE_PRICE values.
3. The total invoice amount is $14.99 + $ 9.95 = $ 24.94.
4. Assuming a tax percentage of 8%, the tax is $24.84 x 0.08 = $1.9952, rounded to $2.00.
5. The invoice total is thus $24.94 + $2.00 = $26.94.
6. Lets assume that the customer pays $20.00, leaving a $6.94 balance.
27
MS-Access-Tutorial
7. Customer 10014 now has a customer balance of $0.00 + $6.94 = $6.94.
8. The product table must be updated, too. The PROD_QOH values for each of the two products
must be decreased by one each to reflect the sale activity.
The completed transaction is shown in Figure 38. (The table limits were dragged to ensure that all the
tables would fit in the same window in Figure 38.)
NOTE
Remember that the product price is copied to the LINE table to maintain the historical accuracy of the
transaction. It is likely that, over time, the product prices will change but the original product prices
will be maintained in the LINE table to reflect the product price that was correct at the time of the
transaction. If necessary, review the texts Chapter 3, The Relational Database Model, Section 3.7,
Data Redundancy Revisited.
Go ahead and complete the remaining transactions to practice the just illustrated tracing process. Note
that the data entry is awkward, because you have to actually enter the product price in the LINE table,
multiply the product price by the number of items (LINE_UNITS) to get the line total values. After that
28
MS-Access-Tutorial
job is done, youll have to calculate the subtotals, the sales tax, and the total in the INVOICE table, and
then update the inventory -- PROD_QOH in the PRODUCT table -- and the customer balance
CUST_BALANCE in the CUSTOMER table. When you are done, your completed tables should contain
the values shown in Figure 39.
If you have recorded all the transactions shown in Figure 39 manually, you realize that such time
consuming and exacting tasks are not a good idea in the real world. You will learn in Section 2.1 that
MS Access includes a query type known as an Update Query that, as its name suggests, can make the
necessary transaction updates. In Section 5.1 you will learn that macros can be used to automate the
transaction update process.
29
MS-Access-Tutorial
After you click on the relationships button shown in Figure 40, a blank relationships screen is shown.
(That is, the relationships screen will be blank if you have not yet established relationships or you have
not imported the relationships with the tables when you learned how to import tables.) Right-click
anywhere on the blank relationships screen to pop up the options Show Table , Show All, Save
Layout -- you see in Figure 41.
30
MS-Access-Tutorial
Click on the Show Table option shown in Figure 40 to generate the Show Table window you see in
Figure 42. Now select the two tables as shown in Figure 42 and the click on the Add button. (You can
also double-click on each table to move it from the Show Table window to the relationships screen.)
This action will generate the screen shown in Figure 43.
To create the relationship between the CUSTOMER and INVOICE tables, select the CUST_CODE in
the CUSTOMER table and drag it to the CUST_CODE in the INVOICE table, and then drop it on the
CUST_CODE in the INVOICE table. This action will produce the Edit Relationships dialog box you
see in Figure 44.
31
MS-Access-Tutorial
Next, click on the Enforce Referential Integrity option you see in Figure 44s Edit Relationships
window to place a checkmark in the square shown on the left side of that option and then click on the
Create button to create the relationship. This action will produce the results shown in Figure 45. Note
that the 1 side of the relationship is marked by the boldfaced 1, while the many (M) side of the
relationship is marked by the infinity symbol .
32
MS-Access-Tutorial
Note
Always drag and drop from the 1 side to the M side in a one-to-many (1:M) relationship. If you are
creating a relationship between tables in a 1:1 relationship, drag from the parent entity to the
dependent entity.
Now go ahead and create the relationships between all the tables. When you are done, your results will
resemble Figure 46. (We have dragged and sized the tables to enhance the presentation.)
33
MS-Access-Tutorial
The cleanup is accomplished by selecting the sequence shown in Figure 48 Tools/Database
Utilities/Compact and Repair Database
When the procedure is completed, look how much smaller the SaleCo database has become. Figure 49
shows that the SaleCo database now uses only 260 KB.
2.1 Queries
Queries are used to extract data from the database and to help transform data into information. To create
a query, follow the procedure shown in Figure 50. That is, click on the Queries option in the Objects
column and then click on the New option.
34
MS-Access-Tutorial
Clicking on the New option shown in Figure 50 will produce the New Query dialog box in Figure 51.
Select the Design View option from the list and then click OK to generate the screen you see in Figure
52. (Many of the wizards are useful, but you are better served by using the Design View option if you
want to learn how to create and manipulate queries. All the queries in this tutorial will be created with
the help of the MS Access Graphical User Interface, or GUI.)
35
MS-Access-Tutorial
Figure 52 shows three QBE (query by example) window components.
1. The top blank -- portion of the window is the QBE windows data source display, which may
be a table or another query.
2. The bottom lined -- portion shows the available options in the QBE grid. The grid represents
the data manipulation portion of the QBE window. As its name suggest, the Field: option lets
you place a tables or querys field on the line. The Table: shows the fields origin. (Clicking
on the Queries tab will show the list of queries that are available as data sources. You can query
a query.) The Sort: option lets you sort selected field values in either ascending or descending
order. The Show: option lets you select whether or not to display a selected field value set. (Note
that the box on the Show: line is not marked at this point, indicating that a selected fields values
will not be shown.) The Criteria: option lets you select various display options and constraints.
And the Or: option lets you modify the Criteria: constraint selection. (For example, you might
place a restriction of the query output for a CUSTOMER table to show only customers named
Smith or to also show customers whose current balance is over $200.)
3. The Show Table dialog box lets you select tables or queries to be placed as data sources in the
first portion of the screen. (If the Show Table dialog box is not shown, right-click on the blank
portion of the screen.)
To place the CUSTOMER table on the data source portion of the screen, either select the CUSTOMER
as shown in Figure 52 and then click on the Add button or double-click on the CUSTOMER table.
When you have placed all the tables you need on the top (data source) portion of the screen, close the
Show Table dialog box by clicking on its Close button. (See Figure 53.) In this example, only the
CUSTOMER table has been selected as the data source.
36
MS-Access-Tutorial
After you have selected the data source the CUSTOMER table and placed it on the data source
portion of the QBE screen, size the QBE windows components by dragging their limits. Figure 54
shows that the cursor changes when you place it on the boundary between the data source component
that now contains the CUSTOMER table and the data manipulation grid component. When the cursor
changes its shape to the two-sided arrow seen here, you can drag the boundary up or down.
After enlarging the data component portion of the QBE window, drag the CUSTOMER limits to show
all the fields and their names. Then select the CUSTOMER tables fields and drag and drop them in the
Field: spaces as shown in Figure 55. You can drag and drop the individual fields from the CUSTOMER
table to the field spaces in the data manipulation section or you can select multiple fields and drop them
on a single field space. (Access will automatically space them across the field spaces.) Note that the
field origin is automatically displayed in the Table: portion of the grid.
37
MS-Access-Tutorial
After you have selected the fields you want to use in the query, you can choose whether or not to sort the
field values by clicking on the grids Sort: option. Clicking on this option yields a drop-down list of
option you see in Figure 56. Click on the option you want to use to move the option to the grid. (As you
can tell, the CUST_LNAME, CUST_FNAME, and CUST_INITIAL show the selection of the ascending
option.
38
MS-Access-Tutorial
If you want to see the effect of the query actions you have taken thus far, change the View from the
current Design View to the Datasheet View, as shown in Figure 57.
39
MS-Access-Tutorial
The selected Datasheet View yields the query output shown in Figure 58.
Incidentally, you can easily control the query display characteristics through the Format option you see
in the button bar. For example, you can change the font size from the (default) 10 value to 8, you can
select the font type, and so on. (Try clicking on the Format option to see what you can do with the
query outputs display.)
Before you do any additional work on the query, save it. Figure 59 shows the selection of the File/Save
As option.
The selection of the Save As option shown in Figure 59 will produce the Save As dialog box you see
in Figure 60. The dialog box shows a default query name, in this case, Query1.
40
MS-Access-Tutorial
Always use a query name that is self-documenting. In this example, the query will be used as the basis
for a phone list. Therefore, PhoneList is an appropriate name. However, you want to also show that this
object is a query, so use the prefix qry to indicate that fact. Therefore, the appropriate name will be
qryPhoneList. And you see that query name used in Figure 61. (You will discover that this selfdocumenting naming convention is very desirable, because it enables you to easily keep track of
multiple components in an application set. For example, you will learn how to create forms in Section 3
and if you see two objects, frmPhoneList and qryPhoneList, you will know which object you are
looking at, and it will be easy to see that the data source for the form is the query that has the same
name. Using naming conventions that are not self-documenting is not a sign of professionalism
sowing confusion is not an ideal worth pursuing.)
41
MS-Access-Tutorial
After you type the query name as shown in Figure 61, click OK to save the query. Note that the saved
query now shows up in the Queries list shown in Figure 62.
After saving the query, you can take a quick look at its output by selecting the query Open option shown
in Figure 63. (You can also open the query by double-clicking on the query name. Go ahead and do so,
then close the query.)
42
MS-Access-Tutorial
Because the default query output uses the field headers such as CUST_LNAME used by the query
data source table, you might want to make the presentation more finished looking by changing the
query field headers. You can get that job done while you are in the query design mode. Note the
procedure illustrated in Figure 65. That is, right-click on a selected field name grid cell to see the list of
available options, and then click on the Properties option to generate the Field Properties dialog box
you see in Figure 66.
43
MS-Access-Tutorial
44
MS-Access-Tutorial
You can change the field header by selecting the Caption option in the Field Properties dialog box you
see in Figure 66, then type in the field header you want to use. In this case, the field header will be Last
Name. Changing the query header does not affect the attribute name (CUST_LNAME) in the query data
source the CUSTOMER table.
Repeat the editing for the remaining query fields and then open the query in its Datasheet View to see
the editing results shown in Figure 67.
45
MS-Access-Tutorial
If you are satisfied with the results, remember to save the query again. (If you forget to save, Access will
remind you as shown in Figure 68.)
46
MS-Access-Tutorial
If you want to limit the query phone list output to customers whose last name is Smith, you can simply
type Smith in the CUST_LNAME criteria grid space marked by the red dot in Figure 69.
Unfortunately, that procedure means that the query must be changed each time a different last name
limitation is required. You will have a much more flexible query if you let the end user specify the last
name restriction through a dialog box. Such a dialog box is created automatically if you type
Like * & [Enter customer last name ] & *
in the CUST_LNAME criteria grid space. (Review Chapter 7, Introduction to Structured Query
Language (SQL), Section 7.4.4, to review the LIKE operator and wildcard characters such as *.)
47
MS-Access-Tutorial
Note
Although you can type the simple criteria restriction directly into the grid, you should become used to
the Expression Builder. This tool is especially useful when you later try to enter more complex
criteria or even simple criteria with multiple components. In fact, you will discover the Section 5,
Macros, that you will have a difficult time typing the sometimes long character strings without
making errors it will be a lot easier to select items from a list than to do the typing. Therefore, we
will use the expression builder in most examples.
If you click on the Build option shown in Figure 69, you will see the Expression Builder dialog box
shown in Figure 70. You can either type the entire expression or you can type Like , click on the *,
type , click on the & and so on. (You do not, of course, type the | symbol you see at the end of the
expression line that just the shape of the cursor in the Expression Builder.) Although you can type
the * and & symbols easily enough, go ahead and practice using the expression builders features while
the expressions are very simple. Familiarity breeds, well, competence!
48
MS-Access-Tutorial
When you have completed the expression shown in Figure 70, click OK to close the Expression
Builder and to transfer the expression to the QBE grid as shown in Figure 71. (If you want to see the
entire expression in the grid space, drag the grid space limit to widen it, as was done in Figure 71.)
Next, open the query in its datasheet view. The expression you see in Figure 71 will trigger the input
request you see in Figure 72. Type the last name Smith to generate the output shown in Figure 73.
Incidentally, the parameter search is not case-sensitive. Therefore, it goes not matter whether you type
SMITH, smith, Smith, or any other combination of lower- and upper-case letters. Also, keep in mind
that the use of the * wildcard character in combination with Like will yield these results:
Input
Output
None. (You just tap the Enter key, instead of All records.
typing a character and then tapping the Enter key.)
The letter s.
All records corresponding to a customer whose
last name includes the letter s. For example,
Ramas, Williams, Olowski, Farriss, and Smith
would all be included.
The letters br.
All records corresponding to a customer whose
last name includes the letters br. For example,
customer OBrian and Brown would be
included.
49
MS-Access-Tutorial
Using the Save As option to save a file, save the qryPhoneList query you have just modified as
qryPhoneListForSelectedLastName. (See Figure 74. Naturally, if you have maximized the screen, you
will only see the Save As dialog box.)
50
MS-Access-Tutorial
You can use the same technique to limit output by any selected criteria for any field. For example,
Figure 75 shows a query that limits its output by VEND_CODE values that are null. (In short, the output
will show all products that do not have a known vendor.) The output is shown in Figure 76.
51
MS-Access-Tutorial
Save this query as qryCustomerInvoices and then open this query to see its output. (See Figure 77a. To
save space, only the first three records are shown.)
52
MS-Access-Tutorial
Figure 79 shows the selected fields. It also shows that, after the fields have been dragged and dropped to
their Field: spaces. Finally, it shows the selection of the Totals button at the top of the screen. (The
button, circled in red, shows the summation symbol.)
53
MS-Access-Tutorial
Figure 79 also shows the remaining actions required to complete the query design:
Click on the Totals button to insert the Group By entry into all the Total: spaces.
Click on the INV_TOTAL field space to produce the drop-down list.
Select the Sum option from the list
Now check the completed query in its Datasheet View to generate the results shown in Figure 81. Note
that the $479.52 sum for customer Leona Dunne is correct. (Customer Leona Dunne customer number
is 10011. If you take a look at the INVOICE table contents displayed in Figure 80A, you will see that
customer 10011 has invoice totals of $10.78, $37.66, and $411.08. The sum of these three invoice totals
is $479.52.)
54
MS-Access-Tutorial
55
MS-Access-Tutorial
Next, drag and drop the PROD_PRICE and PROD_LATE_UPDATE to the field locations as shown in
Figure 82. There will be two updates: The PROD_PRICE will be increased by 10% for a selected
vendor and the current date of the update will be recorded. As you can tell by looking at Figure 82, the
Expression Builder was selected to produce the price change. (Although these updates are simple
enough to type into the Update To: space directly, use the expression builder on all of the updates to
make sure that the expression builder becomes a thoroughly familiar tool. (You will also have to update
the table to enter the current date into the PROD_LATE_UPDATE field and to create a parameter entry
for the selected vendor.)
As you examine the PROD_PRICE update statement in Figure 82, note that the asterisk indicates a
multiplication. You can either type the asterisk or you can click on the asterisk button shown in Figure
82. Type the value 1.10 to indicate the 10% increase.
Because the update is probably too broad it includes all vendors modify the update query by adding
a parameter entry that will generate a dialog box. Figure 83 shows the modification that will yield a
dialog box that will request the vendor code for the update. Note also that the date of the update is
included. (The system date is provided by the Date() function.)
56
MS-Access-Tutorial
57
MS-Access-Tutorial
Before you run the update query you have just created and saved, take a look at the current prices for
vendor 21344, shown in Figure 85.
58
MS-Access-Tutorial
If you click on the Yes button shown in Figure 86, your update query will generate the parameter
request through the dialog box you see in Figure 87. Note that the vendor code 21344 has been entered
to match the display in Figure 85.
59
MS-Access-Tutorial
As soon as you click on the OK button shown in Figure 87s parameter request, the update query will be
launched. But before it actually updates the table, Access gives you one final warning and a way to back
out of the update. Note that the warning shown in Figure 88 shows that you will be updating three rows.
(If you check Figure 85 again, youll see that this is the correct number of rows.)
If you now click the Yes button shown in Figure 88, the update query will make the requested updates.
Open the PRODUCT table after running the update query and check the results. Figure 89 shows that
the changes were made as requested.
60
MS-Access-Tutorial
Compare the original prices shown in Figure 85 to those shown in Figure 89. Note that the initial price
of PROD_CODE = 13-Q2P2 supplied by vendor 21344 was $14.99. The updated price is $14.99 *
1.10 = $16.489, which is properly rounded to $16.49 because the PROD_PRICE data type and format
were both recorded as currency when the table was created. Also note that the update was made on
March 27, 2006. Incidentally, you can even record the time of the update, using the MS Access Time()
function for a new field named PROD_UPDATE_TIME. You can even record the identity of the person
who made the updates through an authorization code that may have been recorded on a scanning device
of some sort. By recording the update date, time, and person you can track all changes and assign proper
responsibility.
61
MS-Access-Tutorial
62
MS-Access-Tutorial
When you run the query shown in Figure 90, you will be prompted to enter the invoice number 1009 and
the line number 1 and the query will properly execute to produce the first line total. Next, run the query
again, this time entering the invoice number 1009 and the line number 2. When you are done, open the
LINE table to see if the amounts were calculated properly. Figure 91 indicates that they were.
63
MS-Access-Tutorial
You can use additional update queries to calculate the invoice subtotal, the tax, and the total. Then enter
the amount paid, $100.00, and then use an update query to calculate the balance due and the customer
balance. At this point, the INVOICE tables INV_NUMBER 1009 shows no entry for any of the values
that are yet to be calculated. (See Figure 92.)
64
MS-Access-Tutorial
Keep in mind that you cannot get a total for a set of values in a query and then use those values during
the same query run. For example, to get the sales for invoice 1009, you will have to add $169.00 and
$11.74 and then use that sum to update the INVOICE tables INV_AMOUNT. Figure 93 shows a simple
parameter query to generate the sum of the line amounts for the selected invoice number. (Note that this
query used the sum function to get its job done. This is not an update query.)
Save the query shown in Figure 93 as qrySumLineAmountsForSelectedInvoice and run it to check its
execution. Figure 94 indicates that the query performed its job properly. (Note that the sum of the last
two LINE_AMOUNT values in Figure 91 should be $169.00 + $11.74 = $180.74.)
There are many ways to enter the $180.74 value into the INVOICE table. (Although you can create an
update query that uses the results of the parameter query in Figure 93 to update the INVOICE table, you
will learn in Section 5.1 how to use some simple, but more effective ways to get that job done.)
However, since this section deals with various query types, lets take a look at some other queries that
will turn out to be useful.
65
MS-Access-Tutorial
Next, select the data source for the new table as shown in Figure 96. In this case, the data source will be
the qrySumLineAmountsForSelectedInvoice query, so select the Queries tab and then select the
query from the list.
66
MS-Access-Tutorial
Adding the query will place it on the design screen. Next, drag and drop the query fields on the Field:
lines as shown in Figure 97. Make sure that you save the just-completed Make Table query. (As you
can tell by looking at Figure 97, the query has been saved as qrySalesBySelectedInvoice.
Note that the updated query list in Figure 98 includes the new query.
67
MS-Access-Tutorial
When you run the new qrySalesBySelectedInvoice query, Access will alert you to the fact that this
query will modify data in a table. (See Figure 99. The new SALES table does not yet contain any data,
but that is about to change.)
If you accept the fact that the query will modify the data in the new SALES table, click on the Yes
button shown in Figure 99 to generate the invoice number entry request in Figure 100.
68
MS-Access-Tutorial
Enter the invoice number 1009 as shown in Figure 100 to produce the warning shown in Figure 101.
Click the Yes button to complete the Make Table action.
The results are shown in Figure 102. Note that the SALES table has been added to the list of tables and
that the table contents show the invoice number 1009 and the sales (invoice) amount $180.74.
69
MS-Access-Tutorial
Now that you have a good (SALES table) data source, which is the sum of the invoice lines for
INV_NUMBER 1009 in the INVOICE table, you can easily create an update query to update the
INVOICE tables INV_AMOUNT. Note that Figures 103 and 104 show the required update query
structure and the results of running that query.
70
MS-Access-Tutorial
71
MS-Access-Tutorial
Save the new append query as qryAppendDateToSaleTable and then close the query. You will now
see the new query as shown in Figure 107.
72
MS-Access-Tutorial
Running the new append query will cause Access to provide the appropriate warnings and the parameter
request as shown in Figures 108, 109, and 110.
73
MS-Access-Tutorial
You can check the results of running the append query by checking the SALE table. Figure 111 shows
that the append query performed its work as intended.
You can now run the qryUpdateInvoiceAmountQuery again see Figures 103 and 104 to enter the
INV_AMOUNT into the INVOICE table for invoice 1008. (Actually, that entry was already made
manually before as you can see in Figure 39s INVOICE table, so this $399.15 value from the SALE
table simply replaces that value.)
At this point, you have learned how to use various query types to store, update, and transfer data.
Therefore, you have the basic tools at your disposal to help you automate the sales transaction process.
For example, you can now create update queries to update the remaining fields in the INVOICE table, to
reduce the QOH values in the PRODUCT table, to update the CUST_BALANCE in the CUSTOMER
table, and so on. Macros or Visual Basic code can then be used to complete the automation process by
executing the queries behind the scenes. Figures 112 through 115 show several update queries and
their effects.
74
MS-Access-Tutorial
75
MS-Access-Tutorial
Keep in mind that all the queries must be run in the proper order. For example, you cannot calculate the
sales tax before you have the updated invoice total. And the invoice total cannot be calculated unless all
the invoice line totals have been calculated and summed. The table values will become meaningless if
the queries were executed in the wrong order. Therefore, it would not be wise to trust humans to
remember the proper sequence. Fortunately, you can use programming techniques or macros to
automate the process. (You will learn how to create and use macros in section 5.)
76
MS-Access-Tutorial
Note
Although the update and append queries can be very useful in managing transactions such as
invoicing and letting customers make payments on their accounts, there are other techniques available
that will perform the transaction management job more efficiently. You will learn how to use a Set
Value technique to manage customer payments on account and to automate the process through
macros in Section 5.
3.1 Forms
While queries let you get data and/or information from the database, forms let you control the
presentation format much better. In addition, forms will enable you to control data input and to present
the results from multiple queries and/or tables. Forms can also be used to tie the application components
together through menus and other devices. In short, forms are the way in which the end user is best
connected to the database and they provide that professional look to your data management efforts.
Forms may be based on tables and/or queries. The simplest and most efficient way to create a form is
through a form wizard, which lets you automatically generate the code that produces the form. Figure
116 illustrates the use of one of the many form wizards. Note that the form production process in Figure
116 is set in motion through the following sequence:
1. Select the Forms option on the left side of the screen.
2. Select the New option to produce the New Form dialog box.
3. Select the Autoform: Columnar option.
4. Select the data source by clicking on the down arrow. This action will produce a drop-down list
not shown here from which you select the data source. Note the selection of the CUSTOMER
table.
5. Click OK to create the form shown in Figure 117.
77
MS-Access-Tutorial
78
MS-Access-Tutorial
As you can see by looking at Figure 117, the form opens up in its Form View format. (Given the work
you have done with the query development, you should be familiar with the various views.)
Save the form before continuing the form design process. Figure 118 shows that the new form was
named frmCUSTOMER.
Note
Here is another example of self-documentation: the frm prefix indicates that the object is a form and
the capital letters used in the CUSTOMER portion indicate that the query data source was a table
named CUSTOMER. If the forms data source had been a query named qryCustomer, the form name
would have indicated the data source through the use of lowercase letters, using caps only to
separate the components. Therefore, if the form had based on the qryCustomer query, the form name
would have been frmCustomer. In either case, looking at the object name would immediately tell you
that the object is a form and that it displays customer information.
Making it easy to keep track of the many components in a set of database applications is a mark of
professionalism. Nobody wins of nobody can figure out what the database objects are or what they
do. Documentation conventions should be made in clear in the formal application documentation.
79
MS-Access-Tutorial
The first time you open the form in its design format, you will see the attribute list shown in Figure 120.
All the CUSTOMER tables attributes have already been included in the form, so go ahead and close
this field list. (If you later want to open this field list box again, click on the Field list button shown in
Figure 120.)
80
MS-Access-Tutorial
If you want to widen the form, put the cursor on the forms edge to change the cursor to the format
shown in Figure 120, and then drag the form limit to wherever you want it to be. You can control the
vertical size the same way. Just put the cursor on the top edge of the Form Footer and drag up or down
to suit your needs. If you put the cursor on the bottom edge of the Form Footer, you will be able to
create a footer and control its width. (Incidentally, the horizontal and vertical rulers can be used later to
help you line up selected output components or to mark multiple selection on the form.)
Figure 121 shows that the form limits have been dragged down and to the right to produce a larger form
surface on which the place the form contents. Also note the various design components that have been
marked on the figure.
81
MS-Access-Tutorial
82
MS-Access-Tutorial
Practice moving the form components around until they approximately match Figure 123. Then change
the fonts to Bold as shown in Figure 123.
You can also change the label box contents by selecting the label box and then clicking on that selected
label box to put it in edit mode. You can then edit label text. Note that Figure 124 shows that the
CUST_CODE was changed to Customer code. (Remember that the label box width can be changed by
dragging its limits.) Note also that the cursor was placed over the text box move handle to change the
cursor shape to a pointing finger you can now drag the text box to line it up with the edited label box.
83
MS-Access-Tutorial
You have a large number of form design tools available. For example, you can add additional text,
create boxes to delineate form components, and so on. If the form Toolbox is not open, right-click on
any blank space on the form to generate the dialog box you see in Figure 125. Click on the Toolbox
selection to open the toll box you see in Figure 126.
84
MS-Access-Tutorial
As you examine Figure 126, note that the tool box has changed shape and location. Like almost any
Windows object, you can drag its limits to change its shape and you can drag it by placing the cursor
over the drag handle and holding the mouse button down while moving the cursor. If you move the
tool box into the screen margin, it will become just another button bar. (This was just a reminder you
should know Windows well enough to be familiar with its operation.) Figure 127 shows that the tool box
was moved and its shape was changed to have a double row of buttons arranged vertically.
+
Figure 127 also shows that the Label button was selected. This selection changes the cursor to the A
shape you see here. If you drag this cursor anywhere on the form, you will create a text space in which
you can type whatever is needed. In this example, the typed text is Customer Information. Click
anywhere outside the text space to return the cursor to its normal function.
85
MS-Access-Tutorial
You can edit the text as needed. Note that Figure 128 shows that the font size was changed, the text was
centered, and the label box was resized.
The forms looks can be improved with raised text box limits and color and you can use the rectangle
tool to create logical groupings of information presented on the form. Figure 129 shows several
enhancements that you can accomplish by doing the following:
Select the Customer Information text box you just edited as shown in Figure 128. (The
selection is confirmed by the selection markers the small black squares around the text box
perimeter.)
Click on the Special Effect: Shadowed button to generate the special effect options.
Select the Shadowed option by clicking on it. Note the change in the text box edge.
Also note when you compare Figures 128 and 129 -- that the Toolbox has been moved and that its
shape has been changed by dragging its limits. (And the attribute text boxes and their label boxes have
been moved, too.) Go ahead and change the form youre working on to approximately match this format
to keep in synch with the remaining discussion. After you have explored the use of many of the form
design tools, you can, of course, change the form to suit your interests.
86
MS-Access-Tutorial
Lets take a look at just a few more form design options. For example, if you look at Figure 130, you
will see that there are quite a few color options available. The match the figure you see here, you can
take the following actions:
To make the form light blue, click on any empty portion of the form to select the entire form.
Next, click on the Fill/Back color button and click on the light blue color square.
To make the Customer Information text box dark blue, click on the text box to select it note
that Figure 130 shows the selection markers around the text box perimeter. Next, click on the
Fill/Back color button and click on the dark blue color square.
To change the font color in the text box to white, make sure that the text box is still selected.
Next, click on the Font/Fore color button and click on the white color square.
Note that Access keeps track of all the recently selected colors to make it easy to later match color
selections of other form components.
87
MS-Access-Tutorial
It is often useful to group logically similar attributes together as a visual unit. You can use the
Rectangle tool to draw a rectangle around such a group. To get the job done, go to the tool box and
click on the rectangle tool, then drag a rectangle around the attributes you want to group. The rectangle
is initially clear and just shows its outline. However, you can use the Fill/Back color option to give the
rectangle a color. Figure 131 shows that the fill selected color was light blue. Unfortunately, the default
setting on the rectangle tool places the rectangle in front of the attributes, thus shading them out.
However, note that you can use the Format/Send to Back option to send the now opaque light blue
rectangle to the back, thus making the attributes visible again.
Repeat the process for the balance attribute, using a light green color. Finally, drag the Customer
Information text box limits to line up with the two rectangles to balance the form. When you are done,
open the form in its Form View to see Figure 132.
88
MS-Access-Tutorial
The form in Figure 132 shows two features you may want to remove: the record selector and the
dividing line. You can control such form presentation features through the properties box. Open the
form in its design view and then right-click to produce the Properties Box as shown in Figure 133. (In
this case, the right-clicking was done on the forms Detail section.)
89
MS-Access-Tutorial
Click on the Properties option shown in Figure 133 to produce the properties box for the
frmCUSTOMER forms detail section. If you want to produce the properties box for the entire form,
rather than for some selected portion of that form, click outside the forms limits, as was done in Figure
134.
Note
You can get the properties box for any Access object by selecting it and then right-clicking on your
selection. For example, if you want to see the properties of just the CUST_CODE text box, click on
the CUST_CODE text box to select it and then right-click. If you want to see the properties of the
Customer code: label box, click on the CUST_CODE textbox to select it and then right-click. And so
on.
Regardless of where you right-clicked to produce the initial properties dialog box, you can generate
the properties for any Access object by simply clicking on it. Go ahead and perform this action a few
times until it becomes routine.
90
MS-Access-Tutorial
As you examine Figure 134, note that the Records Selectors property has been set to No. The same
action was taken for the Dividing Lines property. The available options for each property can be
generated by simply clicking on it to produce an arrow head such as the one shown for the Record
Selectors property you can then click on the arrow head to see the list from which you can make the
selection. When you open the form in Form View again, the record selector and dividing line are gone.
The form is essentially completed. However, you may want to put the Customer Information text box
in a header. You can create the header space by simply dragging the forms detail limit down. (You can
create the form footer by simply dragging the footer limit down.) Finally, if the white background for
the textbox contents are annoying to you and you want them to have the same color as the rectangles,
you can select them and then use a fill color to match the background. As you can tell by looking at
Figure 135, that was done to create this illustration. Figure 136 shows the frmCUSTOMER form in
Form View. (Naturally, given your familiarity with the Windows environment, you can resize the
window and the form within that window.
91
MS-Access-Tutorial
92
MS-Access-Tutorial
93
MS-Access-Tutorial
When you are done with the expression builder, click OK to save the expression and then drag and drop
all the remaining INVOICE table fields to the QBE screens field spaces. Save the query as
qryInvoicesByCustomer and then open it in data sheet form as shown in Figure 138.
Next, begin the form design just as you did in the previous section. However, this time you use the
qryInvoicesByCustomer query as the data source. Use the formatting techniques you learned in the
previous section to produce the form you see in Figure 139.
94
MS-Access-Tutorial
Figure 140 shows the final form view the form limits were dragged in to reduce the presentation size
and to let you see the form relative to its Forms list. (Note that the window has been resized to show
both screen components and that the form limits have been dragged to limit its display size.)
95
MS-Access-Tutorial
In the next section you will learn how to use one form as a subform to another, so lets go ahead and
build an invoice line form next. You will also learn some additional techniques that will turn out to be
very useful when you try to control input via a form.
Start by creating the form for the invoice lines by repeating the now familiar routine you used early in
Section 3.1, Figure 116. (You can use the LINE table as the data source.) Format the form to select
color, font size, and other characteristics to match the form you see in Figure 141.
96
MS-Access-Tutorial
As soon as you have drawn the subform outline shown in Figure 142, you will see its automatically
generated label in this example, it is Child18. (You will probably see a different child number
designation, because the number is based on how many times you gave created a child object before.)
While the subform is still selected note the selection squares in Figure 142 open the Properties box
for the subform. (Note that the properties box is automatically labeled as Subform/Subreport: Child
18.) Now click on the Source Object and select the frmLineWithProducts form from the list as shown
in Figure 142.
Next, click on the Link Child Fields as shown in Figure 143. (This action will produce the Subform
Field Linker you see in Figure 143.) Note that the (correct) link is made through the INV_NUMBER,
because the INV_NUMBER is the foreign key (FK) in the LINE table that links the LINE table to the
INVOICE table. Because this selection is correct, just click OK to accept it. You are done thats all
there is to it. Naturally, you may want to drag the form and/or subform limits to line up components or
to perform other formatting chores you learned earlier in this section. When you are done, open the
form/subform in form view to see Figure 144. (Note that the illustration shows the selection of invoice
record 4. This invoice contains three invoice lines. To preserve the original frmInvoicesByCustomer
form, save the new form as frmInvoicesByCustomerMainFormWithSubform.)
97
MS-Access-Tutorial
98
MS-Access-Tutorial
The form/subform design can be extended to include subforms that themselves include subforms. For
example, you can use the just-created frmInvoicesByCustomerMainFormWithSubform as the
subform to the frmCUSTOMER you saw in Figure 136. (Use the techniques you just learned see
Figures 142-145 -- to produce the form/subform you see in Figure 145. Save the new form/subform as
frmCUSTOMERwithInvoiceAndInvoiceLines to preserve the original frmCUSTOMER form.
Thus far, you saw how forms could be used a subforms to other forms. However, you can also create
subforms based on queries, without first using a form to format the query output. Start the form/subform
design with the same technique that you saw in Figure 142. (Remember to turn off the Form/Subform
Wizard to retain direct control of the design process.) Figure 146 shows that the Source Object is the
table named LINE. To preserve the original frmInvoicesByCustomer form on which this new
form/subform is based, save this second version of the invoice/line form/subform as
frmInvoicesByCustomerMainFormWithSubform-Version2.
99
MS-Access-Tutorial
When you open this new form in Form View, you will see the output in Figure 147. Whether you use
this form/subform design approach or the one in Figure 144 depends largely on end user desires and
your own preferences. (Figure 147s output shows all the invoice lines at once, while Figure 144s
invoice lines are shown one at a time as you click through the records.)
100
MS-Access-Tutorial
Incidentally, you can create a form to produce an output that looks remarkably like a query output. For
example, take a look at Figure 148, which shows the start of the creation of a form based on a tabular
presentation format. (Note that this form will be based on the qryLineWithProducts query.)
When you click OK on the New Form wizard dialog box shown in Figure 148, youll see the output in
Figure 149. (As you can tell, the window size has been decreased by dragging its limits, using the
standard Windows procedure.)
Naturally, you can modify the wizard-produced form, using the design techniques you learned earlier in
this section. Figure 150 shows the edited form. Save the form as frmLineWithProducts-TabularView.
101
MS-Access-Tutorial
You can substitute the frmLineWithProducts-TabularView form in Figure 150 as the subform in the
frmInvoicesByCustomerMainFormWithSubform-Version2 form/subform you saw in Figure 146.
(Note that Figure 151 shows that you have simply used the frmLineWithProducts-TabularView as the
new Source Object.)
102
MS-Access-Tutorial
103
MS-Access-Tutorial
7. Select the Row Source as shown. In other words, the row source for this combo box will be the
query named qryNewSalesLineWithProducts.
8. To let the end user see what is expected to be entered, three fields will be shown. Therefore, the
Column Count is set at 3.
9. Because the control source is the PROD_CODE in the LINE table, only the PROD_CODE field
can be bound to the LINE table. This field value is the first one in the query, so the Bound
Column is column 1.
10. To let the end user know the meaning of the combo box items, select Yes to mark the Column
Heads option. (You can edit the column head text by using the properties for each of the three
fields at the query level. If you want to edit the column heads, you can open the
qryNewSalesLineWithProducts query in its Design View and make the necessary changes.
11. There are three columns to be displayed. To control the Column Widths, enter the column width
values in inches. Note the selection of 0.7;2;0.8 in this option line. (Actually, if you type
.7,2,.8, Access will format the entry for you.) This first entry is an approximation, so may have to
toggle back and forth between the forms Design View and Form View to get the precise
column width setting.
12. The List Rows option shows the default value 10. You can change this value by simply typing
the number of rows you would like to see displayed. Access automatically creates a scroll bar for
you if the actual number of rows exceeds the specified number of rows.
13. For documentation purposes, change the combo box Name to nmProductCodeComboBox.
14. Edit the combo box label on the form to change the Combo23 shown here to Product Code:
15. Drag the completed combo box to the original product code location marked in Figure 153
and drop it. (Figure 154 shows the combo box in its new location.)
16. Save the newly edited frmLineWithProducts-ComboBox form.
104
MS-Access-Tutorial
Open the frmLineWithProducts-ComboBox form in its form view to inspect the results shown in
Figure 155. If you now click on the down arrow at the combo box margin, you will see the available
entries in Figure 156. (You can make a selection by simply clicking on a listed item. However, do not do
so at this point, because you will change the invoice line product code, thus destroying the historical
accuracy of the transaction you see here. You can experiment with the combo box when you make a
new sales transaction.)
105
MS-Access-Tutorial
The procedures for creating a list box are similar to those used in the creation of a combo box. One
major difference is the starting point click on the List Box button in the Toolbox to get the process
started, then drag a space for the list box just as you did for the combo box. Figure 157A shows these
features based on the frmCUSTOMER form:
1. The list box was created and that its properties show a CUST_TITLE Control Source.
2. The Row Source Type is a Value List. (You can click on this property and then click on the
resulting down arrow to see the other row source type options.
3. The list of values is shown in the Row Source. Note that the available options are Mr.; Ms.
and so on. The value list option was selected because there are only a few available values. If
106
MS-Access-Tutorial
4.
5.
6.
7.
the list of values had been long, the values might be listed in a table created for this purpose and
the row source type would then be a table.
Note that there is only a single column of values, so the Column Count is 1.
The (default) Bound Column is 1 there is only a single column available.
The value list does not have a column head, so the Column Head selection is No.
Change the Name of the list box shown in Figure 157A from List22 to the more descriptive
nmCustomerTitleListBox.
Save the frmCUSTOMER form as frmCUSTOMER-ListBoxDemo to preserve the original form for
further experimentation.
As you examine the modified form in Figure 158, note that the original Customer title: textbox has
been deleted from the form and the new list box has been substituted.
107
MS-Access-Tutorial
Figure 159 shows the results of the list box modification when the form is opened in its form view.
To select a customer title, simply click on an item of the list and then move to the next field. Use the
scroll bar to select any item that may be located below the lower limit of the textbox. Figure 160 shows
that several customer titles were entered via the list box.
3.1.6 Menus
If you want to make selected queries, forms, and reports easily available, a menu is a good way to get
the job done. Creating menus is easy just select Forms/New/Design View, then click OK. (Do not
select a table or query, because the menu will not be tied to any one query or table.) When you click
the OK button, you will see a clean design screen on which to create the menu.
Next, select the tool box, select the Tab Control option shown in Figure 161 note that the cursor
changes shape when you do that -- and draw the menu outline by dragging the menu cursor. Figure 161
shows the result of dragging the menu cursor. Note also that the initial use of the tab control
automatically generates two tabbed menu pages, Page1 and Page2.
108
MS-Access-Tutorial
While the menu form is still selected, right-click on the Page2 tab to pop up the options list you see in
Figure 162. Select the Insert Page option. This selection will produce the Page 3 tab you see in Figure
163.
109
MS-Access-Tutorial
As you can tell by looking at Figure 163, the tab labels can be edited through the Properties Box. (As
usual, the properties box is generated by right-clicking on the form component and selecting Properties
from the list thats the last item you see on the list in Figure 162.)
You are now ready to place a few command buttons on the menu pages. Such buttons will be used to let
the end user select a particular object in this case, a query, a form, or a report. (You will learn about
report generation in Section 3.4.) As the name command button suggests, clicking on such a button will
generate a command that will execute the selected option. (That is, the command button will perform
that duty when it has been linked to a macro or Visual Basic code. You will learn how to create and use
macros in Section 3.5. This tutorial will not cover Visual Basic programming.) Figure 164 shows how a
command button is created on the first (queries) page. Make sure that the first page of the menu form
has been selected click on the Queries tab.
Note
If you do not pay attention to the page you are supposed to be on, you may wind up placing command
buttons on all pages simultaneously or you may place a queries command button on a forms or reports
page. Placing a command button on the wrong page or on multiple pages at once will have major
(and unwelcome) consequences when you later connect a macro to open a query and discover that it
also opens a form or some other object. Therefore, if you intend to produce one or more command
buttons on the Queries page, make sure that you select the tab for that page when you are in design
mode.
110
MS-Access-Tutorial
Go ahead and look at the form in form view. You can see in Figure 165 that the menu has a few
unwanted properties such as record selectors. (The default design of a form also includes the nowfamiliar dividing lines not shown here.) Therefore, clean the menu form by performing the actions
indicated in Figure 166. Save the menu form as frmMainMenu.
111
MS-Access-Tutorial
To edit the first command buttons name and caption, stay in the design view, right-click on the
command button to produce the properties box for that button. Change the name and caption as shown in
Figure 167.
112
MS-Access-Tutorial
When you are done with the editing shown in Figure 167, continue the editing to produce the results in
Figure 168. (You should know how to boldface the command buttons text, how to change the text
color, how to change the size and location of the command button by dragging its limits, and how to edit
the tabs.) Note that this command button occurs on the Queries page only the remaining two pages are
still blank. (Go ahead and click from tab to tab to see the results.)
Lets create few more buttons for the Queries page. The easiest way to do this is to use a procedure you
should know if you are familiar with Windows. That is, select the object in design view and then use the
edit/copy/edit/paste routine to make and place copies. (Remember to make sure that you are on the
Queries page!) Drag the buttons to their intended positions. Note that the editing routine ensures that all
the buttons have the same properties. When you have made and placed the button copies, edit each to
match the results in Figure 169. Dont forget to save your efforts from time to time. (Save the form as
frmMainMenu.)
113
MS-Access-Tutorial
Next, copy the buttons you see here and paste tem into the Forms page, then edit the buttons to match
the forms that will be included. When you are done, your page in design view should match Figure
170. Note that the letter color has been changed to dark green and that this page has one more button
than the query page.
114
MS-Access-Tutorial
You have not yet created any reports, so leave the Reports page blank. (You will learn how to create
reports in Section 3.4.)
Next, lets create a Close Menu command button that shows up on each page. Figure 171 shows that a
single button has been copied and pasted outside the pages.
When you drag the command button from its outside the page location and drop it on the first page, it
will show up on each page. When this button is activated via a macro you will develop in Section 3.5,
you will be able to close the menu from any page. (Go ahead and do the just-described drag-and-drop
routine and then click through each pager and note that the Close Main Menu button is located on each
page.)
Finally, lets dress up the menu by using a picture. Any picture will do, but there happens to be a picture
of a Lotus flower in the same folder as the database, so lets see how that picture may be inserted into
the menu form. Figure 172 shows the procedure. Make sure that the picture frame is initially drawn
outside the page limits and then drag-and-drop it on the first page to make sure that the picture shows up
on all pages.
115
MS-Access-Tutorial
When you click OK as shown in Figure 172, the picture will be inserted but all you see is a small
part of it. Thats because the default setting is Clip. Use the pictures properties box to reset the
presentation to Zoom as shown in Figure 173.
116
MS-Access-Tutorial
Note that the Image object in Figure 173 was created outside the menu forms detail section. Therefore,
if you drag this new object to any page, it will show up on all pages. Go ahead and perform the drag
and drop routine, then drag the image objects boundaries to enlarge the image as shown in Figure 174.
You now have an attractive menu that will become the hub for managing the Access applications. (You
can move from menu page to menu page by clicking on the tabs.) Save the form again when you are
satisfied with the forms layout. (The form was saved earlier as frmMainMenu.)
117
MS-Access-Tutorial
4.1 Reports
Although reports often contain the same information as forms, they do have several advantages. First, it
is much easier to show multiple-record information in reports than in forms. Second, given their layout,
reports are much easier to print than forms. In addition, if you have a lot of numeric data to present, the
Access report format enables you to produce subtotals, totals, and grand totals as the report is generated.
The Access report wizard is excellent and it requires little effort on your part to produce a usable report.
The report generating sequence Reports/New -- is shown in Figure 175. Note that the data source is a
query named qryCustomerInvoices.
As soon as you click the OK button shown in Figure 175, Access will generate the report and show its
output. Only a few of the report records are shown. (See Figure 176.)
118
MS-Access-Tutorial
Although the report output shown in Figure 176 is already quite usable, you should use the now-familiar
design tools and practices to add functionality and eye appeal. (After all, end users are likely to grade
your professional expertise on the basis of the presentation quality.) However, before you start editing
the report, save the report as rptCustomerInvoices. Note that naming the report to match its source in
this case, a query named qryCustomerInvoices maintains the self-documentation standard. (Note the
rpt prefix denotes a report.)
Lets begin the editing by improving the headers. Figure 177 shows that the font was changed and
moved to improve the presentation format. Note also that Access has placed some handy functions at the
bottom of the report you can move those to the top of the page later and then add the date function,
date().Because all the necessary fields have been included, go ahead and close the field list for the
qryCustomerInvoices.
119
MS-Access-Tutorial
Save the changes you have just made and then open the report in Print Preview to generate the results
shown in Figure 178. (Only a few records are shown. You may have to go back and forth between the
design and print preview to help you line up the various text boxes.)
120
MS-Access-Tutorial
Lets do some additional report editing. First, move the time and page functions to the top of the page
and add the date function as shown in Figure 179. Next, lets generate the invoice total for each
customer; that is, you need a customer subtotal. Therefore, the invoices must be grouped by the
CUST_CODE field and you must have a place known as a detail footer -- to display the output. To
create a detail footer, (right) click just outside the detail section to generate the options list for the detail
section. The edits are shown in Figure 179 and the edit results are shown in Figure 180.
Now that you have added the section footer, you can insert subtotals for the section and then insert totals
for all the customer invoices in the report footer. Note the textbox formats in Figure 181 and then look at
the revised reports output. Make sure that you save the report again to preserve all the changes you
121
MS-Access-Tutorial
have made. Figures 181 and 182 show the various formatting changes and the final output. Save the
report from time to time, using the report name rptCustomerInvoices.
As you examine Figure 181, note the creation of the (computed) customer total textbox and especially
note its computation through the Sum function. The same function is used to compute the grand total.
Aside from the fact that the two new text boxes have different labels, the important difference between
the two textboxes customer total and grand total is their location. Figure 182 shows the report
output.
122
MS-Access-Tutorial
123
MS-Access-Tutorial
124
MS-Access-Tutorial
You are now ready to create the label report. In this case, the label wizard is a good place to start. (You
will be able to edit the label wizards results later.) Figure 185 shows the start of the process. (Note that
the query named qryCustomerLabels was selected to be the data source for the labels.)
When you click on the OK button shown in Figure 185, youll see the label size selection dialog box in
Figure 186. As you can tell the current selection is an Avery label set, three across the label page, using
labels that measure 1 x 2.
Because this is a very popular label format, go ahead and accept this selection by clicking on the Next >
button in Figure 186 to generate Figure 187. (As you can tell by looking at Figure 186, there are quite a
125
MS-Access-Tutorial
few label format options available only five are shown here, but the scroll bar will reveal quite a few
more.)
After selecting the label font in Figure 187, click on the Next > button in Figure 187 to generate Figure
188 to select the fields to be placed on the label.
126
MS-Access-Tutorial
To place a field on the label, select it and then click on the > button shown in Figure 188 to move it to
the Prototype label. The annotation in Figure 188 tells you how to get a line break so that you can move
to the next label line.
When you have moved all required fields to the Prototype label section, click on the Next > button in
Figure 189 to generate Figure 190 and then type the name for the label report.
127
MS-Access-Tutorial
As you can tell by looking at Figure 190, the name rptCustomerLabels was used to reflect its use of the
qryCustomerLabels query as the reports data source. Click on the Finish button in Figure 190 to save
the report and to see its label output in Figure 191.
As you can tell by looking at Figure 191, the output needs some touch-up editing. The output would
have a much more professional look if there were some space between the city and the state and between
the state and the zip code. Also, putting a comma after the city name is standard. Therefore, open the
label report in its design view and then generate the properties box for the third text box. Scroll down to
the Control Source and make the insertions shown in Figure 192. (Use the Expression Builder to make
the editing changes.)
After you have completed the edits in Figure 192, save the label report again and then open it in its
Print Preview to see the output in Figure 193. Note that the output has a much more professional look
than the one you saw earlier in Figure 191.
128
MS-Access-Tutorial
Using the techniques you have learned in this tutorial, you can easily modify the query to enter last and
first name parameter entries and then to use this modified query as the data source for a label report that
prints labels for one or more selected customers. For example, save the qryCustomerLabels query as
qrySelectedCustomerLabels and then make the changes you see in Figure 193A. Next, save the
rptCustomerLabels report as rptSelectedCustomerLabels and then use the new query as its data
source. (See Figure 193B.)
129
MS-Access-Tutorial
Now add three command buttons on the main menu forms Reports page, one for the customer invoice
report, one for the customer labels, and one for selected customer labels.
130
MS-Access-Tutorial
the prefix repeated. Access will automatically use the macro croup name to label each macro within that
group. Therefore, Access will reference the Maximize macro in the mcrMainMenu group as
mcrMainMenu. Maximize.
To create the macro named Maximize, start by selecting a line in the Macro Name column and then
simply type Maximize as shown in Figure 194. The action is to maximize the screen, so click on the line
in the Action column to place a down arrow on that line. (Note the circled down arrow a few lines
farther down in Figure 194.) Clicking one the down arrow will generate a list of all the actions that are
available to you. Click on one of the options in this case, Maximize, to transfer the selection from the
list to the action line.
The second macro will close the main menu, so type CloseMainMenu in the macro name column as
shown in Figure 194. This macro will include two actions. First, it will maximize the screen just in
case you decided to restore the screen manually and, second, it will close the form. Note that Close is
one of the action options on the list, so click on that option to transfer it to the action line. (You can also
type Close, without selecting it from the option list. In fact, if you type the first few characters of any
action command, Access will fill in the remaining characters.)
The Close action triggers an action argument box such as the one you see in Figure 194. In this case, the
action argument has three components: an Object Type, an Object Name, and a Save. Clicking on the
Object Type line will produce a down arrow again and clicking on that arrow will produce a list of
131
MS-Access-Tutorial
object types. Because this action involves the main menu form, select the Form option as shown here.
Select the Object Name the same way the object is a form named frmMainMenu, so thats the one
you select from the list. The Save option has a Prompt default selection, so go ahead and keep that
default.
As you examine Figure 194 closely, note that the initial action is entered one line lower than the macro
name. Although it is not necessary to do that, you will find that procedure very handy if you later decide
to insert an action before the first one. (All you have to do then is put the cursor on the first action line
below the macro name and tap the Insert button.) Also, note that a comment was typed in for each of
the actions. Since these actions are very simple and are named to reflect their use, comments are not
required here. However, its a good idea to use comments when the macro actions become more
complex and have multiple components.
Go ahead and save the new macro group as mcrMainMenu. Thats it. You can now create the
remaining macro groups the same way. Figure 195 shows the macros in the macro group named
mcrMainMenuQueriesPage. Note that the OpenQuery action triggers an Action Arguments box that
contains a Query Name, the desired View, and the Data Mode.
Repeat the process for the forms and the reports. Naturally, when you select the action OpenForm from
an action list, the action arguments box contains references to form actions and when you select the
action OpenReport from an action list, the action arguments box contains references to report actions.
Save each macro group, using the macro group names suggested earlier in this section. When you are
done, your macro groups will show up as listed in Figure 196.
132
MS-Access-Tutorial
Now open the main menu in form view and click on the Close Main Menu command button and note
that the macro closes the form as intended.
133
MS-Access-Tutorial
Use the same procedure to connect the appropriate macros in the mcrMainMenuQueriesPage and
attach them to the command buttons on the main menus query page. Figure 198 summarizes the
procedure.
Repeat the process for the forms and the reports. You have now created a simple menu-driven system.
Naturally, you can place command buttons on the forms and reports to let you close them as well as
open them. (At this point, you have to close the forms and reports by using the Windows close option on
the screens.)
134
MS-Access-Tutorial
Now that you have created the new menu page, map out what you want the command buttons to do. The
Make payment caption on the first command button shown in Figure 199 is largely self-documenting.
But what precisely do you want to manage through this command option? Before you try to write the
macros to accomplish the intended tasks, write a short, but precise, narrative. In this example, the
narrative spells out what is to be done:
1. Find the customer who wants to make a payment.
2. When the correct customer is found, make a payment entry.
3. When the payment is made, the customer balance must be updated to reflect the payment.
4. The end user must be able to track all payments by customer, date, and amount.
5. The end user should enter only the payment amount customer numbers, date entries, and new
balance calculations must be done automatically.
Item 4 in the preceding list makes it clear that you need to create a new table in which to store all the
payment transactions. Therefore, create a new PAYMENT table that includes the attributes shown in
Figure 200. Note that the CUST_CODE is the foreign key in this new table.
135
MS-Access-Tutorial
Make sure that you relate this PAYMENT table to the CUSTOMER table, so your relationships window
should look like Figure 201.
If you click the Make payments command button shown in Figure 199, the first thing you will have to
do is find the customer for whom the payment is to be recorded. Therefore, you must first design the
frmFindCustomerForPayment form you see in Figure 202. This forms contents will be based on a
parameter query named qryFindCustomerForPayment. This query uses all the CUSTOMER table
fields. To enable the end user to select a customer by that customers last name, use the following
criteria statement for the CUST_LNAME field:
Like "*" & [Enter the last name ] & "*".
136
MS-Access-Tutorial
Note that the form shown in Figure 202 uses two command buttons. The first command button will be
used to execute a macro that lets the end user record a customer payment. (You will see how this macro
works after the remaining form components are completed.) The second command button shown in
Figure 202 lets the end user return to the menu without recording a payment. Clicking on this second
command button merely closes the frmFindCustomerForPayment form and returns you to the menu.
Note
If you open the frmFindCustomerForPayment form from the menu without closing the menu, that
form simply covers up the menu form. (Thats why you want to maximize each form.) Therefore, if
you close the frmFindCustomerForPayment form, the menu form becomes visible again. Because
all of a forms components, including its data components, are available in the computers memory,
such stacking becomes a very handy tool to transfer values form one form to another.
If the end user clicks on the first command button shown in Figure 202, the macro must open a form that
can record the payment. Therefore, make this form now. (Its structure is shown in Figure 203.) Note that
the forms data source is the PAYMENT table. (The data source for any form can be found by clicking
outside its form limits.)
You want to ensure that the end user makes as few data entries as possible. For example, the payment
number is entered through the Autonumber format and the payment date (PAY_DATE) uses the
Medium Date default format. You will use a macro to automate the process of entering the customer
code and the current payment balance. You will also use a macro to calculate the updated payment
balance. Therefore, the only data entry will be the payment amount.
137
MS-Access-Tutorial
If the end user clicks on the second command button shown in Figure 202, the customer information and
the payment history must be shown. Therefore, create the appropriate main form as shown in Figure 204
and the subform as shown. Create the main form (frmShowPayments) first and then create the subform
named frmPaymentSubform. Note the link between the main form and its subform in Figure 204. The
data source for the main form is a query named qryShowPayments and the subform data source is the
query named qryPaymentSubform.
138
MS-Access-Tutorial
139
MS-Access-Tutorial
The UpdateBalance macro shown in Figure 205 takes the PAY_AMOUNT value from the form named
frmMakePayment form and subtracts it from the PAY_OLD_BALANCE on the frmMakePayment
form. (Note the first SetValue actions Item: and Expression: lines in the UpdateBalance macro.)
Once the macros in the mcrPayments macro group have been written, you must attach them to the
command buttons you created on the main menu. For example, Figure 206 shows that the
SelectCustomer macro was attached to the Payments command button, using the On Click format.
After you have attached the macro as shown in Figure 206, open the main menu form and click on the
Payments command button to generate the parameter entry box shown in Figure 207. Note that the
macro worked as intended.
140
MS-Access-Tutorial
141
MS-Access-Tutorial
Making the Smith entry shown in Figure 207 will open the form shown in Figure 208. (Note that the
form shows two records, because the CUSTOMER table contains two customers whose last name is
Smith.) After deciding that this first record is the one you intend to update, click on the first command
button to trigger the next event, which is the payment transaction that will record a payment for
customer Kathy Smiths account.
142
MS-Access-Tutorial
Clicking on the fist command button shown in Figure 208 will open the payment form shown in Figure
209. Review the MakePayment and UpdateBalance macros in Figure 205 to see what actions will be
taken. You will discover that the macros will perform their intended tasks note that the payment of
$100 will update the original $411.02 balance to yield the updated payment balance of $311.02.
Note
The payment balance in the PAYMENT table is not updated until the form is closed. Therefore, you
cannot make single pass multiple payments for any selected customer. If you want to make multiple
payments for a selected customer, the correct process is to make the first payment, close the form,
open the form again, make the next payment, and so on.
This restriction is not a serious limitation -- the multiple payment in one pass scenario is not likely
in a real world environment. For example, if a customer comes in to make a $100 payment by check,
would it make more sense to write a single $100 check or to write two checks for $50 each, or even
five checks for $20 each? It is much more likely that multiple payments are made over some period of
time, in which case you would have to close the form after each payment anyway. (And if you
redesigned the system to let customers make payments on individual balances for each invoice, you
would have to close each payment transaction as you select each of the invoices.)
143
MS-Access-Tutorial
The macro attachment for the Payment Amount is shown in Figure 209A. Note that the update is to be
made On Exit, because you cannot update the balance until after you have made the payment amount
entry.
To track the payment history for any customer, use the second command button on the main menu form
shown in Figure 210. The second command button shown here uses the CurrentCustomerBalance
macro see Figure 205 -- to ensure that the selected customer is found and that this customers payment
record will be shown.
144
MS-Access-Tutorial
Note that the last name entry Smith shown in Figure 210 yields the results shown in Figure 211.
Because Kathy Smith is the only customer for whom a payment entry was made thus far, only one
record in the PAYMENT table matches the Smith last name entry.
As you examine Figure 211, note that a form/subform structure was used to produce a detailed view of
all the relevant information. The main form is basically a copy of the original customer form and the
subform is basically a copy of the payments form you created earlier. The form/subform structure is
shown in Figure 212. If you have used a copy of the payment form shown in Figure 209 that still
includes the update macro, you can make payments via this form/subform, too.
145
MS-Access-Tutorial
Go ahead and make a few more payment on account entries for Kathy Smith. A sample set of several
payment entries is shown in Figure 213.
146
MS-Access-Tutorial
Conclusion
Only a few examples are shown in this tutorial. The objective is not to develop full-blown applications,
but to show you some examples of what can be done in the Microsoft Access environment. Once you
have seen those examples, you have a foundation on which to build greater expertise. Keep in mind that
Access is a superb prototyping tool, but it is not capable of serving the full database and information
needs of even medium-sized organizations, let alone large ones. Products such as Microsofts SQL
Server, IBMs DB2, or Oracle are better candidates for such environments. Nevertheless, given its
ability to let you develop superb prototypes, Access has earned a place of honor in the ranks of database
professionals.
147