Results 1 to 3 of 3

Thread: recognizing a “primary key

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    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.

  2. #2
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    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
  •  



Click Here to Expand Forum to Full Width