
refresh link in code or other method
Hi Song,
Here is a sample code to refresh linked table from someone within this
newsgroup. You can also search the original response in this newsgroup.
Hope this helps!
Simon Liao
**************************************
I got tired of using the Linked Table Manager to move between development
and production environments especially when the linked tables spanned
multiple ODBC and/or Access databases. So I wrote a little routine to help.
You need an Access table in your application called
DevelopmentProductionNames with 3 fields (DevelopmentName text 255,
ProductionName text 255, and id autonumber primary key). Populate with your
own development and production connection names. You can walk through the
routine before populating and see the current names -- just use a
"debug.print db.tabledefs(i).connect" to see them. Once the table is
populated, run the subroutine, it will prompt you for direction in which
you want to relink. You can easily toggle either direction
afterwards.
Sub ChangeLinkedTableConnection()
On Error GoTo ErrorHandler
Dim db As DAO.Database, i As Integer, DevToProd As Integer, CurrentName
As String
Dim NewName As String, rst As DAO.Recordset, Criteria As String,
FieldID As Integer
Set db = CurrentDb
' SET DevToProd to vbYes if you want to relink tables from development to
production otherwise
' SET DevToProd to vbNo if you want to relink tables from production to
development
DevToProd = MsgBox("Do you want to relink from development to
production?", vbYesNo + vbDefaultButton2)
If DevToProd = vbYes Then
CurrentName = "DevelopmentName"
FieldID = 1
Else
CurrentName = "ProductionName"
FieldID = 0
End If
Set rst = db.OpenRecordset("DevelopmentProductionNames",dbOpenSnapshot)
For i = 0 To db.TableDefs.Count - 1
Criteria = CurrentName & " = '" & db.TableDefs(i).Connect & "'"
rst.FindFirst Criteria
If rst.NoMatch = True Then
Debug.Print "Cannot relink "; db.TableDefs(i).Name
Else
db.TableDefs(i).Connect = rst.Fields(FieldID).Value
db.TableDefs(i).RefreshLink
End If
Next
Exit Sub
ErrorHandler:
If Err = 3192 Then Resume Next 'This skips tables which are not linked
anywhere else
End Sub