Solved

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

Posted on 2008-10-27
2
257 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

856 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