VBA & Macro Problems

How to Compare Access Table Fields Using VBA DAO Recordsets

Kushani NimanthikaKushani Nimanthika Oct 23, 2026 870 views

Question details

The user needs to compare two fields across all records in a Microsoft Access table using a VBA DAO recordset and execute an action based on whether they match.

How to Compare Access Table Fields Using VBA DAO Recordsets
Product
Microsoft Access
Device & OS
not provided
Scenario
Comparing user answers (SelectedAnswer) against a master key (CorrectAnswer) within a table named tbl_questions_answers.
Observed behavior
Requires a programmatic VBA loop to evaluate field values sequentially, safely manage potential Null values during the comparison, and move through the dataset without errors.
Before you start

Ensure you have a backup of your Microsoft Access database before running any VBA scripts that iterate through and potentially modify live table data. Familiarize yourself with the VBA Editor by pressing Alt + F11.

Solution 1Recommended

Compare Fields Using a DAO Recordset Loop

Open a DAO recordset to iterate through each record sequentially and compare the target fields using an If...Then statement.

Using a DAO (Data Access Object) Recordset allows you to traverse a table row by row. This is highly useful when you need to trigger complex procedural VBA actions based on the comparison of two fields.

1
Open the VBA Editor

Press Alt + F11 in Access to open the VBA IDE, then insert a new Module from the 'Insert' menu.

2
Declare Variables

Declare your database and DAO recordset variables using 'Dim db As DAO.Database' and 'Dim rs As DAO.Recordset'.

3
Initialize the Recordset

Set the database object ('Set db = CurrentDb') and open the recordset for your table ('Set rs = db.OpenRecordset("tbl_questions_answers")').

4
Create the Loop

Write a 'Do While Not rs.EOF' loop to iterate through the records until the end of the file is reached.

5
Compare Values Safely

Inside the loop, use the Nz function to prevent Null errors: 'If Nz(rs!CorrectAnswer, "") = Nz(rs!SelectedAnswer, "") Then'. Place your matching logic inside this If block.

6
Move Next and Clean Up

Add 'rs.MoveNext' before the 'Loop' command to proceed to the next record. Once the loop finishes, close the recordset using 'rs.Close' and set the object variables to Nothing.

Compare Fields Using a DAO Recordset Loop
Handling Null Values Properly: Always use the Nz() function or IsNull() when comparing fields that might be empty. Evaluating 'field = Null' will never return True in VBA, causing your logic to fail silently.
Free Microsoft Office alternative

Need a Free Alternative to Microsoft Office? Try WPS Office

While WPS Office does not include a direct database equivalent to Microsoft Access, it offers a robust, free, and highly compatible alternative to Microsoft Word, Excel, and PowerPoint. If you are managing data exports or generating reports from your databases, WPS Spreadsheets handles complex data analysis seamlessly.

Completely free to download and use for word processing, spreadsheets, and presentations.Outstanding compatibility with Microsoft Office file formats (.docx, .xlsx, .pptx).Lightweight architecture that runs smoothly on Windows, Mac, Linux, iOS, and Android.Familiar tabbed user interface, ensuring a seamless migration with zero learning curve.
microsoft office alternative - wps office

Frequently Asked Questions

Why does comparing a field to Null in VBA always fail?

In VBA and SQL, Null represents an unknown or missing value. Comparing any value (even another Null) to Null yields Null, not True or False. You must use the IsNull() function or the Nz() function to handle and evaluate Null values accurately.

What does the Nz function do in Access VBA?

The Nz() (Null-to-Zero) function converts a Null value to a zero, a zero-length string (""), or another specified default value. This prevents type mismatch errors and logical failures when comparing fields or assigning variables that might contain empty data.

Is it faster to use a DAO Recordset or a SQL Query to compare records?

Using a SQL query (such as an UPDATE or SELECT query) is generally much faster and more efficient than looping through a DAO Recordset row by row, especially for large datasets. You should use DAO loops only when complex, conditional, record-by-record procedural logic is required that cannot be expressed in standard SQL.