Solved

How to copy Access Table Structure using ADOX/ DAO

Posted on 2006-10-30
7
2,936 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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2003, my txt file export is running backwards 4 52
Run code from text file in vb 1 64
vb6 connector to mongodb 2 83
vbModal 12 51
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…

773 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