Others

How to Fix Microsoft Access VBA DAO Code for Adding and Deleting Records

Khadija KhanKhadija Khan Nov 3, 2026 869 views

Question details

The user needs to correct VBA DAO procedures to successfully add and delete records in a Microsoft Access database.

How to Fix VBA DAO Code for Adding and Deleting Access Records
Product
Microsoft Access
Device & OS
not provided
Scenario
Writing or executing VBA code using Data Access Objects (DAO) to manage database records.
Observed behavior
The VBA procedures fail to execute properly due to mismatched variable names, unnecessary database closures, missing error handling, and compilation issues.
Before you start

Before modifying your VBA code, open the Access VBA Editor (Alt + F11) to check for broken reference libraries and ensure your database is safely backed up.

Solution 1Recommended

Correct Variable Declarations and Remove Unnecessary Closures

Fix mismatched DAO.Recordset variables and optimize the use of CurrentDb to prevent runtime errors.

A common cause of DAO procedure failure is declaring a variable like 'rst1' but calling it differently later in the code. Additionally, closing CurrentDb manually can lead to unexpected database states.

1
Force explicit variables

Open your VBA editor and type 'Option Explicit' at the very top of your module to force explicit variable declaration.

2
Match variable names

Ensure your recordset variable is named consistently throughout the procedure (e.g., Dim rst1 As DAO.Recordset and Set rst1 = CurrentDb.OpenRecordset).

3
Remove CurrentDb closure

Locate any instances of 'dbs1.Close' (where dbs1 represents CurrentDb) and delete them. The CurrentDb object does not need to be closed.

4
Compile the database

Click 'Debug' > 'Compile' in the VBA menu to check for any remaining syntax or missing reference errors.

Best Practice: Using Option Explicit helps catch typos in variable names immediately during compilation rather than failing silently at runtime.
Free Microsoft Office alternative

Looking for a Lightweight Alternative to Microsoft Office?

While Microsoft Access is a powerful database tool, handling data and creating reports can often be done more easily in spreadsheet applications. WPS Office offers a highly compatible, fast, and free alternative for your daily document and data analysis needs.

  1. 1. Install WPS Office: Download and install WPS Office from the official website to your device.
  2. 2. Import database records: Open WPS Spreadsheet and import your exported Access data files such as .csv or .xlsx.
  3. 3. Manage data with ease: Use the built-in data analysis tools, pivot tables, or macros to sort, filter, and manage your records effortlessly.
Fully compatible with Microsoft Excel (.xlsx, .xlsm, .csv) formats.Supports advanced macros and VBA in the Spreadsheet application.Lightweight and runs smoothly on older devices without lagging.Free to use with an intuitive, tabbed user interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a 'User-defined type not defined' error in Access VBA?

This usually means the 'Microsoft Office x.x Access database engine Object Library' (DAO) reference is missing. Go to Tools > References in the VBA editor, locate it in the list, and check the box to enable it.

Do I need to close the DAO.Recordset after use?

Yes, you should always close your recordset using 'rst.Close' and set it to Nothing ('Set rst = Nothing') at the end of your procedure to free up system memory, even though you shouldn't close 'CurrentDb'.

What does dbFailOnError do in Access VBA?

It forces the database to trigger a runtime error if a bulk SQL operation (like DELETE or UPDATE) fails, automatically rolling back any partial changes to maintain data integrity.