Calculate a value

Microsoft Office Access 2003

  1. Open a form or report in Design view.
  2. Click the Text Box tool Button image in the toolbox.
  3. In the detail section, click where you want to place the text box.
  4. Do one of the following:

    Place the insertion point in the text box and type an expression that calculates a total.

    Select the text box, click Properties Button image on the toolbar, and type the expression in the ControlSource property box. In a Microsoft Access database (.mdb), to use the Expression Builder to create the expression, click the BuButton imageild button next to the ControlSource property box.

Notes

  • If you need more room to type an expression in the ControlSource property box, press SHIFT+F2 to open the Zoom box.
  • If a form or report is based on a query, you may want to put the expression in the query instead of in a calculated control. It may improve performance, and if you are going to calculate totals for groups of records, it's easier to use the name of a calculated field in an aggregate function.

ShowIn a data access page

If you want to do complex calculations using spreadsheet functions, use the Microsoft Office Spreadsheet Component. For all other calculations, you can use the bound span control.

ShowUsing the Spreadsheet Component

  1. Open the data access page in Design view.
  2. If they aren't already on the page, add the controls that will contain the data you want to use in the calculation.
  3. Click the Office Spreadsheet tool Office Spreadsheet control in the toolbox, and then click where you want the upper-left corner of the spreadsheet to be on the data access page.
  4. In any cell in the spreadsheet, enter a formula that uses the HTML Host function to refer to the fields used in the calculation, and the specific operation you want to perform on the field's values expressed as a combination of operators, identifiers, and values.

    Example    To multiply the value in the Quantity field by the value in the UnitPrice field, type:

    =Host().Quantity.value*Host().UnitPrice.value

    Example    To multiply the value in the UnitPrice field by a percentage rate stored in cell C1, type:

    =$C$1*Host().UnitPrice.value

Notes

  • To see the name of a spreadsheet, select the control, and then click Properties Button image on the toolbar. The property sheet title bar displays the spreadsheet name.
  • You can use spreadsheet functions, which, in Microsoft Access, are available only in this control.
  • Most likely, you'll want the calculated value to display in an area that looks like the other controls on the data access page. To do this you can modify the appearance of the spreadsheet so it looks like a text box.

ShowUsing the bound span control

  1. Open the data access page in Design view.
  2. If the expression will reference field names, the fields must be in the page's data definition. Either add the fields to the page as bound span controls, or add the fields as bound text boxes.

    ShowTip

    The page will load faster in Page view and in Microsoft Internet Explorer if you use bound span controls.

  3. Create another control by clicking the Bound Span tool Bound HTML control or the Text Box tool Button image in the toolbox, and then click where you want the upper-left corner of the control to be on the page.

  4. To display the property sheet, make sure that the control is selected, and then click Properties Button image on the toolbar.
  5. In the ControlSource property box, type an alias and an appropriate expression. To reference fields on the page, use the names of the fields in the expression. For example, if you want to compute the extended price for a record in the Order Details table, type:

    ExtendedPrice: UnitPrice*Quantity

    If you don't specify an alias, you must type an equal sign (=) before the expression. Microsoft Access will automatically supply an alias— for example, Expr1.