I recently made a very simple little function:
Function x()
Dim Tbl As TableDef
Dim fld As DAO.Field
Set Tbl = currentdb.TableDefs("search")
Debug.Print Tbl.Name
For Each fld In Tbl.Fields
Debug.Print fld.Name
Next fld
To my astonishment, it gave an error: object no longer set/not valid
Then I changed it to
Public Function xx()
Dim Tbl As TableDef
Dim fld As DAO.Field
Dim Dbs As Database
Set Dbs = CurrentDb
Set Tbl = Dbs.TableDefs("search")
Debug.Print Tbl.Name
For Each fld In Tbl.Fields
Debug.Print fld.Name
Next fld
End Function
and it worked.
Can someone tell me why you explicitly have to declare the current database?
TIA
- Nicolaas