Code to get the primary key of table using ADO and VBA

  • Thread starter Faysal Aberkane
  • Start date
F

Faysal Aberkane

Dim db As New ADODB.Connection
Dim resultsRS As New ADODB.Recordset

'open your database connection first

Function getPrimaryKeyName$(tableName$)
Set resultsRS = db.OpenSchema(adSchemaPrimaryKeys, Array(Empty,
Empty, tableName))
getPrimaryKeyName = resultsRS !COLUMN_NAME
End Function

I know that people often want to know how to do that (because I did)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top