2013-03-05 48 views
1

我正在爲使用MS Access 2003(沒有其他可用)的快照樣式數據庫構建臨時後端,這涉及到向臨時後端添加一些鏈接的tabledefs。代碼已經工作了大約3周,但截至今天下午早些時候,開始拋出3356(即機器Y上的用戶X已經以獨佔模式打開數據庫...)或3045(大致無法打開數據庫在exlcusive模式下),取決於我是否已經在Access中建立連接。當在Access中追加tabledef時出現錯誤3045或3356

的示數碼粗略(有點修整):

Private Sub AddTabledefToDb(dbC As DAO.Database, dbTarget As DAO.Database, strLinkedName As String) 
    Dim strPath As String, tdfLinked As DAO.TableDef 
    strPath = strGetPathFromConnect(tdfLinked.Connect) 

    Set tdfLinked = dbC.TableDefs(strLinkedName) 

    ' With the lines below, error thrown is 3356; without 3045 ' 
    Dim dbLinkedTableIn As DAO.Database 
    Set dbLinkedTableIn = Application.DBEngine.Workspaces(0).OpenDatabase(strPath, False, True, tdfLinked.Connect) 

    Dim tdfNew as DAO.TableDef 
    Set tdfNew = dbTarget.CreateTableDef(Attributes:=dbAttachedTable) 
    tdfNew.Name = tdfLinked.Name 
    tdfNew.SourceTableName = tdfLinked.SourceTableName 
    tdfNew.Connect = tdfLinked.Connect 
    dbTarget.TableDefs.Append tdfNew ' Throws 3045 without the lines above or 3356 with them ' 

    ' If needed... ' 
    dbLinkedTableIn.Close 
    Set dbLinkedTableIn = Nothing 

End Sub 

我有一個懷疑,這樣做的原因可能與顯示,如果我打開數據庫住房表我留言直接鏈接,即它只能在只讀模式下使用(我相當肯定以前不是這種情況)。然而,我不清楚爲什麼我的代碼需要的不僅僅是隻讀訪問,而且我也不知道爲什麼它試圖獲取它(特別是當我以只讀模式預先打開數據庫時)。

任何幫助將非常感激。

謝謝

+0

好吧,它可能是一些事情。假設某人實際上並沒有以獨佔模式打開其中一個數據庫(你可能已經檢查過),這聽起來像是一個權利問題。即使在您鏈接到的數據庫中,您也需要擁有創建/修改/刪除.ldb鎖定文件的權限,因此如果這是網絡共享,您可能需要從這裏啓動。 – 2013-03-05 18:47:22

+0

嗯......嗯,我知道我可以創建一個.idb,因爲它是在我明確打開數據庫時創建的,並且在關閉/沒有對象(僅查找文件夾)時被刪除。我隱約想知道這是否與2GB的限制有關Access雖然...我試圖連接的數據庫顯示爲1.87GB(不問),但考慮它可能是2萬億字節(給出1000和1024之間的區別等)。 今天早上再試一次,它似乎功能(觸摸木材)。真的很困惑,爲什麼它首先打破了。 – tobriand 2013-03-06 09:29:22

+0

@tobriand我認爲你對文件大小的懷疑是正確的,1.87GB的顯示應該等同於大約2GB的字節數限制。當你達到這些限制時,Access會觸發一個權限擴展,而不是一個更有意義的**空間不足**錯誤。數據庫最近是否被壓縮和修復?如果是這樣,您可能需要考慮將某些表分割爲另一個mdb文件。 – 2013-03-06 12:41:21

回答

1

認爲我遇到了答案:不要使用DAO,而是使用ADO。在下面看得非常粗略。目前,我還沒有將Mode屬性設置爲Read,但最初的測試表明它至少在沒有這樣做的情況下才起作用。

Dim cn As ADODB.Connection 
Dim tbl as ADOX.Table 
Dim cat as ADOX.Catalog 

Set cn = New ADODB.Connection 
cn.Provider = "Microsoft.Jet.OLEDB.4.0" 
cn.Open dbTarget.Name ' Path of the db the linked table needs to end up in' 

Set cat = New ADOX.Catalog 
cat.ActiveConnection = cn 

Set tbl = New ADOX.Table 
Set tbl.ParentCatalog = cat 

tbl.Name = tdfLinked.Name 
tbl.Properties("Jet OLEDB:Link Datasource") = strGetPathFromConnectString(tdfLinked.Connect) 
tbl.Properties("Jet OLEDB:Link Provider String") = "MS Access" ' If Password protected, details go here ' 
tbl.Properties("Jet OLEDB:Remote Table Name") = tdfLinked.SourceTableName 
tbl.Properties("Jet OLEDB:Create Link") = True 

cat.Tables.Append tbl 

cn.Close 
Set tbl = Nothing 
Set cat = Nothing 
Set cn = Nothing 

大致來說,它看起來像ADO是不快樂收購守則讀/寫訪問,以創建一個鏈接表,而DAO不是。

+0

在我接受我的回答之前等待幾天,因爲有可能再次出現問題! – tobriand 2013-03-11 12:32:40

相關問題