473,397 Members | 2,084 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,397 software developers and data experts.

Finding the primary key using VBA

Hi all,

I need to determine the name of the primary key in a table, but this
must be done using VBA.

I was looking for something like,

for each field in someRecordset.fields
if field.type="primaryKey" then
do something
end if
Next

Does anyone know if a property exists for primary key??

Thanks

Mark

Jul 18 '07 #1
4 25313
On Wed, 18 Jul 2007 19:10:36 -0000, mark_aok <ma******@hotmail.com>
wrote:

You can iterate the Indexes collection for the table, looking for one
with the Primary property set to True.

-Tom.

>Hi all,

I need to determine the name of the primary key in a table, but this
must be done using VBA.

I was looking for something like,

for each field in someRecordset.fields
if field.type="primaryKey" then
do something
end if
Next

Does anyone know if a property exists for primary key??

Thanks

Mark
Jul 19 '07 #2
On Jul 18, 3:10 pm, mark_aok <mark_...@hotmail.comwrote:
Hi all,

I need to determine the name of the primary key in a table, but this
must be done using VBA.

I was looking for something like,

for each field in someRecordset.fields
if field.type="primaryKey" then
do something
end if
Next

Does anyone know if a property exists for primary key??

Thanks

Mark
Solved here,
http://groups.google.com/group/comp....dfe3fec1853126

Jul 19 '07 #3
Using DAO:

CurrentDb.TableDefs("MyTable").Indexes("PrimaryKey ").Fields

With a table named 'MyTable' and an Index on your key field named
"PrimaryKey" (which is the Access default name for a table Index
flagged as key..
Ron, King of Chi

On Jul 18, 2:10 pm, mark_aok <mark_...@hotmail.comwrote:
Hi all,

I need to determine the name of the primary key in a table, but this
must be done using VBA.

I was looking for something like,

for each field in someRecordset.fields
if field.type="primaryKey" then
do something
end if
Next

Does anyone know if a property exists for primary key??

Thanks

Mark

Jul 19 '07 #4
Mark-

Try this:

Public Sub PrimKey(tblName As String)
'*******************************************
'Purpose: Programatically determine a
' table's primary key
'Coded by: raskew
'Inputs: from Northwind's debug window:
' Call PrimKey("Products")
'Output: "ProductID"
'*******************************************

Dim db As Database
Dim td As TableDef
Dim idxLoop As Index

Set db = CurrentDb
Set td = db.TableDefs(tblName)
For Each idxLoop In td.Indexes
If idxLoop.Primary = True Then
Debug.Print Mid(idxLoop.Fields, 2)
Exit For
End If
Next idxLoop

db.Close
Set db = Nothing
End Sub

HTH - Bob

mark_aok wrote:
>Hi all,

I need to determine the name of the primary key in a table, but this
must be done using VBA.

I was looking for something like,

for each field in someRecordset.fields
if field.type="primaryKey" then
do something
end if
Next

Does anyone know if a property exists for primary key??

Thanks

Mark
--
Message posted via http://www.accessmonster.com

Jul 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Wagstaff | last post by:
I have an interesting challenge. I have a client that is putting pictures into a single folder on the web server. The files are specific in that the first 5 characters of the filename are the...
0
by: Gabriel Harrison | last post by:
Hi, I have a mysql db with with autoincrementing primary keys in each table. I want to allow a user to add a record using a php webpage. Once the page is added how can I re SELECT that record...
1
by: Randy | last post by:
I have trying to figure out the above topic. Can someone help me with this? I need for staff to be able to go to a dialog box or a form and then be able to enter data in mutliple fields and...
11
by: Ian | last post by:
Hi, I am writing an application and am creating a plug-in interface for it. The application will need to 'find' the plug-in's and will then need to find all accessible methods. The...
2
by: themillerman | last post by:
Hi, I'm an access 2003 novice trying to improve the search ability of my club membership database. I am currently searching for the members surnames by simply running a query (Please enter surname...
2
by: williamz | last post by:
Hi! I am not that experienced with javascript and I am having a problem reading some properties using javascript. I have a menu bar sitting inside a div that is centred. Some menu options drop...
2
by: dotNetDummi | last post by:
Hi experts, (I hope I didn't post my question in the wrong category of the forum) I am using in C#, Asp.Net, SQL Server for my project. I have a situation whereby I need to update a primary...
1
by: Mufasa | last post by:
I have a page that has a master page. I'm writing generic code to find multiple controls on the page (I have a number of controls called tbName1, tbName2, tbName3, ..., tbName20) and rather than...
1
by: kinnu | last post by:
Hi, Am using Data1.Recordset.FindFirst "Odd_Company_Name='" & sstr & "'" to find a company name in my database. but when we enter a company name with ' then its giving error. Ex: Oracle -...
3
by: ahmedalhoseny | last post by:
i have three groups each group consists of five members group 1 members: a b c d e group 2 members: e f g h i group 3 members: j k l m n I need to find all available combinations using...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.