3

我正在尝试使用关系窗口来查看 MS-Access 数据库中表之间的关系。我隐藏了所有其他表,并在其中一个表中找到了一个外键,看起来它应该链接到另一个表,主键是我需要追踪的信息。

有没有办法在 Access 中列出表的现有外键约束,就像MySQL 中的 SHOW CREATE TABLE 语法一样?

4

1 回答 1

9

DAO 和 ADOSchemas 中的 tabledef 集合将显示外键。

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

关系

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

最后,还有一个隐藏的系统表:

SELECT * FROM MSysRelationships
于 2012-07-31T14:16:48.127 回答