|
版友樟树:在对表的结构不知情的情况下,要用VBA获取表的主键字段(可能由多个字段组成),该怎么做?
该问题解法如下:
Public Function Keyname(tbname As String, ktype As Long) As String
'功能:获取数据表键列字段名称
'参数:tbname---表名;ktype---键类型(1为主键,2为外键,3为唯一键)
Dim cnn As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Dim i As Long, j As Long
Set cnn = CurrentProject.Connection
Set cat.ActiveConnection = cnn
Set tbl = cat.Tables(tbname)
For i = 0 To tbl.Keys.Count - 1
If tbl.Keys(i).Type = ktype Then
For j = 0 To tbl.Keys(i).Columns.Count - 1
Keyname = Keyname & tbl.Keys(i).Columns(j).Name & ";"
Next
End If
Next
End Function
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|