Using code to change the linked table location is not uncommon and like many dev tasks, it is just one of several ways to get the job done.
I like to work with a "local version" of the BE Data so that I can work without network, without affecting live data and when designing queries and reports, it's simply faster (how much faster depends on a lot of things, including a slow network).
Another reason to relink a table in code is when the app is portable. A frequent example is when the app could be anywhere and simply needs the BE to be in the same folder. In this instance, I use code in the startup form to verify the tables have a valid link. If the link is not valid, then it looks for the BE in a typical location (such as the current folder, or a sub folder of the current app folder called Data or similar). If it is still not found, it then prompts the user for the location using a file open dialog. If that doesn't work, it thanks the user for playing and gracefully closes.
I could come up with a half dozen more examples and various levels of complexity from simple relinking to managing multiple link to locations, to switching between ODBC, Access and other etc. etc. that I have in use.
Here's a sample of a really basic procedure. Make sure you have DAO referenced (in code window, View menu then References).
Code:
Sub Map(blLocal As Boolean)
Dim strFile As String
Dim tblDef As DAO.TableDef
Dim tblDefs As DAO.TableDefs
On Error GoTo MapError
DoCmd.Hourglass True
If blLocal Then strFile = Nz(Me.txtLocal, "") Else strFile = Nz(Me.txtNet, "")
If strFile = "" Then Exit Sub
strFile = ";DATABASE=" & strFile
Set tblDefs = CurrentDb.TableDefs
For Each tblDef In tblDefs
If tblDef.Connect <> "" Then ‘ an empty string means it’s a local table
Me.Label1.Caption = "Rerouting " & tblDef.Name & " to " & strFile & " ..."
Me.Repaint
tblDef.Connect = strFile
tblDef.RefreshLink
End If
Next
Me.Label1.Caption = "Done."
MapExit:
DoCmd.Hourglass False
Exit Sub
MapError:
Me.Label1.Caption = "Unexpected error!"
Me.Repaint
Resume MapExit
End Sub
Private Sub cmdNet_Click()
Call Map(False)
End Sub
Private Sub cmdLocal_Click()
Call Map(True)
End Sub
Two text boxes hold the "local" and "live" paths (which I set using code that calls on the built in file open dialog). Of course, they also default to the typical places so all I have to do is press the appropiate button.
Although I run this in Acc2010, is should work all the way back to Acc97.
Enjoy!