【问题标题】:Connecting to access backend from access 2013 frontend using VBA使用 VBA 从 access 2013 前端连接到 access 后端
【发布时间】:2015-08-05 23:44:56
【问题描述】:

我将向 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 Stackoverflow 得到了上面的代码。

然后我尝试了下面的代码

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 得到的。但没有规定密码。

请帮帮我。

要么指出第一个代码中的错误。或如何在中指定密码 第二个代码或新代码。

花了 4 小时寻找解决方案。访问 VBA 的新手。

经历了thisthis,但不明白如何实现。

【问题讨论】:

  • 我试过dbs.TableDefs(strTable).Connect = "Provider=Microsoft.ACE.OLEDB.12.0; " &amp; "DATABASE= " &amp; LnkDataBase &amp; ";Jet OLEDB:Database Password=123;" 但仍然无法让它工作。此代码给出错误。

标签: ms-access vba ms-access-2013


【解决方案1】:

试试这个:

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

【讨论】:

  • 用户将在 Access 2013 上运行前端,所以 Microsoft.ACE.OLEDB.12.0 可以工作吗?或者,如果这是合乎逻辑的,我应该使用 Microsoft.ACE.OLEDB.15.0。?
  • 12 可以,但 15 也应该可以。用 15 测试,如果成功,使用 15。
  • 如果文件的位置不可访问(由于 PC 未连接到网络)。上面的代码使 Access 崩溃。我们可以在上面的代码中添加一些超时吗?
  • 试过你的解决方案,它给出了错误Runtime error 3170Could Not find installable ISAM
【解决方案2】:

在这个问题上再投入 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 专家添加他们的 cmets 或修改此代码以添加错误调试。像 - 如果 PC 未连接到网络,并且指定的路径在网络上,则 Access 挂起。这个问题还有待修复。

【讨论】:

  • 错误代码是什么?您可以添加ON ERROR GOTO err_hndl 并在那里处理它。否则,您可以使用 If (Len(Dir("MappedDriveLetterHere:\"))) Then blnConnected=True 之类的方式测试网络连接
  • 没有显示错误。访问运行时间只是挂起。什么也没有发生,当按下关闭按钮时,Access 停止工作并关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多