D
David Anderson
I am using the following VBA code in Access 2003 to create a Primary Key in
a table exported from SQL Server (the export process does not preserve the
PKs). However, I have to do something similar for tables with two or more
fields that act as a joint PK. For example, I have an EntrySummary table
with a joint PK of EntrantID and CompID.
Does anyone know how I can use VBA to create a joint PK?
David
===========================================
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim ind As DAO.Index
Set db = CurrentDb()
Set tdf = db.TableDefs("Users")
For Each ind In tdf.Indexes
If ind.Name = "PrimaryKey" Then
'Exit if PK already exists
Exit Sub
End If
Next ind
Set ind = tdf.CreateIndex("PrimaryKey")
With ind
.Fields.Append .CreateField("UserID")
.Primary = True
End With
tdf.Indexes.Append ind
Set ind = Nothing
Set tdf = Nothing
Set db = Nothing
a table exported from SQL Server (the export process does not preserve the
PKs). However, I have to do something similar for tables with two or more
fields that act as a joint PK. For example, I have an EntrySummary table
with a joint PK of EntrantID and CompID.
Does anyone know how I can use VBA to create a joint PK?
David
===========================================
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim ind As DAO.Index
Set db = CurrentDb()
Set tdf = db.TableDefs("Users")
For Each ind In tdf.Indexes
If ind.Name = "PrimaryKey" Then
'Exit if PK already exists
Exit Sub
End If
Next ind
Set ind = tdf.CreateIndex("PrimaryKey")
With ind
.Fields.Append .CreateField("UserID")
.Primary = True
End With
tdf.Indexes.Append ind
Set ind = Nothing
Set tdf = Nothing
Set db = Nothing