Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-27
2
Medium Priority
?
305 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 1000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

972 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