How to Fix Microsoft Access VBA DAO Code for Adding and Deleting Records
Question details
The user needs to correct VBA DAO procedures to successfully add and delete records in a Microsoft Access database.

- 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 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.
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.
Open your VBA editor and type 'Option Explicit' at the very top of your module to force explicit variable declaration.
Ensure your recordset variable is named consistently throughout the procedure (e.g., Dim rst1 As DAO.Recordset and Set rst1 = CurrentDb.OpenRecordset).
Locate any instances of 'dbs1.Close' (where dbs1 represents CurrentDb) and delete them. The CurrentDb object does not need to be closed.
Click 'Debug' > 'Compile' in the VBA menu to check for any remaining syntax or missing reference errors.
Bulk Delete Records Using SQL Execution
Use the CurrentDb.Execute method for a faster and more reliable way to delete all records from a table.
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. Install WPS Office: Download and install WPS Office from the official website to your device.
- 2. Import database records: Open WPS Spreadsheet and import your exported Access data files such as .csv or .xlsx.
- 3. Manage data with ease: Use the built-in data analysis tools, pivot tables, or macros to sort, filter, and manage your records effortlessly.

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.




