DAO TableDef Not Working

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

OK, I'm sure I've done this before, but I can't remember how. All I want to
do is get the list of field names from a table. The table will always be
called TEMP_IMPORT. Here's the code I have:

Dim objT As DAO.TableDef
Dim F As DAO.Field
Dim strCols As String

Set objT = CurrentDb.TableDefs("TEMP_IMPORT")

For Each F In objT.Fields

strCols = strCols & "," & F.Name

Next

Everytime I run it, I get an error message saying "Object is invalid or is
no longer set." It highlights the code "objT.Fields".

What am I doing wrong? I'm on Access2k, Windows2k. Thanks.
 
MDW said:
OK, I'm sure I've done this before, but I can't remember how. All I
want to do is get the list of field names from a table. The table
will always be called TEMP_IMPORT. Here's the code I have:

Dim objT As DAO.TableDef
Dim F As DAO.Field
Dim strCols As String

Set objT = CurrentDb.TableDefs("TEMP_IMPORT")

For Each F In objT.Fields

strCols = strCols & "," & F.Name

Next

Everytime I run it, I get an error message saying "Object is invalid
or is no longer set." It highlights the code "objT.Fields".

What am I doing wrong? I'm on Access2k, Windows2k. Thanks.

You need to get the TableDef object from a Database object that remains
in existence while you use the TableDef. For example:

Dim db As DAO.Database
Dim objT As DAO.TableDef
Dim F As DAO.Field
Dim strCols As String

Set db = CurrentDb

Set objT = db.TableDefs("TEMP_IMPORT")

For Each F In objT.Fields

strCols = strCols & "," & F.Name

Next

Set objT = Nothing
Set db = Nothing
 
AHA! I knew it some some silly detail I'd forgotten about. Works like a
charm. Thanks, DG.
 
Back
Top