SQL or VBA to set Primary Keys

smed

Registered User.
Local time
Today, 16:16
Joined
Apr 18, 2002
Messages
50
Is it possible to either use SQL or VBA to set a table field to a Primary Key, instead of going into design mode.

Any help would be great.

Smed
 
Try the VBA below.

Sub AddPrimaryX()

Dim dbs As Database
Dim tdf As TableDef
Dim idx As Index

Set dbs = CurrentDb

Set tdf = dbs.TableDefs("YourTableHere")

With tdf
Set idx = .CreateIndex("YourTargetFieldNameHere")
idx.Fields.Append idx.CreateField("YourTargetFieldNameHere")
idx.Primary = True
.Indexes.Append idx

End With

dbs.Close

End Sub
 
Tim,

Originally I coulnd't get it to work.

However I realised I had not enabled the "Microsoft DAO 3.6 Object Libirary" reference.

Now it works Great !

Thanks
Smed
 

Users who are viewing this thread

Back
Top Bottom