适用于:Access 365、Access 2021、Access 2019、Access 2016
更新链接表的连接信息(仅适用于 Microsoft Access 工作区)。
语法
表达式 。RefreshLink
表达式 一个表示 TableDef 对象的变量。
说明
若要更改链接表的连接信息,请重置相应 TableDef 对象的 Connect 属性,然后使用 RefreshLink 方法更新信息。 使用 RefreshLink 方法不会更改链接表的属性和 Relation 对象。
要使此连接信息存在于与代表链接表的 TableDef 对象关联的所有集合中,必须对每个集合使用 Refresh 方法。
从 Access 365 版本 2403 开始,RefreshLink 方法保留现有表索引。 如果以前有一个链接表,该表在运行 RefreshLink 方法后会丢失主键,并且你添加了代码以在 RefreshLink 之后显式重新创建索引,则现在将导致错误 3283“主键已存在”。
示例
以下示例在链接表的连接由一个数据源更改为另一个数据源后,使用 RefreshLink 方法刷新该链接表中的数据。 若要使该过程运行,需要使用 RefreshLinkOutput 过程。
Sub RefreshLinkX()
Dim dbsCurrent As Database
Dim tdfLinked As TableDef
' Open a database to which a linked table can be
' appended.
Set dbsCurrent = OpenDatabase("DB1.mdb")
' Create a linked table that points to a Microsoft
' SQL Server database.
Set tdfLinked = _
dbsCurrent.CreateTableDef("AuthorsTable")
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
tdfLinked.Connect = _
"ODBC;DATABASE=pubs;DSN=Publishers"
tdfLinked.SourceTableName = "authors"
dbsCurrent.TableDefs.Append tdfLinked
' Display contents of linked table.
Debug.Print _
"Data from linked table connected to first source:"
RefreshLinkOutput dbsCurrent
' Change connection information for linked table and
' refresh the connection in order to make the new data
' available.
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
tdfLinked.Connect = _
"ODBC;DATABASE=pubs;DSN=NewPublishers"
tdfLinked.RefreshLink
' Display contents of linked table.
Debug.Print _
"Data from linked table connected to second source:"
RefreshLinkOutput dbsCurrent
' Delete linked table because this is a demonstration.
dbsCurrent.TableDefs.Delete tdfLinked.Name
dbsCurrent.Close
End Sub
Sub RefreshLinkOutput(dbsTemp As Database)
Dim rstRemote As Recordset
Dim intCount As Integer
' Open linked table.
Set rstRemote = _
dbsTemp.OpenRecordset("AuthorsTable")
intCount = 0
' Enumerate Recordset object, but stop at 50 records.
With rstRemote
Do While Not .EOF And intCount < 50
Debug.Print , .Fields(0), .Fields(1)
intCount = intCount + 1
.MoveNext
Loop
If Not .EOF Then Debug.Print , "[more records]"
.Close
End With
End Sub