Preserving linked tables for Access DBs in same folder when the folder changes

前端 未结 4 1547
陌清茗
陌清茗 2020-12-30 11:40

I\'ve got two Access databases that share linked tables. They are deployed together in a directory and accessed via code in a Word form.

How can I make sure that the

4条回答
  •  清酒与你
    2020-12-30 11:54

    Renaud's answer no longer works in Access 2010 with Excel or CSV files.

    I have made a few modifications:

    • Adapted to the current pattern for the connection string
    • Handled the database path differently for Excel files (includes filename) and CSV files (does not include filename)

    Here is the code:

    Public Function RefreshLinksToPath(strNewPath As String, _
    Optional OnlyForTablesMatching As String = "*") As Boolean
    
    Dim collTbls As New Collection
    Dim i As Integer
    Dim strDBPath As String
    Dim strTbl As String
    Dim strMsg As String
    Dim strDBName As String
    Dim strcon As String
    Dim dbCurr As DAO.Database
    Dim dbLink As DAO.Database
    Dim tdf As TableDef
    
    Set dbCurr = CurrentDb
    
    On Local Error GoTo fRefreshLinks_Err
    
    'First get all linked tables in a collection'
    dbCurr.TableDefs.Refresh
    For Each tdf In dbCurr.TableDefs
        With tdf
            If ((.Attributes And TableDefAttributeEnum.dbAttachedTable) = _
               TableDefAttributeEnum.dbAttachedTable) _
               And (.Name Like OnlyForTablesMatching) Then
                Debug.Print "Name: " & .Name
                Debug.Print "Connect: " & .Connect
                collTbls.Add Item:=.Name & ";" & .Connect, Key:=.Name
            End If
        End With
    Next
    Set tdf = Nothing
    
    ' Now link all of them'
    For i = collTbls.Count To 1 Step -1
        strConnRaw = collTbls(i)
        ' Get table name from the full connection string
        strTbl = Left$(strConnRaw, InStr(1, strConnRaw, ";") - 1)
        ' Get original database path
        strDBPath = Right(strConnRaw, Len(strConnRaw) - (InStr(1, strConnRaw, "DATABASE=") + 8))
        ' Get the name of the linked database
        strDBName = Right(strDBPath, Len(strDBPath) - InStrRev(strDBPath, "\"))
        ' Get remainder of connection string
        strConn = Mid(strConnRaw, InStr(1, strConnRaw, ";") + 1, InStr(1, strConnRaw, "DATABASE=") _
                    - InStr(1, strConnRaw, ";") - 1)
    
        ' Reconstruct the full database path with the given path
        ' CSV-Files are not linked with their name!
        If Left(strConn, 4) = "Text" Then
            strDBPath = strNewPath
        Else
            strDBPath = strNewPath & "\" & strDBName
        End If
    
        ' Reconnect '
        Set tdf = dbCurr.TableDefs(strTbl)
        With tdf
            .Connect = strConn & "Database=" & strDBPath
            .RefreshLink
            collTbls.Remove (.Name)
        End With
    Next
    RefreshLinksToPath = True
    
    fRefreshLinks_End:
        Set collTbls = Nothing
        Set tdf = Nothing
        Set dbLink = Nothing
        Set dbCurr = Nothing
        Exit Function
    
    fRefreshLinks_Err:
        RefreshLinksToPath = False
        Select Case Err
            Case 3059:
    
            Case Else:
                strMsg = "Error Information..." & vbCrLf & vbCrLf
                strMsg = strMsg & "Function: fRefreshLinks" & vbCrLf
                strMsg = strMsg & "Description: " & Err.Description & vbCrLf
                strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf
                MsgBox strMsg
                Resume fRefreshLinks_End
        End Select
    End Function
    

提交回复
热议问题