Solved

Export linked table from MS Access to another Access file

Posted on 2014-11-11
4
1,024 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

695 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