简体   繁体   中英

Getting a list of foreign key constraints for a table in ms-access?

I'm trying to use the relationships window to view relations between tables in an MS-Access database. I hid all of the other tables, and found a foreign key in one of the tables that appears like it should link to another table with a primary key the information I need to track down.

Is there any way to list the existing foreign key constraints on a table in Access, much like the SHOW CREATE TABLE syntax in MySQL ?

The tabledef collection in DAO and ADOSchemas will show foreign keys.

Dim tdf As TableDef
Dim db As Database
Dim ndx As Index

Set db = CurrentDb

For Each tdf In db.TableDefs
    For Each ndx In tdf.Indexes
        If ndx.Foreign = True Then
            Debug.Print tdf.Name, ndx.Name
            For Each fld In ndx.Fields
                Debug.Print fld.Name
            Next
        End If
    Next
Next

Relation

Dim rel As DAO.Relation
Dim db As Database
Set db = CurrentDb

For Each rel In db.Relations
    Debug.Print rel.Name, rel.ForeignTable
    For Each fld In rel.Fields
        Debug.Print fld.Name
    Next
Next

ADOSchema

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = CurrentProject.Connection

Set rs = cn.OpenSchema(adSchemaForeignKeys)

Do While Not rs.EOF
    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i).Name & ":- " & rs.Fields(i)
    Next
    rs.MoveNext
Loop

Finally, there is a hidden system table:

SELECT * FROM MSysRelationships

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM