1

我正在使用 MS Access 2003(没有其他可用)为快照样式数据库构建临时后端,这涉及将一些链接的 tabledef 添加到临时后端。该代码已经工作了大约 3 周,但截至今天下午早些时候,开始抛出 3356(即机器 Y 上的用户 X 已经以独占模式打开了数据库......)或 3045(大致,无法打开数据库在独占模式下),这取决于我是否已经在 Access 中建立了连接。

错误代码大致(略微修剪):

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

我怀疑其原因可能与我打开包含我直接链接到的表的数据库时显示的消息有关,即它仅在只读模式下可用(我相当确定不是以前的情况)。但是,我不清楚为什么我的代码需要的不仅仅是只读访问权限,而且我无法弄清楚它为什么要尝试获取它(尤其是当我事先以只读模式显式打开数据库时)。

任何帮助将不胜感激。

谢谢

4

1 回答 1

1

想我已经找到了答案:不要使用 DAO,而是使用 ADO。大致见下文。目前,我还没有将该Mode属性设置为读取,但初步测试表明它至少可以在不这样做的情况下运行。

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 很乐意创建链接表而无需在 Code 中获取读/写访问权限,而 DAO 则不然。

于 2013-03-11T12:32:02.547 回答