The following code will print the primary keys of all tables to the
Immediate Window. You'll have to modify it if you want to use it in some
other way. Please note that tables sometimes have compound primary keys and
you'll have to deal with that. Anyway:
Public Sub FindPrimaryKeys()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim idx As DAO.Index
Dim fld As DAO.Field
Set db = CurrentDb
Dim i As Integer
For Each tdf In db.TableDefs
If Left(tdf.Name, 4) <> "MSYS" Then
For Each idx In tdf.Indexes
If idx.Primary = True Then
For Each fld In idx.Fields
Debug.Print tdf.Name & " - " & fld.Name
Next
End If
Next
End If
Next
End Sub
Note: code will ignore the Access System tables.
--
--Roger Carlson
MS Access MVP
Access Database Samples:
www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L