Solved

Automate the Excel files link in Access DB

Posted on 2014-10-03
4
286 Views
Last Modified: 2014-10-05
Hi

I have an Access DB, linkChange.accdb, where I modify the table’s link of an other Access DB (client.accdb).

The below code works, for Access tables, but for the Excel file, the link is done in the current DB ( linkChange.accdb).
How can I automate the excel file link, where the VBA code is in linkChange.accdb, but the link need to be done in an other AccessBD (client.accdb)
Thanks,


Set dbs = OpenDatabase(“S:\interface\client.accdb”)
Set tdfs = dbs.TableDefs
For Each tdf In tdfs
            If tdf.SourceTableName <> "" Then 'If the table source is other than a base table
  
             ‘New link for access tables
               If Left(tdf.Name, 2) = "tb" Then
                    tdf.Connect = ";DATABASE“S:\Data\clientData.accdb” 'Set the new source
                    tdf.RefreshLink
              End If

              ‘New link for excel file
                If Left(tdf.Name, 2) = "xl" Then
                    DoCmd.DeleteObject acTable, tdf.Name
                    DoCmd.TransferSpreadsheet acLink, 10, tdf.Name, "S:\data\Product.xlsx", True
                End If
            End If
Next

Open in new window

0
Comment
Question by:Karl001
  • 2
4 Comments
 
LVL 35

Expert Comment

by:PatHartman
ID: 40360193
I don't think I understand the question.
Are you saying you want to link to a BE that links to a spreadsheet and see that spreadsheet?  You can't.  When you link to a database, all you see are local tables.  You don't see linked tables.

If you are asking why the above code doesn't work, it would help if you could tell us what does happen.
Are you getting an error?
Is the link pointing to the wrong spreadsheet?

Have you tried deleting the link before relinking?
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40360214
Your code needs to be similar for the Excel tables:

               ‘New link for access tables
               If Left(tdf.Name, 2) = "tb" Then
                    tdf.Connect = ";DATABASE“S:\Data\clientData.accdb” 'Set the new source
                    tdf.RefreshLink
              End If

              ‘New link for excel file
                If Left(tdf.Name, 2) = "xl" Then
                    tdf.Connect = <Look up connection string from a properly linked Excel file>
                    tdf.RefreshLink
                End If

/gustav
0
 

Author Comment

by:Karl001
ID: 40362377
For the link of the Excel file, I tried to use the same code I used for the access tables (see below) and I had a message error "invalid path".
I am 100% sure of the path.
Do I have to specified the sheet name and if yes, how?
Thanks,


Set dbs = OpenDatabase(“S:\interface\client.accdb”)
Set tdfs = dbs.TableDefs
For Each tdf In tdfs
            If tdf.SourceTableName <> "" Then 'If the table source is other than a base table
  
             ‘New link for access tables
               If Left(tdf.Name, 2) = "tb" Then
                    tdf.Connect = ";DATABASE=“S:\Data\clientData.accdb” 'Set the new source
                    tdf.RefreshLink
              End If

              ‘New link for excel file
                If Left(tdf.Name, 2) = "xl" Then
                    tdf.Connect = ";DATABASE="S:\data\Product.xlsxt_xl.xlsx"
                    tdf.RefreshLink
                End if


                    
                     tdf.Connect = "Excel12.0;HDR=YES;IMEX=2;DATABASE=S:\ABScolaire\data\DESJ\test\Desj_Produit_xl.xlsx;TABLE=Desj_Produit$"
                    tdf.RefreshLink                End If


            End If
Next

Open in new window

0
 

Author Comment

by:Karl001
ID: 40362395
I found the problem,

Excel file format need to be specified in the code

 If Left(tdf.Name, 2) = "xl" Then
     tdf.Connect = "Excel 12.0;HDR=YES;IMEX=2; _
                              DATABASE="S:\dataProduct.xlsxt_xl.xlsx"
     tdf.RefreshLink
End if
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

821 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