Solved

Automate the Excel files link in Access DB

Posted on 2014-10-03
4
289 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
[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
  • 2
4 Comments
 
LVL 36

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 50

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

726 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