Access表中 哪个字段是主键

utada776 2006-03-20 10:33:07
写段代码
得知Access表中 哪个字段是主键
没有主键 返回空
...全文
201 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yxx7347 2006-04-03
大侠,您的方法返回的是主键的字段名。我要的是主键名(设置主键时Access生成的主键名)不是主键所在的字段名。
回复
of123 2006-03-20
ADOX:

Private Sub Form_Click()
Debug.Print Get_Primary("C:\Program Files\Microsoft Visual Studio\VB98\nwind.mdb", "suppliers")
End Sub

Public Function Get_Primary(ByVal databasename As String, ByVal tablename As String) As String
Dim cat As New ADOX.Catalog
Dim ndx As ADOX.Index

cat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source=" & databasename

For Each ndx In cat.Tables(tablename).Indexes
If ndx.PrimaryKey Then
Get_Primary = ndx.Columns(0).Name
Exit For
End If
Next
End Function
回复
of123 2006-03-20
DAO:

Private Sub Form_Click()
Debug.Print Get_Primary("C:\Program Files\Microsoft Visual Studio\VB98\nwind.mdb", "suppliers")
End Sub

Public Function Get_Primary(ByVal databasename As String, ByVal tablename As String) As String

Dim td As DAO.TableDef
Dim db As DAO.database
Dim nx As DAO.Index

Set db = OpenDatabase(databasename)
Set td = db.TableDefs(tablename)
For Each nx In td.Indexes
If nx.Primary = True Then
Get_Primary = nx.Fields(0).Name
Exit For
End If
Next
End Function
回复
相关推荐
发帖

1187

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2006-03-20 10:33
社区公告
暂无公告