Link to home
Start Free TrialLog in
Avatar of marku24
marku24Flag for United States of America

asked on

Linking Tables in Access through VBA

I would like to link tables between two databases using VBA code.  I have two databases, Target.accdb and SharedTables.accdb.  The following code is in my Target.accdb.  I have them linked manually now but when I change the location of the two databases the link to shared doesn't change.  I believe the following code will work but can't get the "td.connect" - string to work.  I want this to link all tables in the target.accdb to the same table names in the sharedtables.accdb that are always in the same directory.  

Function fncLinkTables()

Dim td As DAO.TableDef
Dim tdnames As String
Dim strFilePath As String
Dim strSharedFiles As String


strFilePath = CurrentProject.Path
strSharedFiles = "SharedTables.accdb"


For Each td In CurrentDb.TableDefs
  If Len(td.Connect) > 0 Then    
     td.Connect = ";DATABASE=" & ???????
     td.RefreshLink
 
      End If
Next

MsgBox "Tables Relinked"

End Function
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America 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