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
CustomerIDCompanyFirstNameLastNameCityPostalCodeCountryCustomerSinceRegularDiscount
1NoahEvansLeedsLS1 1URUK28 Feb 2023☑0.1
2Shaw & PartnersNoahDaviesGlasgowG1 1XQUK5 Feb 2024☐0.03
3Northlight IncSophiaHallDenver80202USA30 Jun 2024☑0
4OliviaEvansBristolBS1 4DJUK29 Jun 2022☑0
5OliviaSmithLondonEC1A 1BBUK23 Sep 2025☑0
6Northlight IncMiaEvansManchesterM1 1AEUK1 Feb 2025☐0.1
7Bauer LtdAvaWilsonUK14 Oct 2024☐0.05
8LucasTaylorGlasgowG1 1XQUK2 Jul 2023☐0
9JackClarkSeattle98101USA7 Nov 2022☐0.1
10Northlight IncHenryTaylorDenver80202USA3 Nov 2022☑0.1
11Northlight IncOliverJohnsonBristolBS1 4DJUK6 Mar 2024☐0
12HenryTaylorChicago60601USA24 Dec 2023☐0
13Northlight IncNoahTaylorLondonEC1A 1BBUK12 Oct 2022☐0
14Summit ConsultingMiaSmithAustin78701USA6 Feb 2022☑0
15MiaClarkAustin78701USA9 Mar 2022☑0.05
16ChloeHallSeattle98101USA4 May 2022☐0.03
17Market Street ClinicSophiaJohnsonBoston02108USA4 Jun 2023☑0
18Roth LegalMiaWrightManchesterM1 1AEUK23 Sep 2022☑0.05
19Northlight IncGraceWrightUK16 Dec 2023☐0.03
20Northlight IncOliviaBrownBoston02108USA19 Jun 2023☐0.03
21LiamBrownBoston02108USA23 Jun 2025☐0.03
22Bauer LtdJamesJohnsonManchesterM1 1AEUK5 Jun 2025☐0.05
23NoahWilsonAustin78701USA23 May 2022☐0
24Market Street ClinicJamesSmithBristolBS1 4DJUK16 Jan 2022☐0.1
25Northlight IncSophiaHallDenver80202USA4 Aug 2025☑0

DoCmd: actions from code

Methodlike the macro action
DoCmd.OpenForm "frmCustomers", , , "Country = 'UK'"OpenForm with a Where Condition
DoCmd.OpenReport "rptCustomers", acViewPreviewOpenReport in Print Preview
DoCmd.OpenQuery "qName"OpenQuery
DoCmd.SetWarnings FalseSetWarnings
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
CustomerIDCompanyFirstNameLastNameCityPostalCodeCountryCustomerSinceRegularDiscount
1NoahEvansLeedsLS1 1URUK28 Feb 2023☑0.1
2Shaw & PartnersNoahDaviesGlasgowG1 1XQUK5 Feb 2024☐0.03
3Northlight IncSophiaHallDenver80202USA30 Jun 2024☑0
4OliviaEvansBristolBS1 4DJUK29 Jun 2022☑0
5OliviaSmithLondonEC1A 1BBUK23 Sep 2025☑0
6Northlight IncMiaEvansManchesterM1 1AEUK1 Feb 2025☐0.1
7Bauer LtdAvaWilsonUK14 Oct 2024☐0.05
8LucasTaylorGlasgowG1 1XQUK2 Jul 2023☐0
9JackClarkSeattle98101USA7 Nov 2022☐0.1
10Northlight IncHenryTaylorDenver80202USA3 Nov 2022☑0.1
11Northlight IncOliverJohnsonBristolBS1 4DJUK6 Mar 2024☐0
12HenryTaylorChicago60601USA24 Dec 2023☐0
13Northlight IncNoahTaylorLondonEC1A 1BBUK12 Oct 2022☐0
14Summit ConsultingMiaSmithAustin78701USA6 Feb 2022☑0
15MiaClarkAustin78701USA9 Mar 2022☑0.05
16ChloeHallSeattle98101USA4 May 2022☐0.03
17Market Street ClinicSophiaJohnsonBoston02108USA4 Jun 2023☑0
18Roth LegalMiaWrightManchesterM1 1AEUK23 Sep 2022☑0.05
19Northlight IncGraceWrightUK16 Dec 2023☐0.03
20Northlight IncOliviaBrownBoston02108USA19 Jun 2023☐0.03
21LiamBrownBoston02108USA23 Jun 2025☐0.03
22Bauer LtdJamesJohnsonManchesterM1 1AEUK5 Jun 2025☐0.05
23NoahWilsonAustin78701USA23 May 2022☐0
24Market Street ClinicJamesSmithBristolBS1 4DJUK16 Jan 2022☐0.1
25Northlight IncSophiaHallDenver80202USA4 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
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.

In real Access

  1. 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.
  2. Procedures for every form go into a standard module (Create, Module). Alt+F11 switches to the VBA editor.
  3. 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.

Learn next: Recordset (DAO)

Read and change records in a loop.

Recordset

Write to us

Choose language

Select your preferred language