You are on page 1of 6

COMM 335 – Winter 2019 Term 1

Tutorial 7: Microsoft Access – Calculated Controls, DLookup function


and Event Driven Programming

The purpose of this tutorial is to enable you to:

1) create a calculated field in a main form to add up the total of a field in a subform;
2) use DLookup function to look up the value of a field in another table; and
3) create a simple event procedure to update the combo box.

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

 Creating a Calculated Field in a Main Form to Add up the


Total of a Field in a Subform
Demonstration:

 Add a calculated control “Subtotal” in the Customer Rental Main Form (frmCustRentalMain)
to calculate the total rental charges listed in the Subform.

frmCustRentalMain

frmCustRentalSub

frmCustRentalMain

Calculated fields
on the Main Form

COMM 335 Winter 2019 Term 1 Tutorial 7 Page 1


Copyright © 2019 Y.M.Cheung
Key Steps (Part 1):

a. Open the Customer Rental Subform (frmCustRentalSub) in Design View.


b. Create a textbox in the Form Header section as shown below. frmCustRentalSub

c. Open the property sheet of the textbox.


d. Type the following expression in the “Control Source” property as shown below.
Alternatively, you may type the expression in the textbox.
= Round(Sum([Charge]),2)

(Note: Use the “Round” function to return a number rounded to a number of decimal places.
In this case, it is rounded to 2 decimal places.)
e. Change the name of the textbox to txtSumCharge in the Textbox property sheet.

f. Save and open the Subform. You should see the total charge displayed in the textbox as
shown below. Does it work? (Correct any typo errors if you see #Error in the textbox.)

COMM 335 Winter 2019 Term 1 Tutorial 7 Page 2


Copyright © 2019 Y.M.Cheung
Key Steps (Part 2):

g. Open the Rental Contract Main Form (frmCustRentalMain) in Design View.


h. Create a new textbox in the Detail section of the Main Form (below the Subform).

txtSumCharge

txtSubtotal

i. Open the property sheet of the textbox.


j. Select the “Control Source” property of the textbox and click the “…” button.
k. In the Expression Builder Window:
1) Expand the form frmCustRentalMain in the Expression Elements box;
2) Select the subform frmCustRentalSub;
3) In the Expression Categories box (middle), scroll down and double click on
txtSumCharge (i.e. the textbox created in the subform’s header);
4) The complete expression should look like:
=[frmCustRentalSub].[Form]![txtSumCharge]

l. Click OK to close the Expression Builder. The expression will be displayed in the textbox.

m. Change the label to “Sub Total:”.

COMM 335 Winter 2019 Term 1 Tutorial 7 Page 3


Copyright © 2019 Y.M.Cheung
Key Steps (Part 2):

n. Change the name of this textbox to


“txtSubtotal”.

o. Change the format of this Subtotal textbox


to “Currency”.

p. Save and open the form. Does it work? (Correct any typo errors if you see #Error in the
textbox.)

q. Change the ‘visible’ property of the textbox “txtSumCharge” in the subform header to No to
make it “invisible”.

Question:
1. Why do we need to create an ‘invisible’ field in the Subform to calculate the Subtotal?

COMM 335 Winter 2019 Term 1 Tutorial 7 Page 4


Copyright © 2019 Y.M.Cheung
 Using DLookup Function

Use the DLookup function to look up the value of a field in another table that is not included in the
form. This function can be used when writing expressions in a form or macro.

Syntax: DLookup ( "[field name]", "table name", "criteria" )

Demonstration:

 Add a calculated control “GST” to the Customer Rental form (frmCustRentalMain). It should
display the GST based on the value in the “Subtotal” field and the tax rate in the “tblTax” table.

Key Steps:

a. Create a textbox below the SubTotal field in the main form.


b. Open the property sheet of the textbox.
c. Select the “Control Source” property of the textbox. Right click and then select “Zoom..”
option from the menu. (This is just another way of entering an expression. You may click the
‘…’ button to open the Expression Builder.)

d. Type the following expression:

=[txtSubTotal]*DLookUp("[TaxRate]","tblTax","[TaxType]='GST' ")

e. Click OK to close the window.


f. Change the name of the textbox to “txtGST” and change the format to “currency”.
g. Save and open the form. Does it work? (Correct any typo errors if you see #Error in the
textbox.)
h. Change the label to “GST:”.

Question:

2. When can we use the DLookUp function?

COMM 335 Winter 2019 Term 1 Tutorial 7 Page 5


Copyright © 2019 Y.M.Cheung
 Creating an Event Procedure
Demonstration:

 Create an event procedure in the Customer Form (frmCustomer) to automatically update the
combo box when records are selected using the navigation buttons.

Key Steps:

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


b. Check the name of the “Customer” combo box, e.g. Combo45. Do not rename it. (Otherwise
the combo box might fail.)
c. Open the Form property sheet.
d. On the Event tab, click the On Current event.
e. Click the Build button to open the “Choose Builder” dialog box.

f. Select Code Builder and click OK.


g. A VBA window will open containing the module for the frmCustomer form.
h. You should see a line beginning Private Sub Form_Current() and the line ending End Sub.

i. Type Combo45 = MemberNo between the Private Sub and End Sub statements.
(Note: Always specify the key field or the 1st column of the combo box in the statement. Do
you know why?)

j. Click the Save button and close the VBA window.


k. Open the form and check if the entry in the combo box “follows” the Customer Name in the
form when you navigate back and forth among the records. Does it work?

(Note: The technique used in this exercise is the only way to create an event procedure. If you
create the procedure directly in the module window, it will not create the necessary association
between the event and the code.)

- end of document -

COMM 335 Winter 2019 Term 1 Tutorial 7 Page 6


Copyright © 2019 Y.M.Cheung

You might also like