Fix Invalid Object Errors When Looping Through Microsoft Access Fields
Question details
The user needs to iterate through an Access table's fields without an invalid object error.

- 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.
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.
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.
In Access, press Alt+F11 to open the VBA editor, then open the module containing the field loop.
In the procedure, declare Dim db As DAO.Database and Dim fld As DAO.Field. Before the loop, add Set db = CurrentDb.
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.
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.

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.

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.




