【问题标题】:Relinking database tables: Access, VBA重新链接数据库表:Access、VBA
【发布时间】:2017-07-21 19:38:35
【问题描述】:

我有一个过程,可以根据它们是否是链接表来重新链接数据库中的所有表。目前,它设置为自动运行,因为它设置在调用该函数的 AutoExec 宏中。

代码有效,但只有在我关闭数据库并重新打开它的情况下。我知道这是因为需要执行此操作才能使新链接生效,但无论如何都可以解决这个问题?或者,如果做不到这一点,让 VBA 代码关闭数据库并重新打开它会更好吗?

提前感谢您的反馈

附:以下是代码,如果你好奇的话:

'*******************************************************************
'*  This module refreshes the links to any linked tables  *
'*******************************************************************


'Procedure to relink tables from the Common Access Database
Public Function RefreshTableLinks() As String

On Error GoTo ErrHandler
    Dim strEnvironment As String
    strEnvironment = GetEnvironment

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef

    Dim strCon As String
    Dim strBackEnd As String
    Dim strMsg As String

    Dim intErrorCount As Integer

    Set db = CurrentDb

    'Loop through the TableDefs Collection.
    For Each tdf In db.TableDefs

            'Verify the table is a linked table.
            If Left$(tdf.Connect, 10) = ";DATABASE=" Then

                'Get the existing Connection String.
                strCon = Nz(tdf.Connect, "")

                'Get the name of the back-end database using String Functions.
                strBackEnd = Right$(strCon, (Len(strCon) - (InStrRev(strCon, "\") - 1)))

                'Debug.Print strBackEnd

                'Verify we have a value for the back-end
                If Len(strBackEnd & "") > 0 Then

                    'Set a reference to the TableDef Object.
                    Set tdf = db.TableDefs(tdf.Name)

                    If strBackEnd = "\Common Shares_Data.mdb" Or strBackEnd = "\Adverse Events.mdb" Then
                        'Build the new Connection Property Value - below needs to be changed to a constant
                        tdf.Connect = ";DATABASE=" & strEnvironment & strBackEnd
                    Else
                        tdf.Connect = ";DATABASE=" & CurrentProject.Path & strBackEnd

                    End If

                    'Refresh the table links
                    tdf.RefreshLink

                End If

            End If

    Next tdf

ErrHandler:

 If Err.Number <> 0 Then

    'Create a message box with the error number and description
    MsgBox ("Error Number: " & Err.Number & vbCrLf & _
            "Error Description: " & Err.Description & vbCrLf)

End If

End Function

编辑

从 Gords cmets 开始,我添加了宏 AutoExec 方法来调用下面的代码。有人发现这有问题吗?

Action: RunCode
Function Name: RefreshTableLinks() 

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    这种情况下最常见的错误是忘记.RefreshLink TableDef,但您已经在这样做了。我刚刚测试了以下 VBA 代码,它在两个 Access 后端文件之间切换名为 [Products_linked] 的链接表:Products_EN.accdb(英语)和Products_FR.accdb(法语)。如果我运行 VBA 代码然后立即打开链接表,我会看到更改已经发生;我不必关闭并重新打开数据库。

    Function ToggleLinkTest()
    Dim cdb As DAO.Database, tbd As DAO.TableDef
    Set cdb = CurrentDb
    Set tbd = cdb.TableDefs("Products_linked")
    If tbd.Connect Like "*_EN*" Then
        tbd.Connect = Replace(tbd.Connect, "_EN", "_FR", 1, 1, vbBinaryCompare)
    Else
        tbd.Connect = Replace(tbd.Connect, "_FR", "_EN", 1, 1, vbBinaryCompare)
    End If
    tbd.RefreshLink
    Set tbd = Nothing
    Set cdb = Nothing
    End Function
    

    我什至测试了从 AutoExec 宏调用该代码,它似乎也按预期工作。

    您可以尝试的一件事是在日常工作结束时致电db.TableDefs.Refresh,看看是否有帮助。

    编辑

    这里的问题是数据库在其“应用程序选项”中指定了一个“显示表单”,并且该表单显然会在 AutoExec 宏运行之前自动打开。将重新链接代码的函数调用移动到该“启动表单”的 Form_Load 事件处理程序似乎是一个可能的解决方法。

    【讨论】:

    • 我试过你说的-刷新表格链接-并没有什么不同-我仍然需要关闭它并重新打开它。我不明白为什么。我想唯一的解决方案是重新打开数据库?
    • @Katana24 也许这与 AutoExec 宏中运行的代码有关。尝试进行需要实际更新链接的更改,然后打开数据库,如果新链接没有“获取”,则尝试再次运行仅您的 RefreshTableLinks 函数。如果这有效,那么您可以调查为什么他们第一次没有“接受”(从 AutoExec 运行时)。
    • 我认为你是对的 Gord - 它必须与它在 AutoExec 中的调用方式有关 - 因为当我将它打包到另一个宏中并运行它时它可以工作。当我从表单加载调用 if 时,它也可以工作 - 我已经编辑了答案以显示我在 AutoExec 宏中调用它的方式
    • Gord 我确定了我收到错误的原因 - 这与我的代码无关 - 即使在 autoexec 宏中也能正常工作。问题在于 AutoExec 没有被首先调用 - 表单被首先调用
    • @Katana24 啊哈!很高兴知道。是否会在“当前数据库”的“应用程序选项”中指定“显示表单”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多