How to Compare Access Table Fields Using VBA DAO Recordsets
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.

- 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.
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.
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.
Press Alt + F11 in Access to open the VBA IDE, then insert a new Module from the 'Insert' menu.
Declare your database and DAO recordset variables using 'Dim db As DAO.Database' and 'Dim rs As DAO.Recordset'.
Set the database object ('Set db = CurrentDb') and open the recordset for your table ('Set rs = db.OpenRecordset("tbl_questions_answers")').
Write a 'Do While Not rs.EOF' loop to iterate through the records until the end of the file is reached.
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.
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 an Access Query
If complex procedural VBA is not strictly required, an Access Query can identify matching records much faster than a DAO loop.
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.

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.




