使用 VBA 从 access 2013 前端连接到 access 后端
Connecting to access backend from access 2013 frontend using VBA
我将向 5-6 个客户销售基于 Access 的产品。哪里有后端数据库文件(受密码保护)。访问 accdr
将数据保存在后端文件中的前端文件。后端文件的位置会将客户端更改为客户端,因此需要一个 VBA 代码将前端链接到后端。
我尝试了下面的代码
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0; " _
& Data Source= " & "C:\MyDB_be.accdb" & ";" _
& "Jet OLEDB:Database Password=123;"
但是,表没有重新连接。
我从 this Ques on Whosebug 得到了上面的代码。
然后我尝试了下面的代码
Const LnkDataBase = "C:\MyDB_be.accdb"
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
当文件未受密码保护时,此方法有效。我从 This Ques 获得的这段代码。但是没有指定密码的规定。
请帮帮我。
Either point out mistake in 1st code. OR How to specify password in
2nd Code OR New code altogether.
花了 4 小时寻找解决方案。新访问 VBA.
试一试:
Const LnkDataBase = "C:\MyDB_be.accdb"
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 = "Provider=Microsoft.ACE.OLEDB.12.0; " _
& "Data Source= " & LnkDataBase & ";" _
& "Jet OLEDB:Database Password=123;"
dbs.TableDefs(strTable).RefreshLink
End If
End If
End If
Next tdf
End Sub
再投入 4 小时来解决这个问题。终于找到解决方法了。
这是完美运行的代码。
Const LnkDataBase = "C:\MyDB_be.accdb"
Const DBPassword = "123"
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 = "MS Access;PWD=" & DBPassword & ";DATABASE=" & LnkDataBase
dbs.TableDefs(strTable).RefreshLink
End If
End If
End If
Next tdf
End Sub
我鼓励 VBA 专家添加他们的评论或修改此代码以添加错误调试。比如 - 如果 PC 未连接到网络,并且指定的路径位于网络上,则 Access 会挂起。此问题尚未解决。
我将向 5-6 个客户销售基于 Access 的产品。哪里有后端数据库文件(受密码保护)。访问 accdr
将数据保存在后端文件中的前端文件。后端文件的位置会将客户端更改为客户端,因此需要一个 VBA 代码将前端链接到后端。
我尝试了下面的代码
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0; " _
& Data Source= " & "C:\MyDB_be.accdb" & ";" _
& "Jet OLEDB:Database Password=123;"
但是,表没有重新连接。
我从 this Ques on Whosebug 得到了上面的代码。
然后我尝试了下面的代码
Const LnkDataBase = "C:\MyDB_be.accdb"
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
当文件未受密码保护时,此方法有效。我从 This Ques 获得的这段代码。但是没有指定密码的规定。
请帮帮我。
Either point out mistake in 1st code. OR How to specify password in 2nd Code OR New code altogether.
花了 4 小时寻找解决方案。新访问 VBA.
试一试:
Const LnkDataBase = "C:\MyDB_be.accdb"
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 = "Provider=Microsoft.ACE.OLEDB.12.0; " _
& "Data Source= " & LnkDataBase & ";" _
& "Jet OLEDB:Database Password=123;"
dbs.TableDefs(strTable).RefreshLink
End If
End If
End If
Next tdf
End Sub
再投入 4 小时来解决这个问题。终于找到解决方法了。
这是完美运行的代码。
Const LnkDataBase = "C:\MyDB_be.accdb"
Const DBPassword = "123"
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 = "MS Access;PWD=" & DBPassword & ";DATABASE=" & LnkDataBase
dbs.TableDefs(strTable).RefreshLink
End If
End If
End If
Next tdf
End Sub
我鼓励 VBA 专家添加他们的评论或修改此代码以添加错误调试。比如 - 如果 PC 未连接到网络,并且指定的路径位于网络上,则 Access 会挂起。此问题尚未解决。