
Add field to index thru DAO
Hi Michael,
You need to delete the old index and then create a new one with the
same name and add the fields to it. You cannot just add a field to an
existing index. Don't ask me why <g>.
Something like this:
Dim db As Database
Dim MyDef As TableDef
Dim MyIdx As Index
Dim Myfld As Field
Set db = CurrentDb
Set MyDef = db.TableDefs("MyTable")
MyDef.Indexes.Delete ("MyIndex")
Set MyIdx = MyDef.CreateIndex("MyIndex")
Set Myfld = MyIdx.CreateField("MyTextField", dbText, 50)
MyIdx.Fields.Append Myfld
Set Myfld = myIdx.CreateField("longfield", dbLong)
MyIdx.Fields.Append Myfld
MyDef.Indexes.Append MyIdx
-- Andy
Quote:
>Has anyone tried adding a field to an index programatically thru DAO
>before? I have written the following code to do so, but it will not append
>the field correctly (Invalid Operation error occurrs):
> Set myidx = MyDef.Indexes("JOB_NUM")
> Set myfield = myidx.CreateField("SEQ_NO")
> myidx.Fields.Append myfield
>There is a field in the table called SEQ_NO, and it is an integer. What am
>I doing wrong here?
>Thanks!!
>--
>Michael W. Shrader
>TechLore Solutions
>Houston, TX