Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to copy Access Table Structure using ADOX/ DAO

Posted on 2006-10-30
7
Medium Priority
?
3,008 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 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