日期:2014-05-16  浏览次数:20897 次

ACCESS中建了主键机器自动产生的约束名,请问有什么方法能得到这个约束名
ACCESS中建了主键机器自动产生的约束名,请问有什么方法能得到这个约束名,请高手指点

------解决方案--------------------
'获取指定表名的主键名
Public Sub testKey()
On Error GoTo err_this
Dim strTblName As String

Dim adoxcat As New ADOX.Catalog
Dim adoxtbl As New ADOX.Table
Dim adoxkey As New ADOX.Key
Dim intKeys As Integer
Dim intKey As Integer

strTblName = "表名 "
adoxcat.ActiveConnection = CurrentProject.Connection

Set adoxtbl = adoxcat.Tables(strTblName)
Debug.Print adoxcat.Tables(strTblName).Name
' Set adoxkey = adoxtbl.Keys

' For Each adoxkey In adoxtbl.Keys
intKeys = adoxtbl.Keys.Count - 1
For intKey = 0 To intKeys
Debug.Print adoxtbl.Keys.Item(intKey).Name
Next intKey
' Debug.Print adoxkey.Columns
Debug.Print adoxkey.Name, adoxkey.Type

' Next

Set adoxkey = Nothing
Set adoxtbl = Nothing
Set adoxcat = Nothing
exit_sub:
Exit Sub
err_this:
MsgBox Err.Description, vbOKOnly + vbCritical, Err.Number
Resume exit_sub
End Sub