473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Retrieve primary key

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
2 4211
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however...
0
by: Dimitris Katsikas | last post by:
Hi everybody, I am trying to find a way to export automatically an entire database schema into an .xsd A graphical way is to drag & drop the tables from Server Connection into an empty .xsd...
3
by: ben.werdmuller | last post by:
Hi, Is there an easy way in ASP/VBscript to grab an autonumber (primary key) field just after an SQL insert? This is probably easy, but I'm stuck .. Cheers.
0
by: refer_to_website | last post by:
I am using VB.NET to insert a record into an Access database. The database table uses an AutoNumber for its primary key. How can I determine what value Access assigned to the primary key? All...
13
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
1
by: mrkselm | last post by:
Hi, I am stuck with a problem in MS Access which does not occur in SQL Server and I have been banging my head against the wall for a couple of days now trying to resolve it. Namely, when I...
3
by: olorunfunmi | last post by:
hi there, pls how can i retrieve the value of a primary key in a table using a stored procedure. i tried sp_pkeys but that just displays the column name of the primary key and not the value thanks
2
by: singhanand2009 | last post by:
how could i retrieve data from database for a particular field. example- i have got a table having id as primary key, password, and other entity. how could i retrieve only one colum such as id.
1
by: help4me | last post by:
I am doing a survey with many pages. Everyone is given the same password to get into the survey (bulk mailing) but since it is to be done anonymously, they have no unique id assigned to them. My...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.