Professional Documents
Culture Documents
Creating A Calculated Field in A Main Form To Add Up The Total of A Field in A Subform
Creating A Calculated Field in A Main Form To Add Up The Total of A Field in A Subform
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.
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
(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.)
txtSumCharge
txtSubtotal
l. Click OK to close the Expression Builder. The expression will be displayed in the textbox.
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?
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.
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:
=[txtSubTotal]*DLookUp("[TaxRate]","tblTax","[TaxType]='GST' ")
Question:
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:
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?)
(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 -