设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

如何得到表的所有键、键的类型、对应的字段名

1970-1-1 08:00| 发布者: 未知| 查看: 1549| 评论: 0

 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

最新评论

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2025-4-4 05:17 , Processed in 0.079966 second(s), 17 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部