?
Solved

Export linked table from MS Access to another Access file

Posted on 2014-11-11
4
Medium Priority
?
1,038 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
[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
  • 3
4 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

771 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