由于我的许多项目都涉及使用odbc链接的后端数据源,所以我发现这段代码非常有用。 我通常会触发它以像AutoExec宏一样在系统启动时运行。 它可以避免由于链接断开而造成的任何麻烦,并且如果DSN名称或数据库服务器发生更改,则唯一需要进行的编辑就是连接字符串。
Function relinkTables()
Dim tdf As DAO.TableDef
For Each tdf In CurrentDb.TableDefs
' check if table is a linked table
If Len(tdf.Connect) > 0 Then
tdf.Connect = "odbc connection string to the DSN or database"
tdf.RefreshLink
End If
Next
End Function
注意:如果除通过odbc连接的链接表之外,还有其他链接表,则必须在代码中允许它们。
像往常一样,欢迎您提出所有建议,批评和增强意见:)
玛丽
翻译自: https://bytes.com/topic/access/insights/876985-relinking-odbc-tables-using-vba