Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

COMM 335 – Winter 2019 Term 1

Tutorial 5: Microsoft Access – Combo Boxes and Two-Levels of Subforms

The purpose of this tutorial is to enable you to:

1) create a (Type III) combo box to search a record on a form;


2) create a (Type II) combo box to update/add a record with predefined values;
3) build a main form with two levels of subforms; and
4) add command buttons to a form.

You should download the sample database, comm335_T5.accdb, from the UBC Canvas as the
foundation to build and test the combo box and forms in this tutorial.

 Creating a (Type III) Combo Box to Search a Record on a Form

A combo box is a list of values from which users can select a single value. First, we will learn how to
create a combo box to find a record on a form (i.e. for searching only.) Then we will learn how to
use a combo box to look up a value and store the selected value in a field (i.e. for updating.)

Demonstration:

 Add a combo box in the Customer form (frmCustomer) to search a customer by last name
and first name and sort the entries alphabetically.
incl. Last Name and First Name (and
Member No.) even though only Last
Name is displayed.

Key Steps:
a. Open the Customer form (frmCustomer) in
Design view.

b. Select Design  Combo Box control.


c. Move the mouse to where you want to put
the combo box and click it (the Combo Box
Wizard window pops up).
d. Select the 3rd option, i.e. “Find a record on
my form based on the value I selected in
my combo box”.
e. Choose the field(s) by which you want to
search, i.e. the primary key, MemberNo,
and the field(s) that you want to display in
the combo box, i.e. LName and FName.

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 1


Copyright © 2019 Y.M. Cheung
Key Steps (cont’d):
f. Notice that the “Hide key column”
checkbox is checked.

(Note: What if this checkbox is not


displayed? You may adjust the column width
in the combo box property sheet to hide the
primary key. This will be discussed later.)

g. Adjust the width of the Last Name and


First Name columns if necessary

h. Provide a meaningful label for the combo


box, e.g. “Search by Customer Name:”.

i. Click Finish. Test the combo box in Form


View. Does it work? Last First
Name Name
How many columns are there in this combo
box? How many columns can you see?

j. Switch to Design View. Open the Property


Sheet.

Questions:

1. What are the column widths?

2. Why is it Unbound? What is the Control Source of a Type III combo box?

3. What is Row Source?

4. Why do we need to include MemberNo (key column) in the combo box?

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 2


Copyright © 2019 Y.M. Cheung
Key Steps (cont’d):

k. To sort the entries shown in the


combo box, you have to update the
Row Source property:
 Select Row source, click “…”
to open the underlying query;
 Set the sort criteria for LName
and FName to “Ascending”;
and

 Close the query builder and


save the changes.

l. Test the combo box. Does it work?

Practice:
 Add a combo box in the Movie Rental form (frmMovieRentalMain) to search a movie/video
by movie title and format. Sort the entries in the combo box in ascending order. (Hint: Make
sure that the key column is included.)

Questions:

5. Which key column should be included in the combo box? Why?

6. How to hide the key column?

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 3


Copyright © 2019 Y.M. Cheung
 Creating a (Type II) combo box to update/add a record with
predefined values

To create a combo box that looks up values and updates a record, you need to decide two things:

 Where will the rows for the list come from? (Row Source)
 When a user selects an item in the combo box, what will Access do with that value?
(e.g. Store that value in a field.)

The rows for a combo box can come from:

 a fixed list of values that you enter when you create a combo box (use when the list won't change
very often), e.g. a combo box that contains entries for Mr., Mrs., or Ms. (Type II)
 a table or query (use when you'll frequently update the list), e.g. a combo box that displays a
list of customers from the Customer table so that users can pick a customer and add him/her to
the New Rental Form. (Type I)

Type III

Type I

Type II

In this tutorial, we will look at how to use a combo box to look up a list of predefined value and
store the selected value in a field, i.e. Type II combo box. The Type I combo box will be covered
in the next tutorial.

For Type II combo boxes, the rows for a combo box (Row Source) come from a fixed list of values
that you enter when you create a combo box.

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 4


Copyright © 2019 Y.M. Cheung
Demonstration:
 Add a combo box in the Customer Form (frmCustomer) to enable users to select the value of
the Customer Type field from a list of pre-defined values: “PLATINUM”, “GOLD”, and
“REGULAR”. Set the default value to “REGULAR”.

Key Steps:
a. Open the Customer form (frmCustomer)
in Design view.

b. Select Design  Combo Box.


c. Click where you want to place the combo
box.
d. In the Combo Box wizard dialog box,
select the 2nd option “I will type in the
values that I want” and click Next.

e. Add values: “GOLD”, “PLATINUM”,


and “REGULAR” to the column (Col1),
and then click Next.

f. Select “Store that value in this field”


and choose “CustType”, and then click
Next.

g. Provide a name for the combo box, e.g.


“Customer Type”, and then click Finish.
h. Test the combo box in Form View. Does
it work?

i. Switch to Design View. Open the


Property Sheet.

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 5


Copyright © 2019 Y.M. Cheung
Questions:

7. What is the Control Source of this combo box?

8. What is the Row Source of this combo box?

9. What is the Row Source Type?

Key Steps (cont’d):

j. Set the default value property to “REGULAR”.

k. Replace the “Customer Type” textbox with this combo box.

l. Align the combo box with other textboxes. (Is there an easy way to align the form
controls?)

m. Change the Tab Order accordingly. (Hint: Design  Tab Order.)

n. Test the combo box in Form View. Add a new record. Test the tab order. Does it work?

Practice:

 Add a combo box in the Customer Form (frmCustomer) to enable users to select the value of
the Province field from a list of pre-defined values, e.g. “AB”, “BC”, “MB”, “NB”, etc., and
set the default value to “BC”.
 

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 6


Copyright © 2019 Y.M. Cheung
 Creating a Main Form with Two Levels of Subforms

You learnt how to create a main form with a subform to display data from related tables with a one-to-
many relationship. The following shows you how to create a main form with two levels of subforms.

The main (Movie) form has a one-to-many relationship with the first (Video) subform which displays
all the videos (in different formats) of one movie. The Video subform in turn has a one-to-many
relationship with the second (Rental) subform which displays all the rentals for the selected video.

When you click on a different video format (for the same movie) in the Video subform, the Rental
subform will refresh automatically to show all the rentals for all copies of the selected video.

Demonstration:

 Create a set of forms to show:

1) all the movies the store carries (Movie Main Form)


2) the corresponding video information (Video Subform)
Movie Main Form
3) the corresponding rental transactions (Rental Subform)

Video
Subform

Rental
Subform

Calculated
control

To save time, we may create these forms directly from tables. Please take note of the
differences between this method and the method you learnt last week.

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 7


Copyright © 2019 Y.M. Cheung
Key Steps:
a. Create a set of forms (with two levels of subforms) using the Form Wizard and include
the following fields:
1) from tblMovie: MovieNo, Title, CategoryCode, Rating, Length;
2) from tblVideo: VideoNo, Format, NoOfCopies; and
3) from tblRental: CopyNo, DateOut, DateIn, DueDate, Rate.
b. Choose to view data by tblMovie.

c. Select the Tabular layout (i.e. Continuous


Forms View) for both subforms.
d. Select any style you like.

e. Save the main form as “frmVideoRentalMain”


and the two subforms as “frmVideoRentalSub1”
and “frmVideoRentalSub2”.

f. Test the form to make sure that:


 when a video is selected in the Video
subform (frmVideoRentalSub1), the Rental
subform (frmVideoRentalSub2) should
automatically display the rental information
for the selected video. Does it work?

Questions:
10. What are the Record Sources of the main form and the two subforms created above?

11. What are the differences between this method (i.e. creating forms directly from tables) and
the method you learnt earlier (i.e. creating forms from queries)?

Movie Main Form Video Subform Rental Subform


(frmVideoRentalMain) (frmVideoRentalSub1) (frmVideoRentalSub2)

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 8


Copyright © 2019 Y.M. Cheung
 Adding Command Buttons

Demonstration:

 Add a command button to display the next movie/video record in the Movie Main Form
(frmMovieRentalMain).

Key Steps:
a. Open the Movie Main Form (frmMovieRentalMain) in Design view.

b. Select Design and Command Button control (in the ribbon).


c. Click where you want to place the button.
d. In the Command Button wizard dialog box, choose “Record Navigation” under
Categories and “Go To Next Record” under Actions.
e. Choose a Picture (or Text) for the button.
f. Hide the navigation buttons for the main form (update the form property).
g. Save and test the form. Does it work?

Practice:
 Add the following command buttons in the Movie Main Form (frmMovieRentalMain):
1. to display the previous record, i.e. “Go To Previous Record”; and
2. to close the form.

The buttons may look like the following:

Questions:

1. Do we want to have a Delete button in the Main Form? Why?

2. How to create a command button to Save a record in the Subform?

– end of document –

COMM 335 Winter 2019 Term 1 Tutorial 5 Page 9


Copyright © 2019 Y.M. Cheung

You might also like