VBA in Access
VBA controls Access more precisely than macros: code reacts to clicks and input, checks values before Access saves them, and builds filters from what someone types into a form.
In short: DoCmd runs Access actions (DoCmd.OpenForm works like the OpenForm macro action). Event procedures live in a form’s module and are named Control_Event, such as cmdFilter_Click. Me is the form itself, Me!txtCity a control on it. In Form_BeforeUpdate, Cancel = True stops the save.
Before you start: Macros. The VBA language itself (variables, If, loops) is covered in the VBA course.
Try it now
Run starts your procedure; below it you see what Access does. F8 steps through the code line by line.
Task Write the procedure OpenUSA: it opens the form frmCustomers and shows only the customers from the USA.
Customers 25 rows, 10 columns
| CustomerID | Company | FirstName | LastName | City | PostalCode | Country | CustomerSince | Regular | Discount |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Noah | Evans | Leeds | LS1 1UR | UK | 28 Feb 2023 | ☑ | 0.1 | |
| 2 | Shaw & Partners | Noah | Davies | Glasgow | G1 1XQ | UK | 5 Feb 2024 | ☐ | 0.03 |
| 3 | Northlight Inc | Sophia | Hall | Denver | 80202 | USA | 30 Jun 2024 | ☑ | 0 |
| 4 | Olivia | Evans | Bristol | BS1 4DJ | UK | 29 Jun 2022 | ☑ | 0 | |
| 5 | Olivia | Smith | London | EC1A 1BB | UK | 23 Sep 2025 | ☑ | 0 | |
| 6 | Northlight Inc | Mia | Evans | Manchester | M1 1AE | UK | 1 Feb 2025 | ☐ | 0.1 |
| 7 | Bauer Ltd | Ava | Wilson | UK | 14 Oct 2024 | ☐ | 0.05 | ||
| 8 | Lucas | Taylor | Glasgow | G1 1XQ | UK | 2 Jul 2023 | ☐ | 0 | |
| 9 | Jack | Clark | Seattle | 98101 | USA | 7 Nov 2022 | ☐ | 0.1 | |
| 10 | Northlight Inc | Henry | Taylor | Denver | 80202 | USA | 3 Nov 2022 | ☑ | 0.1 |
| 11 | Northlight Inc | Oliver | Johnson | Bristol | BS1 4DJ | UK | 6 Mar 2024 | ☐ | 0 |
| 12 | Henry | Taylor | Chicago | 60601 | USA | 24 Dec 2023 | ☐ | 0 | |
| 13 | Northlight Inc | Noah | Taylor | London | EC1A 1BB | UK | 12 Oct 2022 | ☐ | 0 |
| 14 | Summit Consulting | Mia | Smith | Austin | 78701 | USA | 6 Feb 2022 | ☑ | 0 |
| 15 | Mia | Clark | Austin | 78701 | USA | 9 Mar 2022 | ☑ | 0.05 | |
| 16 | Chloe | Hall | Seattle | 98101 | USA | 4 May 2022 | ☐ | 0.03 | |
| 17 | Market Street Clinic | Sophia | Johnson | Boston | 02108 | USA | 4 Jun 2023 | ☑ | 0 |
| 18 | Roth Legal | Mia | Wright | Manchester | M1 1AE | UK | 23 Sep 2022 | ☑ | 0.05 |
| 19 | Northlight Inc | Grace | Wright | UK | 16 Dec 2023 | ☐ | 0.03 | ||
| 20 | Northlight Inc | Olivia | Brown | Boston | 02108 | USA | 19 Jun 2023 | ☐ | 0.03 |
| 21 | Liam | Brown | Boston | 02108 | USA | 23 Jun 2025 | ☐ | 0.03 | |
| 22 | Bauer Ltd | James | Johnson | Manchester | M1 1AE | UK | 5 Jun 2025 | ☐ | 0.05 |
| 23 | Noah | Wilson | Austin | 78701 | USA | 23 May 2022 | ☐ | 0 | |
| 24 | Market Street Clinic | James | Smith | Bristol | BS1 4DJ | UK | 16 Jan 2022 | ☐ | 0.1 |
| 25 | Northlight Inc | Sophia | Hall | Denver | 80202 | USA | 4 Aug 2025 | ☑ | 0 |
DoCmd: actions from code
| Method | like the macro action |
|---|---|
DoCmd.OpenForm "frmCustomers", , , "Country = 'UK'" | OpenForm with a Where Condition |
DoCmd.OpenReport "rptCustomers", acViewPreview | OpenReport in Print Preview |
DoCmd.OpenQuery "qName" | OpenQuery |
DoCmd.SetWarnings False | SetWarnings |
DoCmd.RunSQL "UPDATE …" | RunSQL |
Unlike in a macro, warnings stay off after DoCmd.SetWarnings False until your code turns them back on with DoCmd.SetWarnings True. DoCmd.RunSQL takes only action queries; Access answers a SELECT with error 2342.
Event procedures and Me
An event procedure runs when something happens in the form: a click, an entry, a save. Me refers to the form whose module holds the code; outside it, VBA reports “Invalid use of Me keyword”.
Task In the form frmCustomers someone types a city into the unbound text box txtCity and clicks the button cmdFilter. Write the event procedure cmdFilter_Click: it filters the form to customers in that city.
Customers 25 rows, 10 columns
| CustomerID | Company | FirstName | LastName | City | PostalCode | Country | CustomerSince | Regular | Discount |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Noah | Evans | Leeds | LS1 1UR | UK | 28 Feb 2023 | ☑ | 0.1 | |
| 2 | Shaw & Partners | Noah | Davies | Glasgow | G1 1XQ | UK | 5 Feb 2024 | ☐ | 0.03 |
| 3 | Northlight Inc | Sophia | Hall | Denver | 80202 | USA | 30 Jun 2024 | ☑ | 0 |
| 4 | Olivia | Evans | Bristol | BS1 4DJ | UK | 29 Jun 2022 | ☑ | 0 | |
| 5 | Olivia | Smith | London | EC1A 1BB | UK | 23 Sep 2025 | ☑ | 0 | |
| 6 | Northlight Inc | Mia | Evans | Manchester | M1 1AE | UK | 1 Feb 2025 | ☐ | 0.1 |
| 7 | Bauer Ltd | Ava | Wilson | UK | 14 Oct 2024 | ☐ | 0.05 | ||
| 8 | Lucas | Taylor | Glasgow | G1 1XQ | UK | 2 Jul 2023 | ☐ | 0 | |
| 9 | Jack | Clark | Seattle | 98101 | USA | 7 Nov 2022 | ☐ | 0.1 | |
| 10 | Northlight Inc | Henry | Taylor | Denver | 80202 | USA | 3 Nov 2022 | ☑ | 0.1 |
| 11 | Northlight Inc | Oliver | Johnson | Bristol | BS1 4DJ | UK | 6 Mar 2024 | ☐ | 0 |
| 12 | Henry | Taylor | Chicago | 60601 | USA | 24 Dec 2023 | ☐ | 0 | |
| 13 | Northlight Inc | Noah | Taylor | London | EC1A 1BB | UK | 12 Oct 2022 | ☐ | 0 |
| 14 | Summit Consulting | Mia | Smith | Austin | 78701 | USA | 6 Feb 2022 | ☑ | 0 |
| 15 | Mia | Clark | Austin | 78701 | USA | 9 Mar 2022 | ☑ | 0.05 | |
| 16 | Chloe | Hall | Seattle | 98101 | USA | 4 May 2022 | ☐ | 0.03 | |
| 17 | Market Street Clinic | Sophia | Johnson | Boston | 02108 | USA | 4 Jun 2023 | ☑ | 0 |
| 18 | Roth Legal | Mia | Wright | Manchester | M1 1AE | UK | 23 Sep 2022 | ☑ | 0.05 |
| 19 | Northlight Inc | Grace | Wright | UK | 16 Dec 2023 | ☐ | 0.03 | ||
| 20 | Northlight Inc | Olivia | Brown | Boston | 02108 | USA | 19 Jun 2023 | ☐ | 0.03 |
| 21 | Liam | Brown | Boston | 02108 | USA | 23 Jun 2025 | ☐ | 0.03 | |
| 22 | Bauer Ltd | James | Johnson | Manchester | M1 1AE | UK | 5 Jun 2025 | ☐ | 0.05 |
| 23 | Noah | Wilson | Austin | 78701 | USA | 23 May 2022 | ☐ | 0 | |
| 24 | Market Street Clinic | James | Smith | Bristol | BS1 4DJ | UK | 16 Jan 2022 | ☐ | 0.1 |
| 25 | Northlight Inc | Sophia | Hall | Denver | 80202 | USA | 4 Aug 2025 | ☑ | 0 |
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.
Checking input with Before Update
Form_BeforeUpdate runs before Access saves the changed record. If the procedure sets its Cancel argument to True, Access does not save.
Task In the form frmProducts, Access should not save a negative price. Write Form_BeforeUpdate: if txtPrice is less than 0, show “The price cannot be negative.” and cancel saving.
Products 18 rows, 6 columns
| ProductID | ProductName | CategoryID | UnitPrice | UnitsInStock | Discontinued |
|---|---|---|---|---|---|
| 1 | Blue ballpoint pen | 1 | 1.20 | 121 | ☐ |
| 2 | Highlighter 4-pack | 1 | 3.90 | 76 | ☐ |
| 3 | Hole punch | 1 | 8.50 | 237 | ☐ |
| 4 | Stapler | 1 | 12.90 | 61 | ☐ |
| 5 | Lever arch file | 1 | 2.80 | 76 | ☑ |
| 6 | USB drive 64 GB | 2 | 9.90 | 193 | ☐ |
| 7 | Wireless mouse | 2 | 19.90 | 157 | ☐ |
| 8 | Keyboard | 2 | 34.90 | 216 | ☐ |
| 9 | 27-inch monitor | 2 | 229.00 | 129 | ☐ |
| 10 | Headset | 2 | 49.00 | 47 | ☐ |
| 11 | Office chair | 3 | 189.00 | 135 | ☐ |
| 12 | Desk | 3 | 349.00 | 46 | ☐ |
| 13 | Mobile pedestal | 3 | 129.00 | 105 | ☐ |
| 14 | Copy paper A4 | 4 | 4.50 | 35 | ☐ |
| 15 | Notepad | 4 | 1.90 | 122 | ☐ |
| 16 | Envelopes 100-pack | 4 | 6.90 | 93 | ☐ |
| 17 | Labels | 4 | 7.50 | 75 | ☐ |
| 18 | Laminating pouches | 4 | 11.90 | 211 | ☐ |
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.
In real Access
- Open the form in Design view, select the button, Property Sheet (F4), Event tab, On Click: choose [Event Procedure] and click …. Access creates the empty procedure in the form’s module.
- Procedures for every form go into a standard module (Create, Module). Alt+F11 switches to the VBA editor.
- In the Immediate window (Ctrl+G) you start a procedure by its name and see the output of Debug.Print.
Common mistakes
Filter without FilterOn
Me.Filter = … alone changes nothing you can see; only Me.FilterOn = True applies the filter.
Quotes in the filter text
Text inside the filter goes in single quotes: "City = '" & Me!txtCity & "'". Numbers go without: "CustomerID = " & Me!CustomerID.
Wrong form name
“The form name '…' is misspelled or refers to a form that doesn't exist.” (error 2102) means DoCmd.OpenForm cannot find the name.
FAQ
Where is the code of a button?
In the form’s module. In the VBA editor it is called Form_ plus the form name, such as Form_frmCustomers.
Can I read another open form?
Yes, through the Forms collection: Forms!frmCustomers!txtCity. If the form is not open, Access reports error 2450.