Question : VBA to amend table links to a new location

Hi All,

I am attempting to use code to amend the links of several tables in a database.

The Database has a number of local tables and a few linked ones. Users download their own copy of the system and when they run it for the first time they get a startup screen on first use that asks them to go through a setup process.

First in line is re-linking the few linked tables.  The form has a default location but a user can map the network drive as whatever they want and may need to connect to a different drive depending on their location.

So on the form is a field with the default location, but a button to browse for an return a database in a differing location. Once done the following simple code runs.
   
1:
2:
3:
4:
Sub ChangeLink()    
    CurrentDb.TableDefs("tbl_SystemLog").Connect = ";DATABASE=" & Me.txtBackend
    CurrentDb.TableDefs("tbl_SystemLog").RefreshLink
End Sub


Where Me.txtBackEnd is the drive map i.e "C:\My Documents\db.mdb"

This doesnt seem to work.  If I debug the tables connection before and after this the drive location is the same despite the fact it should have changed.  

As another test, if I choose another completely random database that doesn't contain the tables we're looking for I get no errors or anything....

Can anyone see what might be up with this?

Cheers
DeZZar

Answer : VBA to amend table links to a new location

> At the core of both link functions is exactly the same proceedure as I've posted above ..

No, because you call CurrentDb multiple times where you should call it once only.
Here is how for attaching tables in a backend file located in the same folder as the frontend.
You can easily modify it using the path for the backend as a parameter.

/gustav
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
  Dim dbs As DAO.Database
  Dim tdf As DAO.TableDef
  
  Dim strSourceTableName  As String
  Dim strCurrentPath      As String
  Dim strAttachPath       As String
  Dim strFrontendPath     As String
  Dim strBackendPath      As String
  Dim strConnect          As String
  
  On Error GoTo Err_GentilknytData
  
  Set dbs = CurrentDb
  
  strFrontendPath = dbs.Name
  strCurrentPath = Mid(strFrontendPath, 1, InStrRev(strFrontendPath, "\") - 1)

  For Each tdf In dbs.TableDefs
    strSourceTableName = tdf.SourceTableName
    If Len(strSourceTableName) > 0 Then
      strBackendPath = Split(tdf.Connect, "=")(1)
      strAttachPath = Mid(strBackendPath, 1, InStrRev(strBackendPath, "\") - 1)
      If StrComp(strCurrentPath, strAttachPath, vbTextCompare) <> 0 Then
        strConnect = ";DATABASE=" & strCurrentPath & "\AktivitetData.mdb"
        tdf.Connect = strConnect
        tdf.RefreshLink
      End If
    End If
  Next

  Set tdf = Nothing
  Set dbs = Nothing
  
Random Solutions  
 
programming4us programming4us