I need to reate multiple PK fields on table.
It need to be done using VBA as I have no access to the client's BE db, only to the FE one (Sending him a new version).
This is a long going (few years) developed app.
I can easily add a single field PK using Alter Table Alter Column routines.
I tried using the code below but it will work for the first field only. For the second one I get an error saying a PK is already exist
I have a table ([NewVersion_Tables_Columns]) that hold all the columns I want to make as PK
Thanks,
Tal
It need to be done using VBA as I have no access to the client's BE db, only to the FE one (Sending him a new version).
This is a long going (few years) developed app.
I can easily add a single field PK using Alter Table Alter Column routines.
I tried using the code below but it will work for the first field only. For the second one I get an error saying a PK is already exist
Code:
' --- Set PK (As part of the Index)
strSQL = "SELECT * FROM [NewVersion_Tables_Columns] " & _
"WHERE [dbVersion] > " & LatestDataMDBVersion(strDataMDBFile) & " " & _
"AND Trim([ColumnName] & '') <> '' " & _
"AND [ColumnPrimaryKey] = True " & _
"AND (LCase([Modify]) = 'alter' or LCase([Modify]) = 'add') " & _
"ORDER BY [TableName], [ColumnName] "
Set rs = CurrDB.OpenRecordset(strSQL)
With rs
If .RecordCount > 0 Then
.MoveFirst
Do While Not .EOF
strTableName = .Fields("TableName")
strColumnName = .Fields("ColumnName")
' Create and append a new Index object to the
' Indexes collection of the new TableDef object.
Set indx = DataDB.TableDefs(strTableName).CreateIndex("PrimaryKey")
indx.Fields.Append indx.CreateField(strColumnName)
indx.Primary = True
DataDB.TableDefs(strTableName).Indexes.Append indx
.MoveNext
Loop
.Close
End If
End With
I have a table ([NewVersion_Tables_Columns]) that hold all the columns I want to make as PK
Thanks,
Tal