hi to all:
This code create a ID field:
and i would like create a primary key too in this field...Code:Set fld = tdf.CreateField("ID, dbLong)
fld.Attributes = dbAutoIncrField
tdf.Fields.Append fld
Can anyone tell me the way to do this?
Thanks
Printable View
hi to all:
This code create a ID field:
and i would like create a primary key too in this field...Code:Set fld = tdf.CreateField("ID, dbLong)
fld.Attributes = dbAutoIncrField
tdf.Fields.Append fld
Can anyone tell me the way to do this?
Thanks
Create a new DAO.Index object, set its properties and then append it to the Indexes collection.
Code:Set fld = tdf.CreateField("ID, dbLong)
fld.Attributes = dbAutoIncrField
tdf.Fields.Append fld
Dim idx As DAO.Index
Set idx = New DAO.Index
idx.Name = "PK_Testing"
idx.Fields.Append fld
idx.Primary = True
tdf.Indexes.Append idx
Hi:
I had try your suggestion but VB returns this message:
Run time error3367
It's impossible add.One object with this name already exists in the collecion
the error is in the line market in bold:
Code:Set fld = tdf.CreateField("ID, dbLong)
fld.Attributes = dbAutoIncrField
tdf.Fields.Append fld
Dim idx As DAO.Index
Set idx = New DAO.Index
idx.Name = "PK_Testing"
idx.Fields.Append fld
idx.Primary = True
tdf.Indexes.Append idx
Moved To Database Development
Sorry for the delay in answering.
I assumed you could use the same Field object in both the Fields and Index collections of the TableDef, guess not. This code works for me
Code:Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim idx As DAO.Index
Set db = DBEngine.OpenDatabase("m:\testing\nwind2.mdb")
Set tdf = New DAO.TableDef
tdf.Name = "TestTable"
Set fld = tdf.CreateField("Id", dbLong)
fld.Attributes = dbAutoIncrField
Set idx = tdf.CreateIndex("PK_TestTable")
idx.Fields.Append idx.CreateField("Id", dbLong)
idx.Primary = True
tdf.Indexes.Append idx
tdf.Fields.Append fld
db.TableDefs.Append tdf
db.Close
Hi:
Sory too the delay of the replay...
Yes...your code work...
Thanks for the help