Click to See Complete Forum and Search --> : Getting Primary Keys of Access Tables with VB


Tasjake
August 22nd, 2001, 06:40 AM
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

Cakkie
August 22nd, 2001, 07:19 AM
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
slisse@planetinternet.be

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