Link to home
Start Free TrialLog in
Avatar of Karl001
Karl001Flag for Canada

asked on

Automate the Excel files link in Access DB

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

Avatar of PatHartman
PatHartman
Flag of United States of America image

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?
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Karl001

ASKER

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

Avatar of Karl001

ASKER

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