views:

460

answers:

2

Hey all

I have a bunch of Access databases, each of which has several tables. Each table has a composite primary key, with varying fields comprising that key.

How can I, for a given table, get the name of the primary key and the names of the fields which are used in it? I.e., what's the SQL to do that? (I need to use raw SQL rather than Visual Basic)

BTW I know that I can open the tables in the Access GUI and see the primary keys there but I need to automate the process so that I can modify the primary keys.

thanks! max

A: 

Have a look at the ADOX Object Model. Specifically the Key Object:

With the properties and collections of a Key object, you can:

  • Identify the key with the Name property.

  • Determine whether the key is primary, foreign, or unique with the Type property.

  • Access the database columns of the key with the Columns collection.

  • Specify the name of the related table with the RelatedTable property.

  • Determine the action performed on deletion or update of a primary key with the DeleteRule and UpdateRule properties.

Also, How to list the primary key columns in an Access table

Update: I think the question originally said programmatically: If you want to use TSQL then you need to query the Hidden System Tables

Mitch Wheat
thanks mitch - would you mind giving me some pointers for the sql for that? I never heard of the adox model and have no idea how to get at it...
Max Williams
Indexes are not listed in MSysObjects, nor any other system table that I know of.
David-W-Fenton
How do you use ADOX in SQL, which is what the original questioner is asking for?
David-W-Fenton
@David W. Fenton: you don't. I updated my answer. Original question said 'programmatically'
Mitch Wheat
A: 

Here is an Access VBA function that uses ADOX to get the primary Key columns.

Private Function getPrimaryKeyFields(ByRef strFieldNames() As String) As Integer
On Error GoTo HandleErr

Dim intReturn As Integer

'just get the primary key field here.
Dim idx As ADOX.Index
Dim Table As ADOX.Table
Dim col As ADOX.Column
Dim cat As New ADOX.Catalog

Set cat.ActiveConnection = CurrentProject.Connection

Set Table = cat.Tables(mTableName)
Set idx = Table.Indexes("PrimaryKey")

ReDim strFieldNames(idx.Columns.Count)
Dim intCount As Integer
intCount = 0
For Each col In idx.Columns
    strFieldNames(intCount) = col.Name
    intCount = intCount + 1
Next
intReturn = intCount
Set idx = Nothing
Set Table = Nothing
Set col = Nothing
Set cat = Nothing



ExitHere:
getPrimaryKeyFields = intReturn
    Exit Function

HandleErr:
    Select Case Err.Number
        Case Else

            'put some error handling here.  
            Resume ExitHere
    End Select
' End Error handling block.
End Function

Pass in a string array and it is filled in with the field names. Number of fields is returned by the function. Hope this helps.

Seth

Seth Spearman
I realize that this is not a sql way to do it...but it is a way that will work. Before using this code you need to set a reference to ADOX for your project. To do that...from a code windows Tools/References. Scroll down until you see "Microsoft ADO Ext. 6.0 for DDL and Security." Click the checkbox next to that. Then the code above should work. If you do not SEE that reference then you might have to install it from one of the MDACs. Seth
Seth Spearman