We help IT Professionals succeed at work.

How to create a multi-field primary key in Access using DAO in VBA?

Dave Goodmanson
on
423 Views
Last Modified: 2017-03-22
I have a task in Access 2016 where I'm required to create a primary key consisting of multiple fields.  I need to do it in VBA code using the DAO model.  This is because it is a repetitive task and I need to loop through multiple tables setting a primary key each time the tables are replaced.  From what I can gather, what is needed are multiple fields to be added to the indexes collection and the primary key properties are set before the index is appended to the collection.  I would really prefer to receive some example code using VBA and DAO in Access so that I can work with the example and adapt it to my situation.  Thank you.
If Helen Feddema is available, that would be good.
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
You need to use the Constraint clause of Alter Table

https://msdn.microsoft.com/en-us/library/bb177883(v=office.12).aspx
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
President, J Street Technology
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Dave GoodmansonData Analyst

Author

Commented:
I want to thank those who responded.  I appreciate Gustav's pseudo-code, that has helped my understanding of this issue.  The responses from Joe and Armen are relevant but in this situation I am using these keys on a temporary basis.  Once they have done their job on the temp tables they are set up on, I can delete them at the end.  But it is handy to know the code to create them quickly.
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Pseudo-code? Please - I provided the exact answer to your question.

/gustav
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Since you apparently didn't understand my link, here's further explanation with the link obfuscated so hopefully, you'll get to see it before it gets deleted.

http  ://  www     dot      orafaq      dot     com/faq/what_are_the_difference_between_ddl_dml_and_dcl_commands

I never write code when I have the option of using a query so my choice for modifying the schema would be DDL as described in the original link.  You run the DDL using .Execute
Dave GoodmansonData Analyst

Author

Commented:
I obviously hadn't realised what a delicate group some of the Experts can be!  If I didn't understand something it is probably why I'm posting here in the first place.  Ask an expert.  Well I reconstructed your link and went to the oracle faq site.  OK, that's fine, so it has comments about various types of data language.  That's great, you have an opinion, I didn't find that it had a lot to do with my question.  But my question has been answered, and I've accepted the solution, so that's it.  Thank you!  Next question.
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
The first link explains how to do what you asked to do by using a query rather than writing code.
Here is the relevant example

This example creates a new table called MyTable with two text fields, a Date/Time field, and a unique index made up of all three fields.
Sub CreateTableX2()

    Dim dbs As Database

    ' Modify this line to include the path to Northwind
    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    ' Create a table with three fields and a unique
    ' index made up of all three fields.
    dbs.Execute "CREATE TABLE MyTable " _
        & "(FirstName CHAR, LastName CHAR, " _
        & "DateOfBirth DATETIME, " _
        & "CONSTRAINT MyTableConstraint UNIQUE " _
        & "(FirstName, LastName, DateOfBirth));"

    dbs.Close

End Sub
 
The second link (obfuscated) was to a site that explained what DDL, DML, DCL, and TCL are since not many people get beyond DML.
Dave GoodmansonData Analyst

Author

Commented:
That's great, thanks very much Pat.