Lessons 32 · 1 lessons · N. I. Taylaqov (general editor), A. B. Akhmedov, M. D. Pardayeva, A. A. Abdug‘aniyev, U. M. Mirsanov. Informatics and Information Technologies, Grade 10, 1st edition. “Extremum-Press”, Tashkent, 2017
32
Performing mathematical operations in MS Access
Textbook: pp. 71–75
GoalCreates a calculated field in a query, uses arithmetic operations and the functions Sqr, Abs, Int, Log and IIf, and calculates totals by group.
New words
calculated field: a query column whose value is computed from other fields · hisoblanadigan maydonexpression: operands, operators and functions that give one value · ifodaIIf: the Access function that chooses one of two values by a condition · IIfGroup By: the Totals option that forms groups of equal values · Group By
Explanation
In an Access query you can calculate a new value from the fields of a table: in the Field row write “Name: expression”, for example Total: [Price]*[Qty], with field names in square brackets. The arithmetic operations are + – * /, power ^, integer division \ and remainder Mod; among the functions are Sqr(x) – square root, Abs(x) – absolute value, Int(x) – integer part (rounding down), Sin, Cos, Tan and Log(x). Note: in Access, Log gives the natural logarithm (base e ≈ 2.718), not the base-10 logarithm; so Log(1) = 0. To choose by a condition, use IIf(condition, value1, value2), for example IIf([Mark]>=3,"Pass","Fail"); depending on the local settings the arguments are separated by a comma or a semicolon. For calculations by groups, press Σ Totals in Query Design, choose Group By for the group field and Sum, Avg, Min, Max or Count for the calculated field. Because the decimal separator depends on settings, a form such as [Price]*12/100 is convenient for percentages.
Worked examples
An Orders table: Item, Price (so‘m), Qty. Notebook: Price = 5000, Qty = 6; Total: [Price]*[Qty] = 30000. With 12% VAT: Total2: [Price]*[Qty]*112/100 = 30000*112/100 = 33600. Price and Qty in the table do not change: Total is only a calculation in the query.
From the marks in the Students table: 10A has 5, 3, 5, 4, 5 (sum 22, Avg = 4.4); 10B has 4, 5, 3 (sum 12, Avg = 4). In a Totals query with Class – Group By and Mark – Avg, two rows appear. IIf([Mark]>=4,"good","low") gives good, low, good, good, good for 5, 3, 5, 4, 5.
Class activity
“The shop query”: a pair builds a 5-product table, calculates Total and the amount with VAT, adds a cheap/expensive column with IIf and checks the results with a calculator.
Practice
1
If the price is 12000 and the quantity is 3, what is Total: [Price]*[Qty]?
36000
2
With 12% VAT added to this amount, what is the total?
40320
3
Write the values of Sqr(144) and Log(1).
12 and 0
4
Why does the calculated field Total not change Price and Qty in the table?
The query only displays the result; the data in the table stays unchanged.