PDA

Click to See Complete Forum and Search --> : primary key


xavier
Sep 1st, 1999, 05:28 PM
How do I make my field to a primary key?

here is my code:

Dim myDataBase As Database
Dim myTable As TableDef
Dim myField As field

Set myDataBase = OpenDatabase("c:\Data.mdb")
Set myTable = myDataBase.CreateTableDef("table")
'this the field I want to be the primary key
Set myField = myTable.CreateField("ID", dbLong)

myTable.Fields.Append myField
myDataBase.TableDefs.Append myTable

Thanks
Xavier

------------------

Ludmila
Sep 3rd, 1999, 12:14 AM
You can try this:

Dim td As TableDef
Dim idxNew As Index

Set td = db.CreateTableDef("MyTable")
With td
.Fields.Append .CreateField("ID", dbLong)
.Fields.Append ..................
End With
db.TableDefs.Append td
With td
' Create and append a new Index object to the
' Indexes collection of the new TableDef object.
Set idxNew = .CreateIndex("PrimaryKey")
idxNew.Fields.Append idxNew.CreateField("ID")
idxNew.Primary = True
.Indexes.Append idxNew
End With