Calculated fields in Access queries

A query can show more than stored fields: it calculates. A sale price, the total of an order line, or a status that follows from the stock level.

In short: in an empty column of the Field row write Name: expression, for example Sale: [UnitPrice]*0.9. Field names go in square brackets. The value is recalculated every time the query runs.

Before you start: Dates and empty fields.

Try it now

Task Create a price list with ProductName, UnitPrice and a calculated field Sale: the unit price with 10% off (times 0.9).

Products 18 rows, 6 columns
ProductIDProductNameCategoryIDUnitPriceUnitsInStockDiscontinued
1Blue ballpoint pen11.20121☐
2Highlighter 4-pack13.9076☐
3Hole punch18.50237☐
4Stapler112.9061☐
5Lever arch file12.8076☑
6USB drive 64 GB29.90193☐
7Wireless mouse219.90157☐
8Keyboard234.90216☐
927-inch monitor2229.00129☐
10Headset249.0047☐
11Office chair3189.00135☐
12Desk3349.0046☐
13Mobile pedestal3129.00105☐
14Copy paper A444.5035☐
15Notepad41.90122☐
16Envelopes 100-pack46.9093☐
17Labels47.5075☐
18Laminating pouches411.90211☐

How a calculated field is built

Before the colon comes the name of the new column, after it the expression. Without a name Access picks one itself: Expr1, Expr2 and so on. The expression can calculate (+ − * /), join text (&) and call functions.

In the gridSQL view
Sale: [UnitPrice]*0.9[UnitPrice]*0.9 AS Sale
FullName: [FirstName] & " " & [LastName][FirstName] & " " & [LastName] AS FullName
Status: IIf([UnitsInStock]<50,"reorder","ok")the same
Disc: Nz([Discount],0)the same

A calculated field may use another one from the same query: next to Sale: [UnitPrice]*0.9, Saving: [UnitPrice]-[Sale] keeps working with the result. As a criterion or sort of another field, however, Access does not know the name Sale and asks for a parameter value.

Combining several fields

Task For each order line show OrderID, Quantity, SalePrice and the line total (quantity times sale price).

OrderDetails 321 rows, 5 columns
DetailIDOrderIDProductIDQuantitySalePrice
11121.20
221757.50
32122349.00
428334.90
53328.50
64689.90
74348.50
847719.90
9410149.00
105181.20
1154212.90
1251444.50
135121349.00
1461424.50
156131129.00
1677219.90
1778734.90
188572.80
199582.80
209328.50
21108134.90
2210233.90
2310191.20
24111511.90
2511263.90
2611132129.00
271110149.00
2812111189.00
2913131129.00
30131616.90
3114132129.00
3214542.80
33144312.90
34156109.90
35164712.90
36161444.50
37163108.50
381691229.00
39177619.90
40181541.90
41187119.90
421992229.00
4319131129.00
44204612.90
4520582.80
4620122349.00
472017107.50
48218834.90
4921562.80
50217419.90
51211571.90
5222388.50
532392229.00
5423213.90
552491229.00
5624669.90
5724233.90
58244612.90
59257619.90
60251474.50
612515101.90
6226552.80
632617107.50
642692229.00
65271474.50
66274912.90
6728689.90
68287919.90
69291444.50
70297619.90
7130552.80
723110149.00
73311747.50
743192229.00
75321484.50
7632111189.00
773292229.00
78331737.50
7933121349.00
8034111.20
81351531.90
82358934.90
8336233.90
84361561.90
85368134.90
86367519.90
8737253.90
883791229.00
89381727.50
9038131129.00
9138619.90
9239131.20
933910149.00
94401454.50
954110149.00
96411571.90
9741112189.00
98411727.50
9942213.90
100433108.50
1014317107.50
102431484.50
1034410249.00
104448534.90
105451686.90
10645328.50
107455102.80
10846512.80
109467519.90
110461797.50
11147572.80
11247121349.00
1134710249.00
114473108.50
11548132129.00
116488134.90
11748121349.00
1184841012.90
119498434.90
120491541.90
12149293.90
12249141.20
123501444.50
124504112.90
12550131129.00
12651619.90
12751243.90
12851121349.00
129517719.90
130521727.50
131528134.90
13252358.50
133522103.90
13453132129.00
13553348.50
13653582.80
13754111189.00
138541797.50
13954293.90
1405410249.00
141558934.90
14256679.90
14356112189.00
144571616.90
145574412.90
14657112189.00
14757131129.00
148581656.90
149591571.90
15059111189.00
151594612.90
152608834.90
153601521.90
154611727.50
15561293.90
15661582.80
157624312.90
158621767.50
15963112189.00
160631521.90
161647219.90
162654512.90
16365572.80
164651777.50
1656692229.00
16666121349.00
1676615101.90
16867132129.00
1696792229.00
170674112.90
171681444.50
172681747.50
17368619.90
174698134.90
175707319.90
176701797.50
177701676.90
17870348.50
179711581.90
180717319.90
1817191229.00
18272121349.00
183721434.50
18472131129.00
18572542.80
18673273.90
18773122349.00
188731424.50
1897410249.00
190741484.50
19175112189.00
1927515101.90
19375132129.00
194764812.90
195761676.90
196778634.90
19777121349.00
198781494.50
19978542.80
200781636.90
201795102.80
2028010249.00
203807419.90
20480112189.00
20581122349.00
2068181034.90
2078210249.00
208821551.90
20982318.50
21082649.90
211831474.50
212831551.90
21384689.90
214848134.90
2158491229.00
21684112189.00
217851797.50
21885111189.00
21985121349.00
22085131129.00
221861511.90
22286582.80
223867219.90
22487233.90
225877119.90
22687111189.00
227874512.90
228888734.90
229881511.90
23088629.90
231897619.90
23290122349.00
233901656.90
23490131.20
2359015101.90
236911561.90
2379181034.90
238927619.90
2399216106.90
24093669.90
24193131129.00
242934912.90
24394522.80
24495328.50
2459610249.00
24697112189.00
24797669.90
24897358.50
24998111189.00
25098181.20
2519810149.00
25298659.90
253991646.90
254997519.90
255994412.90
256991777.50
2571007219.90
2581008134.90
2591001727.50
260100669.90
261101388.50
262101121349.00
263101243.90
264102121349.00
265102132129.00
2661027719.90
267102398.50
268103512.80
2691041494.50
270104112189.00
2711041521.90
2721047219.90
273105132129.00
2741058734.90
2751054812.90
2761051676.90
2771067319.90
278107233.90
27910881034.90
2801081531.90
2811087419.90
282108111189.00
283109131.20
2841094212.90
2851091511.90
2861092103.90
2871101666.90
2881104912.90
289110368.50
29011091229.00
291111121349.00
2921111531.90
2931111727.50
294112121349.00
29511210249.00
296112223.90
2971131717.50
29811315101.90
29911392229.00
3001134912.90
3011143108.50
302114532.80
303114273.90
3041148234.90
3051151727.50
3061153108.50
307116659.90
30811691229.00
3091171636.90
3101178934.90
311117171.20
3121171757.50
3131181464.50
314118659.90
315118112189.00
316119639.90
3171191777.50
318119131129.00
3191201777.50
320120542.80
3211204512.90

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Conditions with IIf

IIf(condition, if true, if false) works like IF in Excel.

Task Show ProductName, UnitsInStock and a field Status: “reorder” if stock is below 50, otherwise “ok”.

Products 18 rows, 6 columns
ProductIDProductNameCategoryIDUnitPriceUnitsInStockDiscontinued
1Blue ballpoint pen11.20121☐
2Highlighter 4-pack13.9076☐
3Hole punch18.50237☐
4Stapler112.9061☐
5Lever arch file12.8076☑
6USB drive 64 GB29.90193☐
7Wireless mouse219.90157☐
8Keyboard234.90216☐
927-inch monitor2229.00129☐
10Headset249.0047☐
11Office chair3189.00135☐
12Desk3349.0046☐
13Mobile pedestal3129.00105☐
14Copy paper A444.5035☐
15Notepad41.90122☐
16Envelopes 100-pack46.9093☐
17Labels47.5075☐
18Laminating pouches411.90211☐

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Empty values with Nz

If an expression uses an empty field, the result is empty: [UnitPrice]*[Discount] gives Null when Discount is missing. Nz([Discount],0) puts 0 in place of Null. Nz exists only in Access itself, not in other databases.

In real Access

  1. In Design view, click the Field row of an empty column.
  2. Type the expression, or right-click, Build, to open the Expression Builder. It lists fields and functions to click.
  3. For a number format (such as Currency) right-click the column, Properties, Format.

Other ways

Tables offer a Calculated data type that stores an expression permanently. It can only use fields of the same table and is more awkward to change. For reports and lists, the calculated field in a query is the usual way.

Common mistakes

Typo in a field name

If Access does not know a name, it asks for a parameter value. Check the spelling; square brackets around field names keep the expression clear.

Name equal to an existing field

UnitPrice: [UnitPrice]*0.9 causes a circular reference error. Choose a new name such as Sale.

FAQ

Can I sort or filter on a calculated field?

Yes, like on any field: sort and criterion go in the same column. The level project uses this.

Is the calculated value stored?

No. It is created fresh each time the query runs, so it is always up to date.

Learn next: parameter query

A query that asks for the value when it opens.

Parameter query

Write to us

Choose language

Select your preferred language