Create a primary index

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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
 
Back
Top