Create a primary index

G

Guest

Using Access 97. I am in database 1. I am trying to create a primary key in a
table in database 2 where the field already exists. The code I have come up
with is as follows:

Dim db As DAO.database
Dim td As DAO.TableDef
Dim id As DAO.Index
Dim fd As DAO.Field

Set db = OpenDatabase("c:\ABC.mdb")
Set td = db.TableDefs("Employees")

Set id = td.CreateIndex("PrimaryKey")
Set fd = id.CreateField("EmpID")

id.Primary = True
id.Required = True
id.Unique = True
td.Indexes.Append id
td.Indexes.Refresh

The error I get is "Error 3264: No field defined - Cannot append tabledef or
index."

Any help would be appreciated. Thanks in advance.
 
D

Douglas J. Steele

You've forgotten to append the field fd to the index id.


Dim db As DAO.database
Dim td As DAO.TableDef
Dim id As DAO.Index
Dim fd As DAO.Field

Set db = OpenDatabase("c:\ABC.mdb")
Set td = db.TableDefs("Employees")

Set id = td.CreateIndex("PrimaryKey")
Set fd = id.CreateField("EmpID")

id.Fields.Append fd

id.Primary = True
id.Required = True
id.Unique = True
td.Indexes.Append id
td.Indexes.Refresh
 

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