|
-
Dec 9th, 2008, 11:04 AM
#1
Thread Starter
Frenzied Member
[RESOLVED]Create a Primary Key
hi to all:
This code create a ID field:
Code:
Set fld = tdf.CreateField("ID, dbLong)
fld.Attributes = dbAutoIncrField
tdf.Fields.Append fld
and i would like create a primary key too in this field...
Can anyone tell me the way to do this?
Thanks
Last edited by sacramento; Dec 12th, 2008 at 02:13 PM.
-
Dec 9th, 2008, 12:41 PM
#2
Re: Create a Primary Key
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
-
Dec 9th, 2008, 05:35 PM
#3
Thread Starter
Frenzied Member
Re: Create a Primary Key
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
-
Dec 10th, 2008, 02:15 PM
#4
Re: Create a Primary Key
Moved To Database Development
-
Dec 10th, 2008, 03:30 PM
#5
Re: Create a Primary Key
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
-
Dec 12th, 2008, 02:13 PM
#6
Thread Starter
Frenzied Member
Re: Create a Primary Key
Hi:
Sory too the delay of the replay...
Yes...your code work...
Thanks for the help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|