Dear VB users,
Please can someone tell me how to recognize a “primary key” in a field in a Access database.
Can you give a source.
Nice regards,
Michelle.
Printable View
Dear VB users,
Please can someone tell me how to recognize a “primary key” in a field in a Access database.
Can you give a source.
Nice regards,
Michelle.
Something like this should work.
To try this:
Create a new project (standard exe)
Put a commandbutton on the form using the default name.
Put a DAO.datacontrol on the form using the default name.
Connect the datacontrol to your database.
(If you don't want to use a datacontrol set a reference to a microsoft DAO object library and make a connection to the database.)
Copy this code into the form (with a datacontrol).
[Edited by LAURENS on 10-12-2000 at 08:44 AM]Code:Public Function FindPrimaryKey(strTableName As String, strRetKey As String) As Boolean
Dim tblTabel As DAO.TableDef
Dim indIndex As DAO.Index
Dim fldField As DAO.Field
FindPrimaryKey = False
For Each indIndex In Data1.Database.TableDefs(strTableName).Indexes
If indIndex.Primary = True Then
FindPrimaryKey = True
strRetKey = indIndex.Name & vbCrLf & "Contains these field:"
For Each fldField In indIndex.Fields
strRetKey = strRetKey & vbCrLf & fldField.Name
Next fldField
Exit For
End If
Next indIndex
End Function
Private Sub Command1_Click()
Dim strKey As String
If FindPrimaryKey("Your Tablename", strKey) Then
MsgBox "Prime key = " & strKey
End If
End Sub
Hello LAURENS,
Thanks for your information. It works!
Nice regards,
Michelle.