用于连接到加密/受密码保护的数据库的代码

时间:2014-02-12 17:36:30

标签: ms-access encryption ms-access-2007 access-vba

启动时,我的前端front.accdr数据库链接到后端back.accde使用:

DoCmd.TransferDatabase acLink, "Microsoft Access", "back.accde", acTable, "aTable", "aTable"

后端确实需要加密,因此我需要使用密码连接到加密的数据库。我该怎么做?

2 个答案:

答案 0 :(得分:4)

如果找不到使用TransferDatabase包含数据库密码的方法,则可以将表链接创建为DAO.TableDefs集合的新成员。

我确认此代码适用于Access 2007 ACCDR文件。

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strConnect As String
Dim strDbFile As String
Dim strLinkName As String
Dim strPassword As String
Dim strSourceTableName As String

strDbFile = "C:\share\Access\PasswordEquals_foo.accdb"
strPassword = "foo"
strSourceTableName = "Contacts"
strLinkName = "link_to_contacts"

strConnect = "MS Access;PWD=" & strPassword & _
    ";DATABASE=" & strDbFile
Debug.Print strConnect
Set db = CurrentDb
Set tdf = db.CreateTableDef
tdf.Connect = strConnect
tdf.SourceTableName = strSourceTableName
tdf.Name = strLinkName
db.TableDefs.Append tdf

请注意,即使使用ACCDR,任何能够阅读链接的TableDef.Connect属性的人都能够看到存储的数据库密码。例如,以下代码在立即窗口中显示" MS Access; PWD = foo; DATABASE = C:\ share \ Access \ PasswordEquals_foo.accdb"

Dim dbRemote As DAO.Database
Dim objWorkspace As Workspace
Set objWorkspace = CreateWorkspace("", "admin", "", dbUseJet)
Set dbRemote = objWorkspace.OpenDatabase("C:\share\Access\Database2.accdr")
Debug.Print dbRemote.TableDefs("link_to_contacts").Connect
dbRemote.Close
objWorkspace.Close

因此该链接会破坏加密的db文件的安全性。

答案 1 :(得分:4)

我用来做这个的方法实际上非常简单:

Set db = CurrentDb
Set dblink = DBEngine.OpenDatabase(strDbFile, False, False, ";PWD=" & strP)

For Each strTable In strLinkedTablesArray
     DoCmd.TransferDatabase acLink, "Microsoft Access", dblink.name, acTable, _
         strTable, strTable
Next