To access VBA code in Microsoft Access, open your database, press Alt+F11 to launch the Visual Basic Editor, and use the Project Explorer to browse modules, forms, and reports. You can also right-click any form or report in the Navigation Pane and choose Design View, then click View Code in the ribbon. This tutorial walks you through finding, reading, and editing VBA code safely, even if you have never written a macro before.

What You'll Need

  • A desktop version of Microsoft Access (Microsoft 365, Access 2021, 2019, or 2016). The browser version does not include the VBA editor.
  • A database file (.accdb or .mdb) that you have permission to modify. If the file is shared, make a copy before editing code.
  • Basic familiarity with Access objects such as tables, forms, and reports.
  • Optional: a sample database or a blank one to practice in. You can create a blank database from File > New.

Step-by-Step

  1. Open your database in Access. Double-click the .accdb file or use File > Open. If a security warning appears about blocked content, click Enable Content only if you trust the file. Note that menu labels can differ slightly by version and region.
  2. Launch the Visual Basic Editor (VBE). Press Alt+F11 on your keyboard. Alternatively, go to the Database Tools tab and click Visual Basic. The VBE opens in a separate window.
  3. Explore the Project Explorer. In the VBE, if the Project Explorer pane is not visible, press Ctrl+R or choose View > Project Explorer. You will see a tree of your database and its objects: Microsoft Access Class Objects (forms and reports), Modules, and References.
  4. Open a standard module. Expand the Modules folder. If none exist, insert one: choose Insert > Module. A blank code window appears where you can write or paste VBA procedures.
  5. View code behind a form or report. In the Project Explorer, expand Microsoft Access Class Objects and double-click a form or report name (for example, Form_Form1). This opens the code module attached to that object, where event procedures like button clicks live.
  6. Read existing code. Scroll through the code window. Procedures start with Sub or Function and end with End Sub or End Function. Comments begin with an apostrophe ('). Use Ctrl+F to find specific text.
  7. Edit code carefully. Click in the code window and make changes. VBA does not auto-save; press Ctrl+S to save the module. If you are experimenting, save a backup copy of the database first (File > Save As).
  8. Run a procedure. Place the cursor inside a Sub procedure and press F5 to run it, or use Run > Run Sub/UserForm. For event procedures, you typically trigger them by using the form in Access, not by pressing F5 directly.
  9. Use the Immediate Window for quick tests. Press Ctrl+G to open the Immediate Window. Type a question mark followed by an expression, such as ?Now(), and press Enter to see the result. This is useful for checking values without writing a full procedure.
  10. Compile and debug. Choose Debug > Compile [YourDatabaseName] to catch syntax errors. If code stops with an error, use Debug > Step Into (F8) to run line by line. Press F5 to continue or Shift+F5 to reset.
  11. Return to Access. Close the VBE window or press Alt+F11 again. Save your database. Test the form or report that uses the code to confirm it behaves as expected.

Common Mistakes

  • Editing a shared database without a backup. VBA changes are saved immediately to the file. Always work on a copy when experimenting.
  • Confusing macros with VBA. Access macros are separate from VBA modules. The VBE shows VBA code only; macros are edited in the Macro Designer.
  • Running event procedures directly with F5. Procedures that expect a form to be open may fail. Trigger them through the form instead.
  • Ignoring compile errors. Code with syntax errors may still run partially, causing confusing behavior. Compile before testing.
  • Forgetting to enable the Developer tab. The Visual Basic button is on the Database Tools tab by default, but some versions place it under a Developer tab you must enable in Access Options > Customize Ribbon.

Tips

  • Use Option Explicit at the top of every module (Tools > Options > Require Variable Declaration) to catch undeclared variables.
  • Name your modules and procedures descriptively, such as modUtilities or Sub OpenReport.
  • Comment complex logic with an apostrophe so future you (or a colleague) can follow it.
  • Press F2 to open the Object Browser and explore available objects and methods.
  • Keep a separate copy of working code in a text file before major edits.

Troubleshooting

If Alt+F11 does nothing, click inside the Access window first; the shortcut only works when Access has focus. If the Visual Basic button is greyed out, the database may be opened in a restricted mode or from a trusted location issue. Check File > Options > Trust Center > Trust Center Settings > Macro Settings, and consider adding the folder to trusted locations. If code fails with a "Project cannot be compiled" message, use Debug > Compile to locate the offending line. If a form's code does not run, confirm the form is not in Layout View (code runs in Form View) and that the event property (such as On Click) is set to [Event Procedure]. If the VBE shows no project, ensure the database is not read-only. For persistent issues, close Access, reopen the file, and try again; if the problem continues, create a new blank database and import objects one at a time to isolate corruption.

Frequently Asked Questions

Can I use VBA in Access on the web?

No. Access for the web (the browser version) does not support VBA. You need the desktop application.

How do I add a new module?

In the Visual Basic Editor, choose Insert > Module. A new standard module appears in the Project Explorer under Modules.

What is the difference between a standard module and a class module?

Standard modules hold general-purpose procedures you can call from anywhere. Class modules are tied to a specific form or report and contain event procedures for that object.

Why does my code not run when I open the form?

Check that the event property (for example, On Load) is set to [Event Procedure] and that the form opens in Form View, not Design View or Layout View.

Can I undo changes in the VBA editor?

Yes, press Ctrl+Z immediately after an edit. However, once you save and close the database, undo history is lost, so keep backups.

Is VBA still supported in Access?

Yes, VBA remains the built-in programming language for desktop Access in current versions.