Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 18
Graphics 2 27
ACCESS 2010 - VBA - generic Table Open Button - Code 7 20
Access: Retrieving Current Month's Orders for Invoice 6 27
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

805 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