Published by Patrick Mutisya · 14 days ago
Be able to use arithmetic operations or numeric functions to perform calculations, including the creation of calculated fields and calculated controls in a database.
Most database programs support the four basic arithmetic operators:
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | \$5 + 3\$ | 8 |
| - | Subtraction | \$10 - 4\$ | 6 |
| * | Multiplication | \$7 * 2\$ | 14 |
| / | Division | \$20 / 5\$ | 4 |
| % | Modulo (remainder) | \$13 % 5\$ | 3 |
| Function | Purpose | Syntax (example) | Result |
|---|---|---|---|
| ABS() | Absolute value | ABS(-12) | 12 |
| ROUND() | Round to a specified number of decimal places | ROUND(3.678, 2) | 3.68 |
| INT() | Integer part of a number (truncates decimals) | INT(7.9) | 7 |
| CEILING() | Smallest integer greater than or equal to the number | CEILING(4.2) | 5 |
| FLOOR() | Largest integer less than or equal to the number | FLOOR(4.9) | 4 |
| POWER() | Raise a number to a power | POWER(2, 3) | 8 |
| SQRT() | Square root | SQRT(25) | 5 |
| MOD() | Modulo – remainder after division | MOD(17, 5) | 2 |
| LOG() | Logarithm (base 10) | LOG(100) | 2 |
| EXP() | e raised to a power | EXP(1) | 2.71828… |
A calculated field stores the result of a formula that uses values from other fields in the same record. The field itself is not edited directly; its value updates automatically whenever the source fields change.
CalculatedField = Expression
Example – total price of an order line:
\$\text{TotalPrice} = \text{UnitPrice} \times \text{Quantity}\$
UnitPrice, Quantity, Discount%.NetAmount with the expression:\$\text{NetAmount} = (\text{UnitPrice} \times \text{Quantity}) \times \bigl(1 - \frac{\text{Discount\%}}{100}\bigr)\$
UnitPrice = 15.00, Quantity = 4, Discount% = 10 then:\$\text{NetAmount} = (15.00 \times 4) \times (1 - 0.10) = 60.00 \times 0.90 = 54.00\$
Calculated controls are similar to calculated fields but are placed on a form or report. They show a result that may combine data from several records or perform aggregate calculations.
Assume a form lists all line items for a single invoice. Add a control named txtRunningTotal with the expression:
\$\text{RunningTotal} = \sum{i=1}^{n} (\text{UnitPrice}i \times \text{Quantity}_i)\$
The control updates each time a new line is added or an existing line is edited.
Field: DateOfBirth (date type). Calculated control txtAge uses:
\$\text{Age} = \text{INT}\!\bigl(\frac{\text{Today()} - \text{DateOfBirth}}{365.25}\bigr)\$
This returns the whole number of years between today and the stored birth date.
TotalCost).=[UnitPrice]*[Quantity]
/. Use a conditional expression such as:IIf([Divisor]=0, 0, [Numerator]/[Divisor])
ROUND() to limit decimal places, especially for currency.VAL() if necessary.Mark1, Mark2, Mark3. Write the expression for a calculated field Average that rounds the result to one decimal place.HourlyRate and HoursWorked. Create a calculated control that also adds a 12% tax to the gross pay.Score/MaximumScore.