problem creating table and adding variables

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

Hello,
I need to create a table and add fields. I tried the following text based
on an example in a book, and receive the error shown below. What would you
suggest to get the sub running?


Public Sub test()

Dim dbs1 As Database
Dim tdf1 As TableDef
Dim fld1 As Field

Set dbs1 = CurrentDb
Set tdf1 = dbs1.CreateTableDef("Data_File_Test")
Set fld1 = tdf1.CreateField("Test_Field_1", dbLong)

' error here is ... “No field defined.. cannot append Tabledef or Indexâ€
dbs1.TableDefs.Append tdf1


Debug.Print "done"

End Sub
 
Hi Keith,

first you have to append the field to the fields collection and then you can
append the table to the tables collection. The following code works correctly
Public Sub test()

Dim dbs1 As Database
Dim tdf1 As TableDef
Dim fld1 As Field

Set dbs1 = CurrentDb
Set tdf1 = dbs1.CreateTableDef("Data_File_Test")
Set fld1 = tdf1.CreateField("Test_Field_1", dbLong)

tdf1.Fields.Append fld1
dbs1.TableDefs.Append tdf1

Debug.Print "done"

end sub

HTH Paolo
 
Back
Top