?
Solved

Automate the Excel files link in Access DB

Posted on 2014-10-03
4
Medium Priority
?
296 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 38

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 51

Accepted Solution

by:
Gustav Brock earned 1500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

800 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