Ste*_*e H 11 ms-access access-vba linked-tables
我有一个Access数据库,在第二个数据库中有一个链接表,与第一个数据库位于同一目录中.
我想将整个目录复制到一个新位置(用于测试),并且数据库一仍然链接到数据库二中的表,但是链接仍然是原始目录,而不是新位置.
我想做两件事之一:要么
以数据库路径为相对的方式链接到数据库2中的表 - 指向数据库2的路径不是硬编码的.
要么
在Form_Load(或autoexec宏)中有一个例程来检查application.path并以编程方式相应地调整链接.
有一个启动表单可以让你浏览你想要的后端和一个应该链接的表的表是很有用的.你可以遍历表集合,但我认为列表更安全.在那之后,只需要一些代码,这里是一个片段:
''Connection string with database password
strConnect = "MS Access;PWD=pw;DATABASE=" & Me.txtNewDataDirectory
Set rs = CurrentDb.OpenRecordset("Select TableName From LinkTables " _
& "WHERE TableType = 'LINK'")
Do While Not rs.EOF
''Check if the table is already linked, if it is, update the connection
''otherwise, link the table.
If IsNull(DLookup("[Name]", "MSysObjects", "[Name]='" & rs!TableName & "'")) Then
Set tdf = db.CreateTableDef(rs!TableName, dbAttachSavePWD, _
rs!TableName, strConnect)
db.TableDefs.Append tdf
Else
db.TableDefs(rs!TableName).Connect = strConnect
End If
db.TableDefs(rs!TableName).RefreshLink
rs.MoveNext
Loop
Run Code Online (Sandbox Code Playgroud)
小智 5
谢谢,
我使用它成功,但没有使用它与记录集.
Const LnkDataBase = "C:\NorthWind.mdb"
Sub relinktables()
'Routine to relink the tables automatically. Change the constant LnkDataBase to the desired one and run the sub
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim strTable As String
Set dbs = CurrentDb()
For Each tdf In dbs.TableDefs
If Len(tdf.Connect) > 1 Then 'Only relink linked tables
If tdf.Connect <> ";DATABASE=" & LnkDataBase Then 'only relink tables if the are not linked right
If Left(tdf.Connect, 4) <> "ODBC" Then 'Don't want to relink any ODBC tables
strTable = tdf.Name
dbs.TableDefs(strTable).Connect = ";DATABASE=" & LnkDataBase
dbs.TableDefs(strTable).RefreshLink
End If
End if
End If
Next tdf
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51126 次 |
| 最近记录: |