CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2001
    Location
    Belgium
    Posts
    1

    Getting Primary Keys of Access Tables with VB

    Does anyone have an idee how I can get, in any way, the primary key of an table (in Microsoft Access) if you don't know the tablestructure? I'm on a project in Visual Basic where I have to get to know in one way or another what the primary key of each table is. But I have no idee if it is possible and if so, how to do it. If anyone has an idee, please let me know, it would help me a lot. Thanx


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Getting Primary Keys of Access Tables with VB

    This function get's the name of the fields in the primary key of a table, note that DB is a DAO databaseobject that points to a database.

    public Function GetPK(Tablename as string)

    Dim td as TableDef
    Dim idx as Index
    Dim fld as Field
    Dim Flds as string

    set td = db.TableDefs(Tablename)

    for Each idx In td.Indexes
    If idx.Primary then
    for Each fld In idx.Fields
    Flds = Flds & ":" & fld.Name
    next fld
    End If
    next idx

    GetPK = mid(Flds, 2)

    End Function




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

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

Featured