开发者

Examining MS Access Database Keys using VB6

how to check how 开发者_C百科many primary key, composite key in existing table using Visual basic 6.0 and ms access as a database?


One table have only one primary key, that may be simple or composite key.


Add reference ADOX and ADODB library.

Function ShowKeys(tbl As String) As String
   'Add reference ADOX library:  Microsoft ADO Ext. 2.8 for DDL and Security.
   'Add reference ADODB library: Microsoft ActiveX Data Objects
   Dim cat As New ADOX.Catalog
   Dim tbl As ADOX.Table
   Dim idx As ADOX.Index
   Dim col As ADOX.Column
   Dim cnn As New ADODB.Connection

   On Error GoTo errh

   cnn.Open "Provider='Microsoft.Jet.OLEDB.4.0';" & _
        "Data Source= 'Northwind.mdb';"

   Set cat.ActiveConnection = cnn

   For Each tbl In cat.Tables
      If tbl.Name = tbl Then
         If tbl.Indexes.Count <> 0 Then
            For Each idx In tbl.Indexes
               With idx
                  If .PrimaryKey Then
                     For Each col In .Columns
                        ShowKeys = col.Name & ", " & ShowKeys
                     Next
                  End If
               End With
            Next
         End If
      End If
   Next

errh:
   If Err <> 0 Then
      MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
   End If

   Set cat = Nothing
   Set tbl = Nothing
   Set idx = Nothing
   Set col = Nothing
   Set cnn = Nothing
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜