473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine Primary Key through VBA

Hi all,

I have a situation where I need to determine a specific table's
primary key, and then output it.

I have tried the Database Object, and the Record Object, but I've had
no luck.

Ideally there would be something like,

Record(0).index , or .type, or something that would determine the
primary key.

Anyone have any ideas?

Mark

Jul 18 '07 #1
1 5333
Loop through the Indexes collection to find the one that has the Primary
property set. Note that the primary key consist of more than one field.

The example below shows how to loop the indexes, and assigns upper case P
for the first field in the primary key, or the lower case p for subsequent
fields. It's part of this routine:
http://allenbrowne.com/AppRelReportCode.html

Note that if you were trying to identify the AutoNumber field, you will need
to check the Attributes of the Field in the TableDef. There's an example in
the DescribeFields( ) function on the same page (above.)

Private Function DescribeIndexFi eld(tdf As DAO.TableDef, strField As String)
As String
'Purpose: Indicate if the field is part of a primary key or unique
index.
'Return: String containing "P" if primary key, "U" if uniuqe index,
"I" if non-unique index.
' Lower case letters if secondary field in index. Can have
multiple indexes.
'Arguments: tdf = the TableDef the field belongs to.
' strField = name of the field to search the Indexes for.
Dim ind As DAO.Index 'Each index of this table.
Dim fld As DAO.Field 'Each field of the index
Dim iCount As Integer
Dim strReturn As String 'Return string

For Each ind In tdf.Indexes
iCount = 0
For Each fld In ind.Fields
If fld.Name = strField Then
If ind.Primary Then
strReturn = strReturn & IIf(iCount = 0, "P", "p")
ElseIf ind.Unique Then
strReturn = strReturn & IIf(iCount = 0, "U", "u")
Else
strReturn = strReturn & IIf(iCount = 0, "I", "i")
End If
End If
iCount = iCount + 1
Next
Next

DescribeIndexFi eld = strReturn
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"mark_aok" <ma******@hotma il.comwrote in message
news:11******** *************@z 24g2000prh.goog legroups.com...
Hi all,

I have a situation where I need to determine a specific table's
primary key, and then output it.

I have tried the Database Object, and the Record Object, but I've had
no luck.

Ideally there would be something like,

Record(0).index , or .type, or something that would determine the
primary key.

Anyone have any ideas?

Mark
Jul 18 '07 #2

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

Similar topics

4
6448
by: Phillip J. Allen | last post by:
Hi all, I have a table with an “autonumber” primary key field that also acts as a foreign key in 2 other tables. I would like to programmatically add a new record to the first table and the other 2 child tables. The reason for doing this is to copy one complete record (parent and child table records) into a new record so the user can make a few modifications. So how do I determine what will be the next auto-generated number for my...
7
12093
by: adm | last post by:
There are a few ways to go about this, but what is the fastest when called from VBA? This if for an ADP with a SQL Server back-end.
11
2122
by: LordHog | last post by:
Hello, I recently wrote an application that is used for testing units in a burn-in chamber. It uses two external library that require the use of P\Invoke in order to work with them. There is a primary thread and a secondary polling thread. Every once in a while the process will consume close to 100% of the CPU, but I am unclear why. I am wondering how would I go about determine the cause of the problem? What sort of tools or...
3
1841
by: jaYPee | last post by:
is there a way to know if the form is edited or not after calling addnew? Me.BindingContext(DsStudentCourse1, "Students").AddNew() because i got an error after closing the form. because in my form closing event i have a code to call the EndCurrentEdit to determine if the dataset has changes or not. here is my code in closing event Private Sub Students_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)...
15
9641
by: Robby Russell | last post by:
I am trying to track down a method of determining what a sequence name is for a SERIAL is in postgresql. For example, CREATE TABLE foo (id SERIAL PRIMARY KEY NOT NULL, bar TEXT); \d foo Table "public.foo" Column | Type | Modifiers
6
2203
by: Robin Lawrie | last post by:
Hi, Looking for some help here! I'm adding records to a SQL 2000 DB using the objRS.AddNew and objRS.Update methods. What I'd like to do is determine the new primary key value of the added record.
7
3733
by: bazzer | last post by:
i am having trouble updating a datset because i get the follwing error in the Data Adapter configuration Wizard: Generated UPDATE statement. could not determine which columns uniquely identify the rows for "Customers" 'Customers' is the only table from the database that im using for this query, so it cant be anything to do with a problem of having multiple tables. and the primary key is set. so i dont understand what the problem is.
6
1724
by: erick-flores | last post by:
Hello, I want to make a report for a single compressor. Instead of showing a report for all the compressors. I would like to place a button in the compressor form, so whenever I want to display a report from that particular compressor I click the button and the report will show ONLY that compressor. Each compressor has it own primary key. So i guess I need to match that pk with my report. Can somebody give me some ideas ? I am pretty...
3
4685
by: Jeff | last post by:
Is there a way in C# to determine the Primary Domain Controller when a users logs in on to a network. I would prefer not to use WMI query method as this may not be available. Regards Jeff
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.