Detect Primary Key Field

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Thanks for taking the time to read my question.

Is there a way to tell (in vba) if a field in a table is the Primary Key or
autonumber?

Right now I am getting some field properties using:

db.TableDefs(x).Fields(i)

Thanks,

Brad
 
Is there a way to tell (in vba) if a field in a table is the Primary
Key or autonumber?

You need to iterate through the Indexes collection and look for the Primary
property, and then iterate the Fields collection of that Index. Remember
that there may be more than one Field in a Primary Key.

Hope that helps


Tim F
 
Thanks Tim, That was perfect.

Brad

Tim Ferguson said:
You need to iterate through the Indexes collection and look for the Primary
property, and then iterate the Fields collection of that Index. Remember
that there may be more than one Field in a Primary Key.

Hope that helps


Tim F
 
Try this:

Function PrimKeys()
Dim dbCurr As DAO.Database
Dim tdfCurr As DAO.TableDef
Dim idxCurr As DAO.index
Dim fldCurr As DAO.Field
Dim strTableName As String


strTableName = "tbltest" 'put name of table here
Set dbCurr = CurrentDb()
Set tdfCurr = dbCurr.TableDefs(strTableName)
Set idxCurr = tdfCurr.Indexes("PrimaryKey")
For Each fldCurr In idxCurr.Fields
Debug.Print fldCurr.name
Next fldCurr



End Function


Bob Galway
 
Back
Top