Primary Property Example (MDB)

The following example checks the Primary property of each index on a Products table, and prints the fields making up the primary key:

Sub SetIndex()
    Dim dbs As Database, tdf As TableDef, fld As Field
    Dim idx As Index

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to Products table.
    Set tdf = dbs.TableDefs!Products
    ' Enumerate through Indexes collection of TableDef object.
    For Each idx In tdf.Indexes
        ' Check Primary property of Recordset object.
        If idx.Primary Then
            For Each fld In idx.Fields
                Debug.Print fld.Name
            Next fld
        End If
    Next idx
    Set dbs = Nothing
End Sub