Others

Fix Invalid Object Errors When Looping Through Microsoft Access Fields

Algirdas JasaitisAlgirdas Jasaitis Nov 22, 2026 868 views

Question details

The user needs to iterate through an Access table's fields without an invalid object error.

Fix Invalid Object Errors When Looping Through Access Fields
Product
Microsoft Access
Device & OS
not provided
Scenario
A VBA procedure loops through CurrentDb.TableDefs(tName).Fields to inspect the fields in a table.
Observed behavior
Access raises “Object invalid or no longer set” during the loop, even though the table and field collection appear to exist.
Before you start

Open the database in Access and locate the VBA procedure that loops through the fields. Confirm that tName contains the name of an existing table and that the Microsoft Office Access database engine object library is available under Tools > References in the VBA editor.

Solution 1Recommended

Keep a DAO database reference while looping through fields

Use an explicit DAO.Database variable to keep the database object alive during field enumeration.

Calling CurrentDb directly in the For Each expression can leave the field collection dependent on a temporary database object. Keeping a database variable in scope maintains an explicit reference throughout the loop. Similar lifetime issues can occur with other temporary objects whose child objects depend on them.

1
Open the VBA procedure

In Access, press Alt+F11 to open the VBA editor, then open the module containing the field loop.

2
Declare and set the database variable

In the procedure, declare Dim db As DAO.Database and Dim fld As DAO.Field. Before the loop, add Set db = CurrentDb.

3
Loop through the stored database reference

Replace the existing For Each expression with a loop using the database variable: For Each fld In db.TableDefs(tName).Fields Debug.Print fld.Name Next fld Replace Debug.Print fld.Name with the field processing your procedure needs.

4
Run the procedure

Run the procedure from the VBA editor or its normal Access entry point and check that the loop processes the table's fields without the error.

Keep a DAO database reference while looping through fields
Object cleanup: Local object variables are normally released automatically when the procedure ends. You can use Set fld = Nothing and Set db = Nothing after the loop to release those references earlier, but explicit cleanup is usually unnecessary at procedure exit.
Free Microsoft Office alternative

Work with spreadsheet data in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing common documents and spreadsheets. It can help you move spreadsheet work from Microsoft Office, but it does not fix Access DAO errors or replace Access database functionality.

Open and edit Microsoft Excel XLS and XLSX filesReview table-like data in WPS SpreadsheetsUse a familiar interface for documents and spreadsheetsMove compatible Office files into a lightweight, free office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why can CurrentDb cause an error in a For Each loop?

The Fields collection can depend on a temporary database object returned by CurrentDb. Assigning Set db = CurrentDb before the loop keeps an explicit database reference in scope while the fields are enumerated.

Can I use DBEngine instead of CurrentDb?

Yes. Declare Dim db As DAO.Database and use Set db = DBEngine(0)(0), then loop through db.TableDefs(tName).Fields. CurrentDb returns a separate database object for the currently open database, while DBEngine(0)(0) refers to the database through the DAO workspace.

Do I need to set the field variable to Nothing after the loop?

Usually not. VBA releases local object references when the procedure exits. Use Set fld = Nothing after the loop if you want to release that reference earlier.

What should I check if the revised loop still fails?

Verify that tName matches an existing table and that the DAO library is available under Tools > References in the VBA editor. Also check whether other code closes or modifies the database or table while the loop is running.