tags:

views:

144

answers:

2

how to check how many primary key, composite key in existing table using Visual basic 6.0 and ms access as a database?

+2  A: 

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
volody
+1. I haven't checked the code, but ADOX is definitely the tool to use.
MarkJ
MarkJ - ADOX is not the only solution. You can also use DAO to do all this as well.
Tony Toews
ADO also has the OpenSchema method on Connection objects.
Bob Riemersma
+4  A: 

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

shinod