Solved

How do I copy all indexes of one MS-Access table to another via DAO?

Posted on 2008-10-27
2
275 Views
Last Modified: 2012-05-05
My C# code (which references Microsoft DAO 3.6 Object Library) contains objects called fromTable and toTable, both defined as DAO.TableDef objects.
I am trying to copy all the indexes of fromTable into toTable but when I try to compile the attached code snippet I get the following error message on the second foreach statement:

foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

I also get the following error message that carries the same meaning on the next line:

'object' does not contain a definition for 'Append'

How can I copy a DAO.Index object if its Fields member is defined as a plain object?
DAO.Index toIndex;
foreach (DAO.Index fromIndex in fromTable.Indexes)
{
    toIndex = toTable.CreateIndex(fromIndex.Name);
    foreach (DAO.Field field in fromIndex.Fields)
        toIndex.Fields.Append(toIndex.CreateField(field.Name, field.Type, field.Size));
    toTable.Indexes.Append(toIndex);
}

Open in new window

0
Comment
Question by:netformx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
Priest04 earned 250 total points
ID: 22820289
Try casting Fields to IndexFields


foreach (DAO.Index fromIndex in fromTable.Indexes)
{
    toIndex = toTable.CreateIndex(fromIndex.Name);
    foreach (DAO.Field field in ((DAO.IndexFields) fromIndex.Fields))
        ((DAO.IndexFields)toIndex.Fields).Append(toIndex.CreateField(field.Name, field.Type, field.Size));
    toTable.Indexes.Append(toIndex);
}

Open in new window

0
 

Author Closing Comment

by:netformx
ID: 31510356
Genius!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question