Checking for PK

D

Dirk Goldgar

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 -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top