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

COMM 335 – Winter 2019 Term 1

Tutorial 6: Microsoft Access - Building Combo Boxes and Compacting Database File

The purpose of this tutorial is to enable you to:

1) create a (Type I) combo box to look up the values from a table/query and add the selected value
to a record;
2) create synchronized combo boxes; and
3) compact the database file.

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

A combo box is a list of values from which users can select a single value. In the last tutorial, you have
learnt how to create combo boxes to find a record on a form and update/add a record with
predefined values. In this tutorial, we will look at how to use a combo box to look up a value from a
table/query and store the selected value in a field.

Type I and Type II Combo Boxes



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

COMM 335 Winter 2019 Term 1 Tutorial 6 Page 1


Copyright © 2019 Y.M. Cheung
Creating a (Type I) combo box to look up values from a

table/query and add the selected value to a record
Demonstration:

 Create a new form New Rental Form and build two combo boxes for the purpose of adding a
new video rental contract for a customer. Sort the combo box entries in Ascending order.

Type I

Key Steps:

a. Create the form using the query, qryNewRental that has already been created for you.
Choose the Columnar layout and save the form as frmNewRental.

b. Add a form header, e.g. “New Rental Form”, but don’t worry about the field alignment
and tab order for the time being.

c. Set “Data Entry” of the form property to “Yes”. (What do you see when the form opens?
Don’t forget this step!)

d. Select Design  Combo Box control. Click where you want to place the combo
box.

e. In the Combo Box wizard dialog box, select the 1st option “I want the combo box to get
the values from another table or query”. Click Next.

f. Choose the table tblCustomer (Row Source for this combo box.) Click Next.

g. Choose the field(s) that you want to display in the combo box, i.e. Last Name and First
Name (the primary key MemberNo field is selected automatically as this is the value that
will be stored in a new record). Click Next.

COMM 335 Winter 2019 Term 1 Tutorial 6 Page 2


Copyright © 2019 Y.M. Cheung
Key Steps:

h. Select the sort order (Ascending) for Last Name and First Name fields. Click Next.

i. Notice that the “Hide the key column” is checked. Click Next.

j. Select “Store that value in this field” and choose “tblRental.MemberNo”, and then
click Next. (Don’t choose tblCustomer.MemberNo. Do you know why? Refer to the
diagrams below. Without the combo box, how do you enter MemberNo into a new rental
record?)

k. Provide a meaningful label for the combo box, e.g. “Search by Customer Name:”.

l. Test the combo box. Does it work?

Row Source

Store that value


in this field.

Question:
1. Which table will be updated as a result of adding/updating a record using this combo box?

(A) tblCustomer (B) tblMovie (C) tblRental (D) tblVideo

Practice:
 Add a combo box in the New Rental Form (frmNewRental) to search a video by Copy No.

(Which table(s) and field(s) should be selected to build the combo box?)

COMM 335 Winter 2019 Term 1 Tutorial 6 Page 3


Copyright © 2019 Y.M. Cheung
 Creating Synchronized Combo Boxes
Demonstration:

 Modify the New Reservation Form (frmNewReservation) and add two combo boxes. The user
should be able to use the 1st combo box to select movie category, and use the 2nd combo box to
select movie title from a list of movies that belong to the category selected in the 1st combo box.

Key Steps:
a. Open the New Reservation Form (frmNewReservation) in design view.
b. Add a combo box to search for Category (choose Type I combo box, display
CategoryDesc from tblCategory table in the combo box, sort Category Description in
ascending order.)

c. Select “Remember the value for later use” and click Next.
d. Provide a meaningful label for the combo box, e.g. “Step 2 - Search for Category:”.
e. Open the property sheet of the combo box and rename the combo box as
“ComboCategory”.
f. Add another combo box to search for Movie Title (choose Type I combo box, display
Movie Title from tblMovie table in the combo box, sort Movie Title in ascending order.)
g. Select “Store that value in this field” and choose “tblVideo_MovieNo”, and then click
Next (Don’t choose the MovieNo in tblMovie.)
h. Provide a meaningful label for the combo box, e.g. “Step 3 - Search for Movie:”.

i. Open the Row Source property of the 2nd combo box.


j. Add the CategoryCode field into the underlying query, and add [ComboCategory] to the
criteria:

COMM 335 Winter 2019 Term 1 Tutorial 6 Page 4


Copyright © 2019 Y.M. Cheung
Key Steps:

k. Rename the 2nd combo box as “ComboMovie”.

l. On the “After Update” event of the 1st combo box (ComboCategory), click the “…”
button and then select Code Builder.

m. Type [ComboMovie].Requery between the Private Sub and End Sub statements.

n. Save the form and test the two combo boxes.

 You should notice that the Movie combo box is empty when you first open it.
 After you pick a category in the Category combo box, then the Movie combo box will
show all movies belonging to the selected category. This shows that your code is
working. Does it work?

Question:
2. Why do we need to select “Remember the value for later use” in Step (c) on page 4 above?

3. What is the purpose of the “Requery” statement in Step (m) above?

COMM 335 Winter 2019 Term 1 Tutorial 6 Page 5


Copyright © 2019 Y.M. Cheung
Compacting an Access database


If you delete data or objects in an Access database, the


Access database can become fragmented and use disk
space inefficiently. Compacting the Access database
makes a copy of the file and rearranges how the file is
stored on your disk. Hence, compacting optimizes the
performance of the Access databases.

Compacting can be done when the database is


opened or closed. Compacting a database when it is
opened saves the database under the same name.
Compacting a database when it is closed is safer. It is
because the compacted database is stored as a new file
(enabling you to return to the original file should
anything go wrong).

Demonstration:
 Compact the database when it is closed. Then Compress the compacted database.

Key Steps:
a. Close your database file.
b. Click the Database Tools and select
Compact and Repair Database.
c. Select the database file to compact.
d. Save the compacted database to a different
name.

e. Open the folder where the file is saved.


Check the file size of the new database file.

f. To compress the file, right click on the file


in the folder and select Send to 
Compressed (zipped) folder. Check the
file size of the zipped file/folder.

(You may use other Zip tools, e.g. WINZIP,


to compress the file.)

Question:
4. What is the difference between “Compact” and “Compress”?

- end of document -

COMM 335 Winter 2019 Term 1 Tutorial 6 Page 6


Copyright © 2019 Y.M. Cheung

You might also like