Case 6: adding training records and checking leave

After a safety briefing in the warehouse, the records should be added with one click, and a leave request that exceeds the remaining days should not be saved at all.

In short: an append query INSERT INTO TrainingRecords … SELECT … WHERE Department = 'Warehouse' adds everyone at once. In Form_BeforeUpdate, DLookup minus Nz(DSum(…)) gives the remainder; if the request is larger, Cancel = True stops the save.

Task 1: records for a department

Task On 1 Dec 2026 the whole warehouse passed the health and safety training (TrainingID 1). Write the procedure SafetyWarehouse: with CurrentDb.Execute and dbFailOnError it appends a record for every active employee of the Warehouse department (TrainingID 1, date 1 Dec 2026, Passed Yes) and then shows “x records added”.

Employees 40 rows, 10 columns
EmployeeIDEmployeeNoFirstNameLastNameDepartmentIDManagerIDHireDateTermDateWeeklyHoursLeaveDays
1E1001EllaHill123 Dec 20134025
2E1002OliviaTaylor2110 Sep 20134025
3E1003GraceWright3122 Nov 20104025
4E1004LiamWright413 Mar 20154025
5E1005AvaWilson519 Sep 20144025
6E1006DavidWilson5513 Dec 20132013
7E1007LiamEvans444 May 20124025
8E1008NinaClark5516 Jun 202411 Mar 20264025
9E1009LeoClark5512 Mar 20154025
10E1010SophiaJohnson4429 Nov 20254025
11E1011LiamTaylor228 Sep 20194025
12E1012ChloeDavies2214 Jun 20204025
13E1013HenrySmith222 Feb 20204025
14E1014MiaBaker554 Apr 20194025
15E1015JackYoung444 Mar 20174025
16E1016MiaWard554 Nov 20174025
17E1017LucasBrown2217 Jan 20163220
18E1018GraceTaylor4422 Jan 20194025
19E1019GraceKing3311 Sep 201630 Dec 20254025
20E1020MiaJohnson3321 Sep 20203220
21E1021NoahBrown3328 Jul 20154025
22E1022OliviaHill339 Sep 20102013
23E1023SamTaylor334 Jun 20224025
24E1024LiamKing558 Aug 20214025
25E1025OliverWard448 Dec 20134025
26E1026ChloeJohnson4418 Oct 20134025
27E1027EllaDavies3321 Nov 20114025
28E1028JackTaylor5510 May 20103220
29E1029OliverBrown2212 Jun 20123019
30E1030NoahHall4424 Jul 20214025
31E1031JackWalker5527 Mar 201427 Dec 20253019
32E1032GraceBaker557 Dec 20222013
33E1033OliverDavies446 Aug 20124025
34E1034SamDavies5511 May 20264025
35E1035EmmaDavies4420 Oct 20253019
36E1036MiaWright5516 May 20214025
37E1037LeoJohnson4421 Jun 20264025
38E1038DavidHughes558 Dec 20254025
39E1039EmmaWright443 Jul 20263220
40E1040ChloeWard333 Apr 20253220
Departments 5 rows, 3 columns
DepartmentIDDepartmentCostCenter
1Management100
2Sales200
3Warehouse300
4Administration400
5IT500
TrainingRecords 271 rows, 5 columns
RecordIDEmployeeIDTrainingIDCompletedOnPassed
12222 Jan 2024☑
23222 Jan 2024☑
39230 Jan 2024☑
42711 Feb 2024☑
51315 Feb 2024☑
61118 Feb 2024☑
733215 Feb 2024☑
811218 Feb 2024☑
914218 Feb 2024☐
10335 Mar 2024☑
1120111 Mar 2024☑
1221215 Mar 2024☑
1318216 Mar 2024☑
1422123 Mar 2024☑
156326 Mar 2024☑
164127 Mar 2024☑
1731129 Mar 2024☑
1832329 Mar 2024☐
193633 Apr 2024☑
20624 Apr 2024☑
2123311 Apr 2024☑
227313 Apr 2024☑
235215 Apr 2024☑
2424217 Apr 2024☑
2513230 Apr 2024☑
262232 May 2024☑
273236 May 2024☑
282027 May 2024☑
292939 May 2024☐
302318 May 2024☑
3126322 May 2024☑
3232123 May 2024☑
3315328 May 2024☑
3424328 May 2024☑
3521330 May 2024☑
3623230 May 2024☑
371222 Jun 2024☑
381734 Jun 2024☑
392938 Jun 2024☑
4014114 Jun 2024☑
414216 Jun 2024☑
4220316 Jun 2024☑
4325216 Jun 2024☑
4428218 Jun 2024☑
451320 Jun 2024☑
4614322 Jun 2024☑
475325 Jun 2024☑
4816325 Jun 2024☑
4922227 Jun 2024☑
503311 Jul 2024☑
51312 Jul 2024☑
521622 Jul 2024☑
53216 Jul 2024☐
54128 Jul 2024☑
552418 Jul 2024☑
5627212 Jul 2024☑
5726113 Jul 2024☑
589315 Jul 2024☑
5931221 Jul 2024☑
6016122 Jul 2024☑
6126226 Jul 2024☑
627230 Jul 2024☑
632831 Aug 2024☑
6417112 Aug 2024☑
6523112 Aug 2024☑
6617213 Aug 2024☑
6729216 Aug 2024☑
6831320 Aug 2024☑
698225 Aug 2024☑
709127 Aug 2024☑
711911 Sep 2024☑
723225 Sep 2024☑
733619 Sep 2024☑
7425111 Sep 2024☐
7519321 Sep 2024☑
7629123 Sep 2024☑
7712128 Sep 2024☑
7830228 Sep 2024☑
7933330 Sep 2024☑
80512 Oct 2024☑
81712 Oct 2024☑
821835 Oct 2024☑
831519 Oct 2024☑
8413311 Oct 2024☑
854317 Oct 2024☑
8625120 Oct 2024☑
8728121 Oct 2024☑
8812325 Oct 2024☑
892537 Nov 2024☑
9036219 Nov 2024☑
9130120 Nov 2024☑
9219116 Jan 2025☑
9330220 Jan 2025☑
9416127 Jan 2025☑
9513430 Jan 2025☑
9619218 Feb 2025☑
9736221 Feb 2025☑
981856 Mar 2025☐
992249 Mar 2025☑
1002419 Mar 2025☑
10122210 Mar 2025☑
10233116 Mar 2025☑
10313217 Mar 2025☑
10415221 Mar 2025☑
10511222 Mar 2025☑
10633226 Mar 2025☑
10727127 Mar 2025☑
1087531 Mar 2025☑
109812 Apr 2025☑
1101424 Apr 2025☑
1111156 Apr 2025☑
1121446 Apr 2025☑
1133222 Apr 2025☑
11419528 Apr 2025☑
115745 May 2025☑
11618210 May 2025☑
11726113 May 2025☑
11814114 May 2025☑
1195218 May 2025☑
12040119 May 2025☑
12111121 May 2025☑
12220122 May 2025☑
12331122 May 2025☑
12418425 May 2025☑
1259131 May 2025☑
1262424 Jun 2025☑
1272915 Jun 2025☑
1283215 Jun 2025☑
1292319 Jun 2025☑
1304214 Jun 2025☑
1319221 Jun 2025☑
13217228 Jun 2025☑
13327228 Jun 2025☑
134821 Jul 2025☑
1352025 Jul 2025☑
1363615 Jul 2025☑
13728212 Jul 2025☑
1388517 Jul 2025☑
1394419 Jul 2025☑
14023421 Jul 2025☑
1415125 Jul 2025☑
14216226 Jul 2025☑
14332229 Jul 2025☐
1442526 Aug 2025☑
1457118 Aug 2025☑
1468423 Aug 2025☑
1471811 Sep 2025☑
1482924 Sep 2025☑
1493224 Sep 2025☑
150725 Sep 2025☑
1511745 Sep 2025☑
152226 Sep 2025☑
1532817 Sep 2025☑
15440418 Sep 2025☑
1553519 Sep 2025☑
1566124 Sep 2025☑
15733525 Sep 2025☑
1584127 Sep 2025☑
1592327 Oct 2025☑
1601114 Oct 2025☑
16121121 Oct 2025☑
1623130 Oct 2025☑
1632512 Nov 2025☑
1643052 Nov 2025☑
1651212 Nov 2025☑
1662615 Nov 2025☑
16715115 Nov 2025☑
16830415 Nov 2025☑
1696216 Nov 2025☑
17014520 Nov 2025☑
17125126 Jan 2026☑
17224227 Jan 2026☑
1733611 Feb 2026☑
1742024 Feb 2026☑
175728 Feb 2026☑
17635112 Feb 2026☐
17738214 Feb 2026☑
17816315 Feb 2026☑
17920318 Feb 2026☑
18029121 Feb 2026☑
1813232 Mar 2026☑
1821319 Mar 2026☑
18321310 Mar 2026☐
18426312 Mar 2026☑
1856214 Mar 2026☑
18627216 Mar 2026☑
1873224 Mar 2026☑
18835124 Mar 2026☑
18914125 Mar 2026☑
1901332 Apr 2026☑
191535 Apr 2026☑
192928 Apr 2026☑
1933338 Apr 2026☑
19421313 Apr 2026☑
19522115 Apr 2026☑
19616218 Apr 2026☐
19733121 Apr 2026☑
19829322 Apr 2026☑
19923227 Apr 2026☑
20027129 Apr 2026☑
20128229 Apr 2026☑
20238329 Apr 2026☑
2031238 May 2026☐
20423314 May 2026☑
2059115 May 2026☑
20640320 May 2026☑
20716226 May 2026☑
20830328 May 2026☑
2092922 Jun 2026☑
2101234 Jun 2026☑
2112624 Jun 2026☑
2122535 Jun 2026☑
213617 Jun 2026☑
2142317 Jun 2026☑
2152438 Jun 2026☑
2161039 Jun 2026☑
2172739 Jun 2026☑
2182110 Jun 2026☑
2197311 Jun 2026☐
22017312 Jun 2026☑
2211318 Jun 2026☐
22228120 Jun 2026☑
22318222 Jun 2026☑
2241125 Jun 2026☐
22536330 Jun 2026☑
2263812 Jul 2026☑
2272119 Jul 2026☑
22815116 Jul 2026☑
22926116 Jul 2026☑
23024119 Jul 2026☑
23128321 Jul 2026☑
2321222 Jul 2026☑
23330225 Jul 2026☑
234432 Aug 2026☑
2351022 Aug 2026☑
2361722 Aug 2026☐
2373222 Aug 2026☑
2384012 Aug 2026☑
2391815 Aug 2026☑
24011315 Aug 2026☑
24112216 Aug 2026☑
24218326 Aug 2026☑
2432330 Aug 2026☑
2444021 Sep 2026☑
2453433 Sep 2026☑
2463913 Sep 2026☑
24710111 Sep 2026☐
24815311 Sep 2026☑
24914215 Sep 2026☐
25020116 Sep 2026☑
2514217 Sep 2026☑
25230121 Sep 2026☑
2537124 Sep 2026☑
25434224 Sep 2026☑
2551321 Oct 2026☑
256935 Oct 2026☑
2572235 Oct 2026☑
2583535 Oct 2026☑
25932110 Oct 2026☑
26016112 Oct 2026☑
26122214 Oct 2026☑
26214218 Oct 2026☑
2636321 Oct 2026☑
26417121 Oct 2026☑
2652223 Oct 2026☑
26625227 Oct 2026☑
267336 Nov 2026☑
2683418 Nov 2026☑
2691119 Nov 2026☑
2703117 Nov 2026☑
27112120 Nov 2026☑

The lesson on VBA in Access shows Execute and RecordsAffected.

Task 2: checking a leave request

Task The form frmAbsence should not save leave that exceeds the remaining allowance. Write Form_BeforeUpdate: if AbsenceType is “Leave”, calculate the remainder (the employee's LeaveDays via DLookup minus their leave days so far via DSum, with Nz). If the days in the form are more than the remainder, show “Only x days of leave left.” and cancel the save.

Employees 40 rows, 10 columns
EmployeeIDEmployeeNoFirstNameLastNameDepartmentIDManagerIDHireDateTermDateWeeklyHoursLeaveDays
1E1001EllaHill123 Dec 20134025
2E1002OliviaTaylor2110 Sep 20134025
3E1003GraceWright3122 Nov 20104025
4E1004LiamWright413 Mar 20154025
5E1005AvaWilson519 Sep 20144025
6E1006DavidWilson5513 Dec 20132013
7E1007LiamEvans444 May 20124025
8E1008NinaClark5516 Jun 202411 Mar 20264025
9E1009LeoClark5512 Mar 20154025
10E1010SophiaJohnson4429 Nov 20254025
11E1011LiamTaylor228 Sep 20194025
12E1012ChloeDavies2214 Jun 20204025
13E1013HenrySmith222 Feb 20204025
14E1014MiaBaker554 Apr 20194025
15E1015JackYoung444 Mar 20174025
16E1016MiaWard554 Nov 20174025
17E1017LucasBrown2217 Jan 20163220
18E1018GraceTaylor4422 Jan 20194025
19E1019GraceKing3311 Sep 201630 Dec 20254025
20E1020MiaJohnson3321 Sep 20203220
21E1021NoahBrown3328 Jul 20154025
22E1022OliviaHill339 Sep 20102013
23E1023SamTaylor334 Jun 20224025
24E1024LiamKing558 Aug 20214025
25E1025OliverWard448 Dec 20134025
26E1026ChloeJohnson4418 Oct 20134025
27E1027EllaDavies3321 Nov 20114025
28E1028JackTaylor5510 May 20103220
29E1029OliverBrown2212 Jun 20123019
30E1030NoahHall4424 Jul 20214025
31E1031JackWalker5527 Mar 201427 Dec 20253019
32E1032GraceBaker557 Dec 20222013
33E1033OliverDavies446 Aug 20124025
34E1034SamDavies5511 May 20264025
35E1035EmmaDavies4420 Oct 20253019
36E1036MiaWright5516 May 20214025
37E1037LeoJohnson4421 Jun 20264025
38E1038DavidHughes558 Dec 20254025
39E1039EmmaWright443 Jul 20263220
40E1040ChloeWard333 Apr 20253220
Absences 144 rows, 6 columns
AbsenceIDEmployeeIDStartDateEndDateAbsenceTypeDays
1286 Jan 20269 Jan 2026Leave4
2114 Jan 202623 Jan 2026Leave8
32616 Jan 202621 Jan 2026Sick4
4819 Jan 202622 Jan 2026Leave4
52523 Jan 202627 Jan 2026Leave3
6426 Jan 20265 Feb 2026Leave9
72726 Jan 202629 Jan 2026Leave4
8175 Feb 202610 Feb 2026Leave4
916 Feb 202613 Feb 2026Leave6
1036 Feb 202613 Feb 2026Leave6
11189 Feb 202612 Feb 2026Leave4
121816 Feb 202616 Feb 2026Sick1
132216 Feb 202618 Feb 2026Leave3
144016 Feb 202625 Feb 2026Leave8
15717 Feb 202618 Feb 2026Sick2
162219 Feb 20262 Mar 2026Leave8
171723 Feb 202627 Feb 2026Leave5
181224 Feb 20264 Mar 2026Leave7
19425 Feb 202626 Feb 2026Leave2
203227 Feb 202612 Mar 2026Leave10
2182 Mar 20266 Mar 2026Leave5
22132 Mar 202611 Mar 2026Leave8
23152 Mar 202611 Mar 2026Leave8
2463 Mar 202611 Mar 2026Leave7
2594 Mar 20265 Mar 2026Sick2
26145 Mar 202613 Mar 2026Leave7
27215 Mar 202613 Mar 2026Leave7
28206 Mar 20269 Mar 2026Sick2
29279 Mar 20269 Mar 2026Sick1
30359 Mar 20269 Mar 2026Sick1
314017 Mar 202617 Mar 2026Sick1
32418 Mar 202619 Mar 2026Sick2
332618 Mar 202623 Mar 2026Leave4
34319 Mar 202631 Mar 2026Leave9
351823 Mar 202624 Mar 2026Leave2
36226 Mar 202627 Mar 2026Leave2
37130 Mar 20262 Apr 2026Leave4
383830 Mar 20262 Apr 2026Leave4
39236 Apr 202610 Apr 2026Sick5
40337 Apr 202620 Apr 2026Leave10
41913 Apr 202622 Apr 2026Leave8
421813 Apr 202614 Apr 2026Sick2
432513 Apr 202613 Apr 2026Sick1
442716 Apr 202629 Apr 2026Leave10
454016 Apr 202622 Apr 2026Sick5
462617 Apr 202623 Apr 2026Leave5
471228 Apr 202629 Apr 2026Leave2
481329 Apr 202629 Apr 2026Sick1
49156 May 20268 May 2026Sick3
50411 May 202621 May 2026Leave9
51911 May 202612 May 2026Leave2
523612 May 202620 May 2026Leave7
532713 May 202621 May 2026Leave7
541814 May 202620 May 2026Sick5
552814 May 202614 May 2026Sick1
56918 May 202629 May 2026Leave10
572119 May 202628 May 2026Leave8
583320 May 202626 May 2026Sick5
591321 May 202626 May 2026Leave4
602021 May 202628 May 2026Leave6
613821 May 202629 May 2026Leave7
622822 May 202626 May 2026Leave3
632626 May 202627 May 2026Sick2
642929 May 20261 Jun 2026Sick2
65335 Jun 20265 Jun 2026Sick1
66385 Jun 202611 Jun 2026Leave5
67309 Jun 202611 Jun 2026Leave3
68311 Jun 202615 Jun 2026Sick3
691215 Jun 202615 Jun 2026Sick1
702315 Jun 202625 Jun 2026Leave9
71717 Jun 202622 Jun 2026Leave4
721417 Jun 202619 Jun 2026Leave3
731517 Jun 202625 Jun 2026Leave7
743418 Jun 202630 Jun 2026Leave9
75523 Jun 202630 Jun 2026Leave6
761723 Jun 202625 Jun 2026Sick3
772923 Jun 202624 Jun 2026Leave2
783229 Jun 202630 Jun 2026Leave2
791530 Jun 20266 Jul 2026Leave5
80131 Jul 202613 Jul 2026Leave9
81116 Jul 20269 Jul 2026Sick4
82176 Jul 20269 Jul 2026Leave4
83309 Jul 202617 Jul 2026Leave7
843610 Jul 202620 Jul 2026Leave7
851013 Jul 202615 Jul 2026Leave3
861121 Jul 202624 Jul 2026Sick4
872021 Jul 202623 Jul 2026Sick3
88522 Jul 202624 Jul 2026Sick3
893822 Jul 202623 Jul 2026Sick2
902527 Jul 20263 Aug 2026Leave6
912328 Jul 202610 Aug 2026Leave10
921729 Jul 202631 Jul 2026Sick3
932931 Jul 20267 Aug 2026Leave6
9493 Aug 20266 Aug 2026Sick4
9565 Aug 202610 Aug 2026Leave4
96106 Aug 202611 Aug 2026Leave4
973610 Aug 202621 Aug 2026Leave10
983910 Aug 202612 Aug 2026Sick3
993512 Aug 202612 Aug 2026Sick1
1004017 Aug 202620 Aug 2026Leave4
1012818 Aug 202619 Aug 2026Sick2
1021425 Aug 202626 Aug 2026Leave2
103427 Aug 202628 Aug 2026Leave2
1042828 Aug 20267 Sep 2026Leave7
105381 Sep 20264 Sep 2026Leave4
106182 Sep 20268 Sep 2026Leave5
107308 Sep 202615 Sep 2026Leave6
1083710 Sep 202623 Sep 2026Leave10
1091814 Sep 202618 Sep 2026Leave5
1102814 Sep 202615 Sep 2026Sick2
1111618 Sep 202629 Sep 2026Leave8
1123318 Sep 202622 Sep 2026Leave3
1132121 Sep 202622 Sep 2026Leave2
1143330 Sep 20269 Oct 2026Leave8
115241 Oct 202612 Oct 2026Leave8
116115 Oct 202613 Oct 2026Leave7
117345 Oct 202615 Oct 2026Leave9
118309 Oct 202621 Oct 2026Leave9
119712 Oct 202616 Oct 2026Sick5
1202415 Oct 202627 Oct 2026Leave9
1212515 Oct 202621 Oct 2026Leave5
122116 Oct 202620 Oct 2026Leave3
1233716 Oct 202622 Oct 2026Sick5
1241119 Oct 202629 Oct 2026Leave9
1251619 Oct 202628 Oct 2026Leave8
1261720 Oct 202626 Oct 2026Sick5
127222 Oct 202623 Oct 2026Leave2
1283723 Oct 202630 Oct 2026Leave6
1293527 Oct 20264 Nov 2026Leave7
1302628 Oct 202629 Oct 2026Leave2
13163 Nov 20269 Nov 2026Sick5
132139 Nov 202610 Nov 2026Leave2
1332610 Nov 202623 Nov 2026Leave10
1342511 Nov 202613 Nov 2026Sick3
1352911 Nov 202617 Nov 2026Sick5
1363411 Nov 202616 Nov 2026Sick4
1371416 Nov 202624 Nov 2026Leave7
1382016 Nov 202616 Nov 2026Sick1
1393516 Nov 202623 Nov 2026Leave6
1403217 Nov 202623 Nov 2026Sick5
1412118 Nov 202625 Nov 2026Leave6
1421123 Nov 202627 Nov 2026Leave5
1431223 Nov 202623 Nov 2026Sick1
1441523 Nov 202624 Nov 2026Leave2

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.

The lesson on VBA in Access also covers events with Cancel, the lesson on DLookup and DSum domain functions.

Common mistakes

Former employees added too

Without TermDate Is Null, former employees get a record dated 1 Dec 2026 as well. Nobody notices until the next audit.

Message without Cancel

Without Cancel = True the procedure shows the message but saves the request anyway. Anyone who clicks OK quickly has booked too much leave.

In real Access

  • In real use, department and training come from combo boxes on a small form; the procedure reads them with Me!cboDepartment.
  • When an existing request is edited, DSum already counts its own days. Exclude the current record in the criterion: AND AbsenceID <> Me!AbsenceID.
  • A leave check with public holidays and half days needs a table of working days; the logic stays the same.

How to check your result

  • Task 1: the message reads “7 records added”, one per active employee in the warehouse.
  • Task 2: for employee 6 with 2 days left, “Only 2 days of leave left.” appears and the 5-day request is not saved.

Done: all six cases

Your account shows which tasks are still missing, and that is where you request the certificate.

Open the course in your account

Back to the course overview.

Write to us

Choose language

Select your preferred language