PC Review


Reply
Thread Tools Rate Thread

How to create index on existing field

 
 
=?Utf-8?B?QnJpYW4=?=
Guest
Posts: n/a
 
      20th Jun 2004
How can I add an index on an existing field programmatically? The .CreateField method assumes that the field is being created as part of the Index collection. I can manually add/remove indexes on existing fields, but I need a way to do it programmatically for remote distribution of changes.
 
Reply With Quote
 
 
 
 
Marshall Barton
Guest
Posts: n/a
 
      20th Jun 2004
Brian wrote:

>How can I add an index on an existing field programmatically? The .CreateField method assumes that the field is being created as part of the Index collection. I can manually add/remove indexes on existing fields, but I need a way to do it programmatically for remote distribution of changes.



You are misreading the creating a field Help topic. It's
not trying to get you to create a field in your table, it's
the table's Index collection that you have to create the
field object in. The index needs to know which field in the
table to use to do its job so you have to create the
field(s) there too.

Try rereading the Help and examples and see if you get a
clearer picture now.

Beside using CreateIndex and CreateField, you
can also use an SQL DDL statement,
CREATE INDEX
or
ALTER TABLE ADD CONSTRAINT

and if you use the ALTER TABLE ADD COLUMN statement to add a
new field to the table, you can use a CONSTRINT clause to
add the index at the same time.
--
Marsh
MVP [MS Access]
 
Reply With Quote
 
=?Utf-8?B?QnJpYW4=?=
Guest
Posts: n/a
 
      20th Jun 2004
Well, I am sure I don't understand the help very well, but I do understand that the field is created in the table's index collection if I use the .CreateField method. However, I also know that I can create a field in the table manually (in table design view), then later go back and add an index, also manually

Doesn't a field reside in the table's fields collection when it is created without an index? If so, is the field actually moved to the index collection when I manually add the index? I am just trying to find a way to add the index after the fact by issuing a "hotfix" without having to get access to table design view.

"Marshall Barton" wrote:

> Brian wrote:
>
> >How can I add an index on an existing field programmatically? The .CreateField method assumes that the field is being created as part of the Index collection. I can manually add/remove indexes on existing fields, but I need a way to do it programmatically for remote distribution of changes.

>
>
> You are misreading the creating a field Help topic. It's
> not trying to get you to create a field in your table, it's
> the table's Index collection that you have to create the
> field object in. The index needs to know which field in the
> table to use to do its job so you have to create the
> field(s) there too.
>
> Try rereading the Help and examples and see if you get a
> clearer picture now.
>
> Beside using CreateIndex and CreateField, you
> can also use an SQL DDL statement,
> CREATE INDEX
> or
> ALTER TABLE ADD CONSTRAINT
>
> and if you use the ALTER TABLE ADD COLUMN statement to add a
> new field to the table, you can use a CONSTRINT clause to
> add the index at the same time.
> --
> Marsh
> MVP [MS Access]
>

 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      20th Jun 2004
Even though the field may exist in the TableDef's Fields collection, you
still have to create a field to add to the Index's Fields collection.

Here's a slightly abridged version from the Help file:

Sub CreateIndexX()

Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim idxCountry As Index

Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind!Employees

With tdfEmployees
' Create first Index object, create and append Field
' objects to the Index object, and then append the
' Index object to the Indexes collection of the
' TableDef.
Set idxCountry = .CreateIndex("CountryIndex")

With idxCountry
.Fields.Append .CreateField("Country")
.Fields.Append .CreateField("LastName")
.Fields.Append .CreateField("FirstName")
End With
.Indexes.Append idxCountry

End With

dbsNorthwind.Close

End Sub



--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Brian" <(E-Mail Removed)> wrote in message
news:5CC79EC9-D67D-4EED-9481-(E-Mail Removed)...
> Well, I am sure I don't understand the help very well, but I do understand

that the field is created in the table's index collection if I use the
..CreateField method. However, I also know that I can create a field in the
table manually (in table design view), then later go back and add an index,
also manually
>
> Doesn't a field reside in the table's fields collection when it is created

without an index? If so, is the field actually moved to the index collection
when I manually add the index? I am just trying to find a way to add the
index after the fact by issuing a "hotfix" without having to get access to
table design view.
>



 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      20th Jun 2004
"Brian" <(E-Mail Removed)> wrote in message
news:5CC79EC9-D67D-4EED-9481-(E-Mail Removed)
> Well, I am sure I don't understand the help very well, but I do
> understand that the field is created in the table's index collection
> if I use the .CreateField method. However, I also know that I can
> create a field in the table manually (in table design view), then
> later go back and add an index, also manually
>
> Doesn't a field reside in the table's fields collection when it is
> created without an index? If so, is the field actually moved to the
> index collection when I manually add the index? I am just trying to
> find a way to add the index after the fact by issuing a "hotfix"
> without having to get access to table design view.


I think what you're not understanding is that the TableDef object -- the
definition of the table itself -- has a Fields collection that
represents all the fields in the table, and the Index object -- the
definition of an index on the table -- *also* has a Fields collection,
which represents the fields (selected from the fields in the table) that
are part of this index. The two Fields collections are different
things.

If you were creating a table and index in code, you would first create
the TableDef object and add fields to it. Then you would create the
index and add fields -- with names chosen from among the fields you
added to the TableDef -- to that. The Index.CreateField method doesn't
actually create a new field in the table, it just creates a field object
that can be added to the index.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
=?Utf-8?B?QnJpYW4=?=
Guest
Posts: n/a
 
      20th Jun 2004
Thank you. Your last statement clarified all. I was getting some sort of "name already exists" error when I was trying to create the field object in the index collection, so I assumed that it was because the field already existed in the table. Having copied in a fresh set of code that evidently resolved whatever syntax error was generating that message, everything works now.

"Dirk Goldgar" wrote:

> "Brian" <(E-Mail Removed)> wrote in message
> news:5CC79EC9-D67D-4EED-9481-(E-Mail Removed)
> > Well, I am sure I don't understand the help very well, but I do
> > understand that the field is created in the table's index collection
> > if I use the .CreateField method. However, I also know that I can
> > create a field in the table manually (in table design view), then
> > later go back and add an index, also manually
> >
> > Doesn't a field reside in the table's fields collection when it is
> > created without an index? If so, is the field actually moved to the
> > index collection when I manually add the index? I am just trying to
> > find a way to add the index after the fact by issuing a "hotfix"
> > without having to get access to table design view.

>
> I think what you're not understanding is that the TableDef object -- the
> definition of the table itself -- has a Fields collection that
> represents all the fields in the table, and the Index object -- the
> definition of an index on the table -- *also* has a Fields collection,
> which represents the fields (selected from the fields in the table) that
> are part of this index. The two Fields collections are different
> things.
>
> If you were creating a table and index in code, you would first create
> the TableDef object and add fields to it. Then you would create the
> index and add fields -- with names chosen from among the fields you
> added to the TableDef -- to that. The Index.CreateField method doesn't
> actually create a new field in the table, it just creates a field object
> that can be added to the index.
>
> --
> Dirk Goldgar, MS Access MVP
> www.datagnostics.com
>
> (please reply to the newsgroup)
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a field using existing information hrbsh97 Microsoft Access 3 14th May 2008 03:39 PM
How to create a table of contents and index for an existing docume =?Utf-8?B?TXNMZWVBYmxhemE=?= Microsoft Word New Users 1 17th Feb 2006 08:39 PM
How to create multiple field UNIQUE index? ljubo lecic via AccessMonster.com Microsoft Access Forms 3 13th Mar 2005 01:47 AM
add index to existing field =?Utf-8?B?SmFkZW4=?= Microsoft Access Database Table Design 2 20th Jan 2005 05:45 PM
Re: How do I create an index field? Douglas J. Steele Microsoft Access Database Table Design 0 30th Jun 2003 11:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:22 AM.