Solved

Automate the Excel files link in Access DB

Posted on 2014-10-03
4
279 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 34

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

744 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

12 Experts available now in Live!

Get 1:1 Help Now