problem creating table and adding variables

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
 
P

Paolo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top