刷新表格链接

Posted

技术标签:

【中文标题】刷新表格链接【英文标题】:Refresh table link 【发布时间】:2012-11-01 13:06:45 【问题描述】:

我在浏览网页时发现了以下函数,它允许我在执行时将表动态链接到我的 Access 数据库:

Function createAttached(strTable As String, strPath As String, strBaseTable As String) As Boolean

'************************************************************************************
'* Create an attached table in the current database from a table in a different MDB file.
'* In:                                                                              *
'*   strTable - name of linked table to create                                      *
'*   strPath - path and name of MDB file containing the table                       *
'*   strBaseTable - name of table in strPath MDB                                    *
'* Out:                                                                             *
'*   Return value: True/False, indicating success                                   *
'* Modifies:                                                                        *
'*   Nothing, but adds a new table.                                                 *
'************************************************************************************

On Error GoTo CreateAttachedError

Dim tdf As TableDef
Dim strConnect As String
Dim fRetval As Boolean
Dim myDB As Database

    DoCmd.SetWarnings False
    Set myDB = CurrentDb
    Set tdf = myDB.CreateTableDef(strTable)

    With tdf
        .Connect = ";DATABASE=" & strPath
        .SourceTableName = strBaseTable
    End With

    myDB.TableDefs.Append tdf

    fRetval = True

    DoCmd.SetWarnings True

CreateAttachedExit:
    createAttached = fRetval
    Exit Function

CreateAttachedError:
    If Err = 3110 Then
        Resume CreateAttachedExit
    Else
        If Err = 3011 Then
            Resume Next
        End If
    End If

End Function

这个脚本可以工作,但是,如果表已经链接,它什么也不做(但仍然会触发错误事件)。如果链接表存在,我希望使用相同的脚本删除链接表,或者至少刷新该链接以使路径正确。我不知道该怎么做,可能很简单,但我不知道从哪里开始。

谢谢。

【问题讨论】:

见***.com/questions/12606326/… 你又来了?呵呵。哎呀,我不知道为什么我没有早点找到那个帖子。 【参考方案1】:

这是我使用的。它还在尝试刷新链接之前测试表是否为链接表。 此代码假定您要链接的数据库与您要链接的数据库位于同一文件夹中。如果没有,请删除“Application.CurrentProject.Path”并添加适当的路径。

Public Sub RelinkTables()
    Dim dbs As Database
    Dim Tdf As TableDef
    Dim Tdfs As TableDefs
    Set dbs = CurrentDb
    Set Tdfs = dbs.TableDefs
    For Each Tdf In Tdfs
        If Tdf.SourceTableName <> "" Then 'If the table source is other than a base table
            Tdf.Connect = ";DATABASE=" & Application.CurrentProject.Path & "\filename.accdb" 'Set the new source
            Tdf.RefreshLink 'Refresh the link
        End If
    Next 'Goto next table
End Sub

【讨论】:

我倾向于检查数据库名称的连接,而不是源表。从多个来源链接的情况并不少见。 如果我从多个来源链接,我通常有一个本地表,其中包含所有链接的表名称和它们链接到的数据库。 经过测试并使用 .RefreshLink。 Remou 发布的解决方案不起作用,但是 TabledDefs 不存在并且 .Refresh 不是 tdf 的属性。

以上是关于刷新表格链接的主要内容,如果未能解决你的问题,请参考以下文章

Power Query加载链接并刷新的疑惑

在PHP中怎么实现新增数据,刷新表格,而不刷新整个页面。

单击后但在重定向之前刷新/重置链接/WPushButton 的 WResource

jqgrid有没有增删改后不刷新表格,而由自己手动刷新的方法?

jQuery 插件 (DataTables) 仅在页面刷新时正确加载

swing 怎样实现表格内容刷新