Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 409471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:47:38+00:00 2026-05-12T17:47:38+00:00

So I’d like to copy a linked table to a local one in code,

  • 0

So I’d like to copy a linked table to a local one in code, structure and data in MS Access 2003.

Code being : VBA or C#. Or anything else for that matter..

UPDATE : I want the copy structure and data behaviour from ms access to keep the Primary Keys. If you copy a linked table, you can choose to paste it as ‘structure and data (local table)’
It is that I want to achieve in code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:47:38+00:00Added an answer on May 12, 2026 at 5:47 pm

    My understanding is that DAO does not support the decimal data type, but ADOX does. Here’s an updated procedure that uses ADOX instead to copy the schema to a new table.

    One interesting item of note: The OLEDB provider for Jet sorts the columns alphabetically rather than by ordinal position as explained in this KB article. I wasn’t concerned about preserving the ordinal position, but you may be, in which case you can update this procedure to meet your needs.

    In order for the ADOX version of the code to work, you’ll need to set a reference to Microsoft ADO Ext. 2.x for DDL and Security (where x = version number; I used 2.8 to test this procedure). You’ll also need a reference to ADO as well.

    Public Sub CopySchemaAndData_ADOX(ByVal sourceTableName As String, ByVal destinationTableName As String)
    On Error GoTo Err_Handler
    
    Dim cn As ADODB.Connection
    Dim cat As ADOX.Catalog
    Dim sourceTable As ADOX.Table
    Dim destinationTable As ADOX.Table
    
    Set cn = CurrentProject.Connection
    Set cat = New ADOX.Catalog
    Set cat.ActiveConnection = cn
    
    Set destinationTable = New ADOX.Table
    destinationTable.Name = destinationTableName
    
    Set sourceTable = cat.Tables(sourceTableName)
    
    Dim col As ADOX.Column
    For Each col In sourceTable.Columns
       Dim newCol As ADOX.Column
       Set newCol = New ADOX.Column
    
       With newCol
          .Name = col.Name
          .Attributes = col.Attributes
          .DefinedSize = col.DefinedSize
          .NumericScale = col.NumericScale
          .Precision = col.Precision
          .Type = col.Type
       End With
    
       destinationTable.Columns.Append newCol
    Next col
    
    Dim key As ADOX.key
    Dim newKey As ADOX.key
    
    Dim KeyCol As ADOX.Column
    Dim newKeyCol As ADOX.Column
    For Each key In sourceTable.Keys
       Set newKey = New ADOX.key
       newKey.Name = key.Name
       For Each KeyCol In key.Columns
          Set newKeyCol = destinationTable.Columns(KeyCol.Name)
          newKey.Columns.Append (newKeyCol)
       Next KeyCol
    
       destinationTable.Keys.Append newKey
    Next key
    
    cat.Tables.Append destinationTable
    
    'Finally, copy data from source to destination table
    Dim sql As String
    sql = "INSERT INTO " & destinationTableName & " SELECT * FROM " & sourceTableName
    CurrentDb.Execute sql
    
    Err_Handler:
       Set cat = Nothing
       Set key = Nothing
       Set col = Nothing
       Set sourceTable = Nothing
       Set destinationTable = Nothing
       Set cn = Nothing
    
       If Err.Number <> 0 Then
          MsgBox Err.Number & ": " & Err.Description, vbCritical, Err.Source
       End If
    End Sub
    

    Here’s the original DAO procedure

    Public Sub CopySchemaAndData_DAO(SourceTable As String, DestinationTable As String)
    On Error GoTo Err_Handler
    
    Dim tblSource As DAO.TableDef
    Dim fld As DAO.Field
    
    Dim db As DAO.Database
    Set db = CurrentDb
    
    Set tblSource = db.TableDefs(SourceTable)
    
    Dim tblDest As DAO.TableDef
    Set tblDest = db.CreateTableDef(DestinationTable)
    
    'Iterate over source table fields and add to new table
    For Each fld In tblSource.Fields
       Dim destField As DAO.Field
       Set destField = tblDest.CreateField(fld.Name, fld.Type, fld.Size)
       If fld.Type = 10 Then
          'text, allow zero length
          destField.AllowZeroLength = True
       End If
       tblDest.Fields.Append destField
    Next fld
    
    'Handle Indexes
    Dim idx As Index
    Dim iIndex As Integer
    For iIndex = 0 To tblSource.Indexes.Count - 1
       Set idx = tblSource.Indexes(iIndex)
       Dim newIndex As Index
       Set newIndex = tblDest.CreateIndex(idx.Name)
       With newIndex
          .Unique = idx.Unique
          .Primary = idx.Primary
          'Some Indexes are made up of more than one field
          Dim iIdxFldCount As Integer
          For iIdxFldCount = 0 To idx.Fields.Count - 1
             .Fields.Append .CreateField(idx.Fields(iIdxFldCount).Name)
          Next iIdxFldCount
       End With
    
       tblDest.Indexes.Append newIndex
    Next iIndex
    
    db.TableDefs.Append tblDest
    
    'Finally, copy data from source to destination table
    Dim sql As String
    sql = "INSERT INTO " & DestinationTable & " SELECT * FROM " & SourceTable
    db.Execute sql
    
    Err_Handler:
       Set fld = Nothing
       Set destField = Nothing
       Set tblDest = Nothing
       Set tblSource = Nothing
       Set db = Nothing
    
       If Err.Number <> 0 Then
          MsgBox Err.Number & ": " & Err.Description, vbCritical, Err.Source
       End If
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 237k
  • Answers 237k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use Case: Select Case When @PaidThisMonth < @OwedPast Then @PaidThisMonth… May 13, 2026 at 6:40 am
  • Editorial Team
    Editorial Team added an answer While "safe" -- either by correctly setting the encoding when… May 13, 2026 at 6:40 am
  • Editorial Team
    Editorial Team added an answer I think this might do it: //div [@class='Text'] [preceding-sibling::div [@class='Heading'… May 13, 2026 at 6:40 am

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
this is what i have right now Drawing an RSS feed into the php,
I have text I am displaying in SIlverlight that is coming from a CMS
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.