By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

Retrieve primary key

P: n/a
Can anyone help me with the code to retrieve the name of the PrimaryKey
field of the current table?

For instance:
qryMfrsAndBrands is based on the tables: tblBrands and tblMfrs
formMfrsBrands is based on qryMfrsAndBrands
fieldBrandName is on the form and comes from tblBrands via
qryMfrsAndBrands.

I'd like to retrieve the name of the primary key field for tblBrands.

Thanks again.

I'm still very new at this - but not as new as I was yesterday!

-Sue

Aug 11 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sue,

Knowing a Primary Key be one or more fields, this sub will print them
to the debug window:
Sub PrintPrimaryKeys(tblName As String)

Dim fld As DAO.Field
Dim tbl As DAO.TableDef
Dim dbs As DAO.Database
Set dbs = CurrentDb
Set tbl = dbs.TableDefs(tblName)
Dim idx As DAO.Index
Set idx = tbl.Indexes("primarykey")
For Each fld In idx.Fields
Debug.Print fld.Name
Next fld

End Sub

you can call it like:

PrintPrimaryKeys "Employees"
If you know there is just one, you could:
Debug.Print
CurrentDB.TableDefs("tblBrands").Indexes("PrimaryK ey").Fields(0).Name
Chris Nebinger
SueA wrote:
Can anyone help me with the code to retrieve the name of the PrimaryKey
field of the current table?

For instance:
qryMfrsAndBrands is based on the tables: tblBrands and tblMfrs
formMfrsBrands is based on qryMfrsAndBrands
fieldBrandName is on the form and comes from tblBrands via
qryMfrsAndBrands.

I'd like to retrieve the name of the primary key field for tblBrands.

Thanks again.

I'm still very new at this - but not as new as I was yesterday!

-Sue
Aug 11 '06 #2

P: n/a
I'll try it out - thank you!

-Sue

ch************@gmail.com wrote:
Sue,

Knowing a Primary Key be one or more fields, this sub will print them
to the debug window:
Sub PrintPrimaryKeys(tblName As String)

Dim fld As DAO.Field
Dim tbl As DAO.TableDef
Dim dbs As DAO.Database
Set dbs = CurrentDb
Set tbl = dbs.TableDefs(tblName)
Dim idx As DAO.Index
Set idx = tbl.Indexes("primarykey")
For Each fld In idx.Fields
Debug.Print fld.Name
Next fld

End Sub

you can call it like:

PrintPrimaryKeys "Employees"
If you know there is just one, you could:
Debug.Print
CurrentDB.TableDefs("tblBrands").Indexes("PrimaryK ey").Fields(0).Name
Chris Nebinger
SueA wrote:
Can anyone help me with the code to retrieve the name of the PrimaryKey
field of the current table?

For instance:
qryMfrsAndBrands is based on the tables: tblBrands and tblMfrs
formMfrsBrands is based on qryMfrsAndBrands
fieldBrandName is on the form and comes from tblBrands via
qryMfrsAndBrands.

I'd like to retrieve the name of the primary key field for tblBrands.

Thanks again.

I'm still very new at this - but not as new as I was yesterday!

-Sue
Aug 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.