A while back I wrote a post on using temp tables in your code and I mentioned using Alter table statements to modify your temp tables on the fly. Today I’m going to talk about adding a primary key using code.

Click for a free quote.

MS Access Report Generator

Primary Keys are good in Temp Tables
They can help up speed your queries and should be added when needed. To add a AutoNumber key that is also a primary key you will need to use two statments, the first creating the field and the second making it a primary key using the following syntax:

Notice in the second statement Primary Key has a space between the words, and if you ever need a composite primary key just separate it by commas:


In the following example we use the table name tblTemp and the column name TempID as the primary key:

Here we used the column name as the index name, making it easier to remember later what is the primary key name when you need to search on the specific index.

I encourage you to continue using temp tables in your code and bringing improved functionality to your Access applications.