Checking for PK

  • Thread starter Thread starter John
  • Start date Start date
John said:
Hi

Is it possible to check via code if a table has a PK?


Yes, but since a table can have multiple indexes, you have to loop through
them all to find out. Here's some quickie, untested "air code":

'----- start of code -----
Function TableHasPK(TableName As String) As Boolean

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim ix As DAO.Index

Set db = CurrentDb
Set tdf = db.TableDefs(TableName)

For Each ix In tdf.Indexes
If ix.Primary Then
TableHasPK = True
Exit For
End If
Next ix

Set tdf = Nothing
Set ix = Nothing

End Function
'----- end of code -----
 
Back
Top