Advertisement
TinmanQC

Refreshing linked tables in Access VBA

May 25th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '***************** Code Start ***************
  2. ' This code was originally written by Dev Ashish.
  3. ' It is not to be altered or distributed,
  4. ' except as part of an application.
  5. ' You are free to use it in any application,
  6. ' provided the copyright notice is left unchanged.
  7. '
  8. ' Code Courtesy of
  9. ' Dev Ashish
  10. '
  11.  
  12. Function fRefreshLinks() As Boolean
  13. Dim strMsg As String, collTbls As Collection
  14. Dim i As Integer, strDBPath As String, strTbl As String
  15. Dim dbCurr As DATABASE, dbLink As DATABASE
  16. Dim tdfLocal As TableDef
  17. Dim varRet As Variant
  18. Dim strNewPath As String
  19.  
  20. Const cERR_USERCANCEL = vbObjectError + 1000
  21. Const cERR_NOREMOTETABLE = vbObjectError + 2000
  22.  
  23.     On Local Error GoTo fRefreshLinks_Err
  24.  
  25.     If MsgBox("Are you sure you want to reconnect all Access tables?", _
  26.             vbQuestion + vbYesNo, "Please confirm...") = vbNo Then Err.Raise cERR_USERCANCEL
  27.  
  28.     'First get all linked tables in a collection
  29.    Set collTbls = fGetLinkedTables
  30.  
  31.     'now link all of them
  32.    Set dbCurr = CurrentDb
  33.  
  34.     strMsg = "Do you wish to specify a different path for the Access Tables?"
  35.    
  36.     If MsgBox(strMsg, vbQuestion + vbYesNo, "Alternate data source...") = vbYes Then
  37.         strNewPath = fGetMDBName("Please select a new datasource")
  38.     Else
  39.         strNewPath = vbNullString
  40.     End If
  41.  
  42.     For i = collTbls.Count To 1 Step -1
  43.         strDBPath = fParsePath(collTbls(i))
  44.         strTbl = fParseTable(collTbls(i))
  45.         varRet = SysCmd(acSysCmdSetStatus, "Now linking '" & strTbl & "'....")
  46.         If Left$(strDBPath, 4) = "ODBC" Then
  47.             'ODBC Tables
  48.            'ODBC Tables handled separately
  49.           ' Set tdfLocal = dbCurr.TableDefs(strTbl)
  50.           ' With tdfLocal
  51.           '     .Connect = pcCONNECT
  52.           '     .RefreshLink
  53.           '     collTbls.Remove (strTbl)
  54.           ' End With
  55.        Else
  56.             If strNewPath <> vbNullString Then
  57.                 'Try this first
  58.                strDBPath = strNewPath
  59.             Else
  60.                 If Len(Dir(strDBPath)) = 0 Then
  61.                     'File Doesn't Exist, call GetOpenFileName
  62.                    strDBPath = fGetMDBName("'" & strDBPath & "' not found.")
  63.                     If strDBPath = vbNullString Then
  64.                         'user pressed cancel
  65.                        Err.Raise cERR_USERCANCEL
  66.                     End If
  67.                 End If
  68.             End If
  69.  
  70.             'backend database exists
  71.            'putting it here since we could have
  72.            'tables from multiple sources
  73.            Set dbLink = DBEngine(0).OpenDatabase(strDBPath)
  74.  
  75.             'check to see if the table is present in dbLink
  76.            strTbl = fParseTable(collTbls(i))
  77.             If fIsRemoteTable(dbLink, strTbl) Then
  78.                 'everything's ok, reconnect
  79.                Set tdfLocal = dbCurr.TableDefs(strTbl)
  80.                 With tdfLocal
  81.                     .Connect = ";Database=" & strDBPath
  82.                     .RefreshLink
  83.                     collTbls.Remove (.Name)
  84.                 End With
  85.             Else
  86.                 Err.Raise cERR_NOREMOTETABLE
  87.             End If
  88.         End If
  89.     Next
  90.     fRefreshLinks = True
  91.     varRet = SysCmd(acSysCmdClearStatus)
  92.     MsgBox "All Access tables were successfully reconnected.", _
  93.             vbInformation + vbOKOnly, _
  94.             "Success"
  95.  
  96. fRefreshLinks_End:
  97.     Set collTbls = Nothing
  98.     Set tdfLocal = Nothing
  99.     Set dbLink = Nothing
  100.     Set dbCurr = Nothing
  101.     Exit Function
  102. fRefreshLinks_Err:
  103.     fRefreshLinks = False
  104.     Select Case Err
  105.         Case 3059:
  106.  
  107.         Case cERR_USERCANCEL:
  108.             MsgBox "No Database was specified, couldn't link tables.", _
  109.                     vbCritical + vbOKOnly, _
  110.                     "Error in refreshing links."
  111.             Resume fRefreshLinks_End
  112.         Case cERR_NOREMOTETABLE:
  113.             MsgBox "Table '" & strTbl & "' was not found in the database" & _
  114.                     vbCrLf & dbLink.Name & ". Couldn't refresh links", _
  115.                     vbCritical + vbOKOnly, _
  116.                     "Error in refreshing links."
  117.             Resume fRefreshLinks_End
  118.         Case Else:
  119.             strMsg = "Error Information..." & vbCrLf & vbCrLf
  120.             strMsg = strMsg & "Function: fRefreshLinks" & vbCrLf
  121.             strMsg = strMsg & "Description: " & Err.Description & vbCrLf
  122.             strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf
  123.             MsgBox strMsg, vbOKOnly + vbCritical, "Error"
  124.             Resume fRefreshLinks_End
  125.     End Select
  126. End Function
  127.  
  128. Function fIsRemoteTable(dbRemote As DATABASE, strTbl As String) As Boolean
  129. Dim tdf As TableDef
  130.     On Error Resume Next
  131.     Set tdf = dbRemote.TableDefs(strTbl)
  132.     fIsRemoteTable = (Err = 0)
  133.     Set tdf = Nothing
  134. End Function
  135.  
  136. Function fGetMDBName(strIn As String) As String
  137. 'Calls GetOpenFileName dialog
  138. Dim strFilter As String
  139.  
  140.     strFilter = ahtAddFilterItem(strFilter, _
  141.                     "Access Database(*.mdb;*.mda;*.mde;*.mdw) ", _
  142.                     "*.mdb; *.mda; *.mde; *.mdw")
  143.     strFilter = ahtAddFilterItem(strFilter, _
  144.                     "All Files (*.*)", _
  145.                     "*.*")
  146.  
  147.     fGetMDBName = ahtCommonFileOpenSave(Filter:=strFilter, _
  148.                                 OpenFile:=True, _
  149.                                 DialogTitle:=strIn, _
  150.                                 Flags:=ahtOFN_HIDEREADONLY)
  151. End Function
  152.  
  153. Function fGetLinkedTables() As Collection
  154. 'Returns all linked tables
  155.    Dim collTables As New Collection
  156.     Dim tdf As TableDef, db As DATABASE
  157.     Set db = CurrentDb
  158.     db.TableDefs.Refresh
  159.     For Each tdf In db.TableDefs
  160.         With tdf
  161.             If Len(.Connect) > 0 Then
  162.                 If Left$(.Connect, 4) = "ODBC" Then
  163.                 '    collTables.Add Item:=.Name & ";" & .Connect, KEY:=.Name
  164.                'ODBC Reconnect handled separately
  165.                Else
  166.                     collTables.Add Item:=.Name & .Connect, Key:=.Name
  167.                 End If
  168.             End If
  169.         End With
  170.     Next
  171.     Set fGetLinkedTables = collTables
  172.     Set collTables = Nothing
  173.     Set tdf = Nothing
  174.     Set db = Nothing
  175. End Function
  176.  
  177. Function fParsePath(strIn As String) As String
  178.     If Left$(strIn, 4) <> "ODBC" Then
  179.         fParsePath = Right(strIn, Len(strIn) _
  180.                         - (InStr(1, strIn, "DATABASE=") + 8))
  181.     Else
  182.         fParsePath = strIn
  183.     End If
  184. End Function
  185.  
  186. Function fParseTable(strIn As String) As String
  187.     fParseTable = Left$(strIn, InStr(1, strIn, ";") - 1)
  188. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement