CreateField method for TableDef (1 Viewer)

ajetrumpet

Banned
Local time
Today, 13:47
Joined
Jun 22, 2007
Messages
5,638
Folks,

I have this code:
Code:
Dim tbldef As TableDef, rs As Recordset, rsOLD As Recordset, 
   myemail As String, fld As Field

Set tbldef = CurrentDb.TableDefs("ContactCheckListTable")
[COLOR="Red"]Set fld = tbldef.CreateField("contact", dbBoolean)[/COLOR]

   With tbldef
      .Fields.Append fld
   End With

Set tbldef = Nothing
I am getting an error "object invalid or no longer set" on the line in red. I have also tried this method of code to accomplish the same thing:
Code:
Dim tbldef As TableDef, rs As Recordset,
   rsOLD As Recordset, myemail As String

Set tbldef = CurrentDb.TableDefs("ContactCheckListTable")

   With tbldef
      [COLOR="red"].Fields.Append .CreateField("contact", dbBoolean)[/COLOR]
   End With

Set tbldef = Nothing
With this code I get the exact same error, again on the line in red. I have used the second method before, and it worked fine, but the only difference was that I was creating a tabledef instead of setting one that already existed in the database as a table. I am lost on this. Can someone shed some light on why I am getting this error? Thanks!
 

Attachments

  • error.jpg
    error.jpg
    95.5 KB · Views: 184
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 19:47
Joined
Sep 12, 2006
Messages
15,660
it might be just the currentdb

try adding

dim dbs
set dbs=currentdb

THEN
Set tbldef = Dbs.TableDefs("ContactCheckListTable")

I have had the currentdb falling out of scope in some circumstances, i never understand why, so i always set a pointer to it now
 

ajetrumpet

Banned
Local time
Today, 13:47
Joined
Jun 22, 2007
Messages
5,638
worked like a charm Dave. I don't understand it either, but we'll go with it. thanks. :)
 

Users who are viewing this thread

Top Bottom