tags:

views:

15304

answers:

5

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

+2  A: 

What version of Access are you using? In 2000, you can go to Tools>Database Utilities>Linked Table Manager to change your settings.

Dinci Garrone
Thanks - you're right it works if I use the Linked Table Manager and check the box "Always prompt for new location" and then choose the ODBC again. I would like to relink the table in code though - I will edit my question to make this clear.
BTB
Why would you like to relink your table? What will be gained by doing that?
Brettski
The ODBC is changed to point at a different server/database - but the Access app is unaware of the change until its restarted.
BTB
+6  A: 

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:

RefreshODBCLinks "ODBC;DRIVER=SQL Server Native Client 10.0;" & _"
                 "SERVER=SQLSERVER;UID=Administrator;" & _
                 "Trusted_Connection=Yes;" & _
                 "APP=2007 Microsoft Office system;DATABASE=OrderSystem;"

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.

Public Sub RefreshODBCLinks(newConnectionString As String)
    Dim db As DAO.Database
    Dim tb As DAO.TableDef
    Set db = CurrentDb
    For Each tb In db.TableDefs
        If Left(tb.Connect, 4) = "ODBC" Then
            tb.Connect = newConnectionString
            tb.RefreshLink
            Debug.Print "Refreshed ODBC table " & tb.Name
        End If
    Next tb
    Set db = Nothing
End Sub

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.

Public Sub RefreshODBCLinks(newConnectionString As String)
    Dim db As DAO.Database
    Dim tb As DAO.TableDef
    Dim originalname As String
    Dim tempname As String
    Dim sourcename As String
    Dim i As Integer

    Set db = CurrentDb
    ' Get a list of all ODBC tables '
    Dim tables As New Collection
    For Each tb In db.TableDefs
        If (Left(tb.Connect, 4) = "ODBC") Then
            tables.Add Item:=tb.Name, key:=tb.Name
        End If
    Next tb

    ' Create new tables using the given DSN after moving the old ones '
    For i = tables.count To 1 Step -1
            originalname = tables(i)
            tempname = "~" & originalname & "~"
            sourcename = db.TableDefs(originalname).SourceTableName
            ' Create the replacement table '
            db.TableDefs(originalname).Name = tempname
            Set tb = db.CreateTableDef(originalname, dbAttachSavePWD, _
                                        sourcename, newConnectionString)
            db.TableDefs.Append tb
            db.TableDefs.Refresh
            ' delete the old table '
            DoCmd.DeleteObject acTable, tempname
            db.TableDefs.Refresh
            tables.Remove originalname
            Debug.Print "Refreshed ODBC table " & originalname
    Next i
    Set db = Nothing
End Sub

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.

Renaud Bompuis
It would be a very good idea to exclude system tables, that is tables that start "MSys"
Remou
OK, but why would they have a connection string starting with "ODBC"?They wouldn't match that in any case.
Renaud Bompuis
It appears to work for me but only if I change the connection string. If I change say the server in the ODBC then the Linked table still connects to the original server after I call RefreshODBCLinks. If I change the database name in the connection string then the linked server is refreshed.
BTB
OK, please try the second version or simply use my restart function if that's enough to force Access to refresh.
Renaud Bompuis
A: 

Could you advise how I would change the refresh links for a MYSQL database on a NON trusted connection using DSN MYAPPDATA and user being MYUSER pass being MYPASS ip source being 1.1.1.1 and driver being MySQL ODBC 5.1 Driver.

Many many thanks

Romolo

A: 

I have created a IMPORT from SQL Server to Access DB using ODBC data sources. I would need to get the Access DB refreshed periodically without any manual intervention. How can I achieve this?

Pradeep Narsimhula
This is a new question, rather than an answer to the present question. Why not post your question as a standalone question, instead? It wouldn't hurt to cite this discussion if it seems to you to be related to your issues.
David-W-Fenton
A: 

Renaud, that worked like a charm. Great when I'm having to relink to a development db server and don't get 80 prompts.

Michael