Public Function displaykey()
Dim kyForeign As New ADOX.Key
Dim cat As New ADOX.Catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb;"
cat.ActiveConnection = CurrentProject.Connection
For Each kyForeign In cat.Tables("ColorSet").Keys
Debug.Print kyForeign.Name & kyForeign.Type
Next
End Function
常量 值 说明
adKeyPrimary 1 默认值。此键是主键。
adKeyForeign 2 此键是外键。
adKeyUnique 3 此键是唯一键。
在这里提醒大家注意:
键名不等于字段名,要获得对应的字段名看以下代码:
Function DisplayAllKeyAndColumnName()
Dim Tbl As ADOX.Table
Dim Cat As New ADOX.Catalog
Dim Key As New ADOX.Key
Dim Col As New ADOX.Column
Cat.ActiveConnection = CurrentProject.Connection
For Each Key In Cat.Tables("表1").Keys
Debug.Print "键名:"; Key.Name & "; 键类型:" & Key.Type
For Each Col In Key.Columns
Debug.Print "对应字段名:" & Col.Name
Next Col
Next Key
End Function