Solved

How to copy Access Table Structure using ADOX/ DAO

Posted on 2006-10-30
7
2,919 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now