Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the Primary Key in VBA

Status
Not open for further replies.

scoobey

Technical User
Sep 18, 2001
32
GB
Hi there!

I'm trying to create a template form which has buttons on it which perform record manipulation, eg Add, Delete, Next Record, Previous Record etc. In order to do this I really need to automate most of the processes when the form loads. The only thing I want to change when I re-use the form for another table is the record source.
My problem is this. How do I get the form to obtain the primary key from the current record set? I suspect it has something to do with dao.index but i'm not too sure on the coding?

Any ideas or help would be very much appreciated. Thanks!
 
There was just a thread about something like that, with some suggestions thread705-1199120.

Roy-Vidar
 
Here's one way. You'll need to set a reference to the DAO object library appropriate for your version of Access.

Code:
Dim tdf As DAO.Tabledef
Dim idx As DAO.Index
Dim fld As DAO.Field

Set tdf = currentdb.Tabledefs ("YourTable")
For Each idx In tdf.Indexes
    ' Loop through each of the table indexes
    If InStr(idx.Fields, fld.Name) Then
        ' if the field name is in the index and the index is the primary one then it is true
        If idx.Primary = True Then
               ' Its part of the primary key...
            Exit For
        End If
    End If
Next

John
 
Thanks guys! I've now got it working - cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top