2014-02-12 42 views

回答

3

我以前做這個的方法其實很簡單:

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 
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文件的安全性。

+0

+1爲有用的代碼和解釋(的TableDef派上了用場其他地方)。我的問題的解決方案竟然比我預想的要簡單得多。 – authentictech

相關問題