Solved

Export linked table from MS Access to another Access file

Posted on 2014-11-11
4
975 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40436537
< 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
ID: 40436556
ahh, ok, i'll try this in the morning.. thanks.
0
 

Author Comment

by:dougfosterNYC
ID: 40438080
Thanks Rey, this worked perfectly.
0
 

Author Closing Comment

by:dougfosterNYC
ID: 40438084
Thanks for writing the exact code change.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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