Solved

Export linked table from MS Access to another Access file

Posted on 2014-11-11
4
895 Views
Last Modified: 2014-11-12
I'd like to export a SQL Server linked table (view) to another MS Access file.  The new table should not be a linked table, but native in Access.

The basic process would be:
1- drop Access file if exists
2- create Access file
3- Export table/data from current Access file to new Access file
4- import data from attached table into native table
5- repeat for other tables

Here is what I have so far, but the result is an attached table

 Dim i As Integer
    
   
   Dim sDir As String
   Dim sFilename As String
   Dim sDirFile As String
   
   Dim sTables() As String
   Dim sExpTable() As String
   
   
   Dim ws As Workspace
   Dim db As Database
   
   
    ReDim sTables(0)
    ReDim sExpTable(0)
    
    ReDim Preserve sTables(3)
    ReDim Preserve sExpTable(3)
    
   sTables(1) = "dbo_v_pub_exp_mall_usa"
   sTables(2) = "dbo_v_pub_exp_stores_all_usa"
   sTables(3) = "dbo_v_pub_exp_stores_anchor_usa"
   
   sExpTable(1) = "mall_usa"
   sExpTable(2) = "stores_all_usa"
   sExpTable(3) = "stores_anchor_usa"
   
   'Get default Workspace
   Set ws = DBEngine.Workspaces(0)
   
   'Path and file name for new mdb file
   sDir = "C:\Projects\Direct\export-data-partners\exports\"
   sFilename = "NewDB.mdb"
   
   sDirFile = sDir & sFilename
   
   'Make sure there isn't already a file with the name of the new database
   If Dir(sDirFile) <> "" Then Kill sFilename
   
   'Create a new mdb file
   Set db = ws.CreateDatabase(sDirFile, dbLangGeneral)
   
    For i = 1 To UBound(sTables)
        'For lookup tables, export both table definition and data to new mdb file
        DoCmd.TransferDatabase acExport, "Microsoft Access", sDirFile, acTable, sTables(i), sExpTable(i) & "_imp", False
        
    Next i
   db.Close
   Set db = Nothing

Open in new window

0
Comment
Question by:dougfosterNYC
  • 3
4 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
< Here is what I have so far, but the result is an attached table >

change this part

For i = 1 To UBound(sTables)
        'For lookup tables, export both table definition and data to new mdb file
        DoCmd.TransferDatabase acExport, "Microsoft Access", sDirFile, acTable, sTables(i), sExpTable(i) & "_imp", False
        
    Next i

Open in new window


with
   Dim sql As strimg
       For i = 1 To UBound(sTables)

        sql = "SELECT " & sTables(i) & ".* INTO " & sExpTable(i) & "_imp" & " IN '" & sDirFile & "'" _
                & " FROM " & sTables(i)

        CurrentDb.Execute sql
    Next i

Open in new window

0
 

Author Comment

by:dougfosterNYC
Comment Utility
ahh, ok, i'll try this in the morning.. thanks.
0
 

Author Comment

by:dougfosterNYC
Comment Utility
Thanks Rey, this worked perfectly.
0
 

Author Closing Comment

by:dougfosterNYC
Comment Utility
Thanks for writing the exact code change.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

762 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

9 Experts available now in Live!

Get 1:1 Help Now