I can create an Access mdb and add a linked table to an Sql Server database via ODBC. If I change the Sql Server that the ODBC is connecting to with the ODBC control panel applet the mdb still connects to the original Sql Server until Access is restarted.
Is there a way to relink these linked server tables without restarting Access?
EDIT: I would like to do this in code
Best Answer
You can use the code below to refresh all ODBC tables in your Access project to a given DSN.
How to use it
Just copy the code in a new or existing VBA module and, where you want to refresh the links, call it with the proper DSN for the new ODBC connection:
Also, have a look at the Access help for the
TableDef.RefreshLink
method.Code version 1
Classic way of relinking but Access may keep connection information in memory if the tables have been used before
RefreshODBCLinks
is called.Code version 2
This will completely re-create the ODBC linked tables: the old ones will be renamed, then new tables using the given DSN will be created before deleting the old linked version.
Please make sure you test this and maybe add some code to better handle errors as necessary.
Note also that the parameter
dbAttachSavePWD
passed during creation of the ODBC table will save the ODBC password (if any) in Access. Just remove it if that's not what you need.One last thing: if you're still getting issues that require that you restart Access for the changes to be visible, then have a look at my code in Restarting and compacting the database programmatically on my site.
Note: Code Version 2 was inspired in part from this Access Web article.