Part I: Access Building Blocks
316
Tip
You can select noncontiguous fields in the list by clicking each field while holding down the Ctrl key. The
selected fields can be dragged (as part of the group) to the form’s design surface.
Creating a Calculated Control
Unbound controls may use an expression as their ControlSource property. As the form loads,
Access evaluates the expression and populates the control with the value returned by the expres-
sion. The following example demonstrates creating an unbound calculated control:
- Select tblProducts in the Navigation Pane.
- Click the Create tab on the ribbon, and then click on the Blank Form command in the
Form group to display a new form in Layout view. - Drag Cost and SalePrice from the Field List onto the form’s surface.
- Switch the form to Design view.
- Click on Text Box in the Controls group and draw a new text box on the form.
- Set the Name property to txtProfit and set its Control Source property to:
=[SalePrice]-[Cost]. - Change the Format property to Currency and its Decimal Places to 2.
- Change the label’s Caption property to Profit:.
- Switch to Form view to test the expression.
Your screen should look like Figure 8.16. txtProfit shows the difference between the
SalePrice and Cost.
FIGURE 8.16
Creating a calculated control