Solved

How to copy Access Table Structure using ADOX/ DAO

Posted on 2006-10-30
7
2,969 Views
Last Modified: 2013-12-25
Hi Gurus

I need to know how to copy the structure of a given table in db_SOURCE to db_DEST.  Structure needs to include essential aspects such as field names, field type, field properties and index fields.

Your help is most appreciated.

0
Comment
Question by:BrianBeck
[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
  • 4
  • 3
7 Comments
 

Author Comment

by:BrianBeck
ID: 17836888
It seems to me that a critical first step is to determine the specific attributes of each field in the source table.  I found the following resource which I'm sure will prove most useful:
http://www.thescripts.com/forum/thread189610.html
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17837612
Why cant u just issue a copy object?

Im assuming u are using VB and Access DB

I was thinking along the lines of this
Now if u copy an object, it should copy everything across for u

Public Sub CopyTable()

    Dim acc As Object
   
    Set acc = CreateObject("Access.Application")
    acc.OpenCurrentDatabase ("C:\mydb.mdb")
    acc.DoCmd.CopyObject , "db_DEST", acTable, "db_SOURCE"
    acc.CloseCurrentDatabase
    Set acc = Nothing
   
End Sub
0
 

Author Comment

by:BrianBeck
ID: 17837645
Hi rockiroads

Thanks, but after experience I deliberately want to bypass Access and just make use of either ADOX or DAO.

I figure that with the above method of fetching field types, all I have to do is create a new table with the respective field names, types etc.  I expect to get to that stage within the day, and will update you.  If you have any suggestions, I most welcome them.

0
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!

 
LVL 65

Expert Comment

by:rockiroads
ID: 17837761
ok then. I guess u had some bad experiences using objects. And I thought I gave a easy answer :)
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 17837929
ok, I have previously done some stuff in DAO before but that was table creation and listing
I took it one step further and merged the two


    Dim db As DAO.Database
    Dim tbl As DAO.TableDef
    Dim fld As DAO.Field
    Dim tblCrt As DAO.TableDef
    Dim idx As DAO.Index
    Dim idxCrt As DAO.Index
    Dim prp As DAO.Property
   

    Set db = DBEngine.Workspaces(0).openDatabase("c:\mydb")
    Set tbl = db.TableDefs("db_SOURCE")
    Set tblCrt = CurrentDb.CreateTableDef("db_DEST")
    For Each fld In tbl.Fields
        Debug.Print "Copying field", fld.name
        tblCrt.Fields.Append tblCrt.CreateField(fld.name, fld.Type, fld.Size)
    Next fld

    db.TableDefs.Append tblCrt
   
    'Now indexes
    For Each idx In tbl.Indexes
        Debug.Print "Copying Index", idx.name
        Set idxCrt = tblCrt.CreateIndex(idx.name)
        idxCrt.Primary = idx.Primary
        idxCrt.Fields = idx.Fields
        idxCrt.IgnoreNulls = idx.IgnoreNulls
        idxCrt.Required = idx.Required
        idxCrt.Unique = idx.Unique
        tblCrt.Indexes.Append idxCrt
    Next idx
0
 

Author Comment

by:BrianBeck
ID: 17840665
Hi rockiroads

Thanks, and timely.  I got my 'copy table structure' routine with handling field types, but hadnt done the indexes so your code is most handy.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17840794
Cool
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

752 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