Solved

How to copy Access Table Structure using ADOX/ DAO

Posted on 2006-10-30
7
2,987 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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
Course of the Month8 days, 11 hours left to enroll

615 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