Detect Primary Key Field

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
 
T

Tim Ferguson

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
 
G

Guest

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
 
B

bob

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
 

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