Function RefreshLinks() As Boolean
Dim strMsg As String, collTbls As Collection
Dim i As Integer, sTbl As String, sDBPath As String
Dim dbCurr As Database, dbLink As Database
Dim tdfLocal As TableDef
Dim iTblNumber As Integer
Const cFILE_NOT_FOUND = vbObjectError + 1000
Const cNO_REMOTE_TABLE = vbObjectError + 2000
sDBPath = DLookup("[BackendPath]", "Admin")
On Local Error GoTo RefreshLinks_Err
'First get all linked tables in a collection
Set collTbls = GetLinkedTables
'first make sure we have a valid path to remote database
If Len(Dir(sDBPath)) = 0 Then Err.Raise cFILE_NOT_FOUND
Set dbLink = DBEngine(0).OpenDatabase(sDBPath)
'now link all of them
Set dbCurr = CurrentDb
iTblNumber = collTbls.Count
For i = iTblNumber To 1 Step -1
sTbl = ParseTable(collTbls(i))
If IsRemoteTable(dbLink, sTbl) Then
'everything's ok, reconnect
Set tdfLocal = dbCurr.TableDefs(sTbl)
With tdfLocal
.Connect = ";Database=" & sDBPath
.RefreshLink
collTbls.Remove (.Name)
End With
Else
Err.Raise cNO_REMOTE_TABLE
End If
Next
RefreshLinks = True
RefreshLinks_End:
Set collTbls = Nothing
Set tdfLocal = Nothing
Set dbLink = Nothing
Set dbCurr = Nothing
Exit Function
RefreshLinks_Err:
RefreshLinks = False
Select Case Err
Case 3059:
Case cFILE_NOT_FOUND:
MsgBox "The Path to file: " & sDBPath & ", couldn't link tables.", _
vbCritical + vbOKOnly, _
"Error in refreshing links."
Resume RefreshLinks_End
Case cNO_REMOTE_TABLE:
MsgBox "Table '" & sTbl & "' was not found in the database" & _
vbCrLf & dbLink.Name & ". Couldn't refresh links", _
vbCritical + vbOKOnly, _
"Error in refreshing links."
Resume RefreshLinks_End
Case Else:
strMsg = "Error Information..." & vbCrLf & vbCrLf
strMsg = strMsg & "Function: RefreshLinks" & vbCrLf
strMsg = strMsg & "Description: " & Err.description & vbCrLf
strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf
MsgBox strMsg, vbOKOnly + vbCritical, "Error"
Resume RefreshLinks_End
End Select
End Function
Private Function IsRemoteTable(dbRemote As Database, sTbl As String) As Boolean
Dim tdf As TableDef
On Error Resume Next
Set tdf = dbRemote.TableDefs(sTbl)
IsRemoteTable = (Err = 0)
Set tdf = Nothing
End Function
Private Function GetLinkedTables() As Collection
'Returns all linked tables
Dim collTables As New Collection
Dim tdf As TableDef
CurrentDb.TableDefs.Refresh
For Each tdf In CurrentDb.TableDefs
With tdf
If Len(.Connect) > 0 Then collTables.Add Item:=.Name & .Connect, Key:=.Name
End With
Next
Set GetLinkedTables = collTables
Set collTables = Nothing
Set tdf = Nothing
End Function
Private Function ParsePath(sIn As String) As String
'Returns the path to the database by stripping the
'prefix "DATABASE=" from the connect prperty
If Left$(sIn, 4) <> "ODBC" Then
ParsePath = Right(sIn, Len(sIn) _
- (InStr(1, sIn, "DATABASE=") + 8))
Else
ParsePath = sIn
End If
End Function
Private Function ParseTable(sIn As String) As String
'strips everything after the ";" to get the table name
ParseTable = Left$(sIn, InStr(1, sIn, ";") - 1)
End Function