-
Oct 12th, 2000, 07:05 AM
#1
Thread Starter
Hyperactive Member
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.
-
Oct 12th, 2000, 07:38 AM
#2
Addicted Member
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).
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
[Edited by LAURENS on 10-12-2000 at 08:44 AM]
Regards,
Laurens
Using VB5 Enterprise edition SP3
VB6 Enterprise edition SP5
-
Oct 12th, 2000, 01:09 PM
#3
Thread Starter
Hyperactive Member
Hello LAURENS,
Thanks for your information. It works!
Nice regards,
Michelle.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|