You are on page 1of 5

COMM 335 – Winter 2019 Term 1

Tutorial 4: Microsoft Access - Building Forms and Subforms

The purpose of this tutorial is to enable you to:

1) build a main form with a subform for viewing and entering data; and
2) add a calculated control to a form.

You should download the sample database, comm335_T4.accdb, from the UBC Canvas as the
foundation to create and test forms in this tutorial.

 Creating Forms with Main form and Subform

A subform is a form within a form. The primary form is called the main form, and the form within
the form is called the subform. A form/subform combination is often referred to as a hierarchical
form, a master/detail form, or a parent/child form.

Subforms are especially effective when you want to show data from tables or queries with a one-to-
many relationship. For example, you could create a form with a subform to show data from a
Customer table and a Reservation table. The data in the Customer table is the "one" side of the
relationship. The data in the Reservation table is the "many" side of the relationship — each customer
can make more than one reservation.

The main form and subform in this type of form are linked so that the subform displays only records
that are related to the current record in the main form.

Demonstration:
 Create a form to show all movies/videos with their rental information (as shown below).

Form
View

Movie Main form


in Columnar
format

Rental
Subform in
Tabular format

COMM 335 Winter 2019 Term 1 Tutorial 4 Page 1


Copyright © 2019 Y.M.Cheung
from tblMovie table from tblCategoryCode table from tblVideo table

Design
View

Movie Main
form in
Link Master Field
Columnar
format

Link Child Field Rental


Subform in
Tabular
format

from tblRental table from tblCopy table Calculated Field Calculated Control
in query in form

Key Steps:

a. Create two queries, one for the main form and another for the subform, to view all
movies/ videos with their rental records. (See note below.)

1) Fields for Movie (main form):

 from tblMovie: MovieNo (PK), Title, Rating, Length;


 from tblVideo: VideoNo (PK), Format, NoOfCopies; and
 from tblCategoryCode: CategoryDesc.

2) Fields for Rental (subform):

 from tblCopy: CopyNo (PK), VideoNo (FK); and


 from tblRental: MemberNo (FK), CopyNo (FK), DateOut, DateIn, Rate,
Discount, Returned.

(Note: To save the time, please use the two queries, qryMovieRentalMain and
qryMovieRentalSub, that have been created for you.)

b. Add a calculated field to the query, qryMovieRentalSub, to calculate the Duration of the
rental, i.e. Duration: [DateIn]-[DateOut]. Insert this field after [DateIn] field. Test the
query. Does it work?

COMM 335 Winter 2019 Term 1 Tutorial 4 Page 2


Copyright © 2019 Y.M.Cheung
Key Steps:
c. Create the form using Form Wizard:

1. Select fields from the two queries:

 from qryMovieRentalMain: All fields.


 from qryMovieRentalSub: All fields EXCEPT CopyNo (PK) of tblCopy, i.e.
tblCopy.CopyNo.

2. View your data by qryMovieRentalMain so that you can see the main form and
subform.

3. Select the Tabular1 layout (i.e. Continuous Forms View) for the subform.

4. Select any style you like.

d. Save the main form as frmMoveiRentalMain and the subform as frmMovieRentalSub.

e. Refine the form.

1. Update the form header and form caption to display “Movie Rental Form”.

2. Delete the subform title. Adjust the width of the subform and the textbox size.

3. Set “Allow Additions” of the form property to “No” for both main form and subform
so that no new records can be added.

4. Remove the navigation buttons in the subform. (Update the form property of the
subform.)

5. Lock all the fields in the main form so that they are not editable.

6. Disable all the fields in the main form so that they cannot be selected for editing.

Disabled
Locked

1
“Tabular” is the layout you select when you create the form and subform. In the Form property sheet, it will be named
as “Continuous Forms View” in the Default View property.

COMM 335 Winter 2019 Term 1 Tutorial 4 Page 3


Copyright © 2019 Y.M.Cheung
Questions:

1. Do you see the difference between Locked and Enabled?

2. Why do we want to disable and lock the Movie/Video fields in the main form?

3. What are the differences between “Record Source” and “Control Source”?

4. What are the differences between “Bound Control” and “Unbound Control”?

5. What are the differences between the Tabular layout and Datasheet layout?

COMM 335 Winter 2019 Term 1 Tutorial 4 Page 4


Copyright © 2019 Y.M.Cheung
 Adding a Calculated Control to a Form
Demonstration:

 Add a calculated control in the Movie Rental Subform (frmMovieRentalSub) to show the
Charge of each rental, i.e. [Duration] * [Rate] * (1-[Discount]).

Key Steps:
a. Close the Movie main form (frmMoveiRentalMain).
b. Open the Rental subform (frmMovieRentalSub).
c. Add a textbox to the Rental Subform (frmMovieRentalSub). (Do you notice that it is an
Unbound Control?)

Unbound
control

d. Enter the expression = [Duration] * [Rate] * (1-[Discount]) in the Control Source of


the new textbox. You may also use Expression Builder to enter the expression.
label

Calculated
control

e. Switch to Form View to check if the expression is correct. Modify the expression if you
see “#Name?” displayed in the Charge textbox.
f. Remove the label of the new textbox (in the Details section) and add a new label,
“Charge”, in the form header (as shown above) and align it with other labels.

Questions:

6. What are the differences between “Bound Control” and “Calculated Control”?

7. Are there any other ways to add the Charge field to the form?

- end of document -

COMM 335 Winter 2019 Term 1 Tutorial 4 Page 5


Copyright © 2019 Y.M.Cheung

You might also like