我正在使用MS Access 2003(没有其他可用的)为快照样式数据库构建临时后端,这涉及向临时后端添加一些链接的tabledef。代码已经工作了大约3个星期,但截至今天下午早些时候,开始抛出3356(即机器Y上的用户X已经在独占模式下打开数据库......)或3045(粗略地,无法打开数据库)在exlcusive模式下),取决于我是否已在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
我怀疑这个原因可能与一条消息有关,如果我打开数据库,显示我正在直接链接的表,即它只能在只读模式下使用(我是相当肯定以前的情况并非如此)。但是,我不清楚为什么我的代码需要的内容不仅仅是只读访问权限,而且我无法解决为什么它会尝试获取它(特别是当我事先以只读模式显式打开数据库时)。
非常感谢任何帮助。
由于
答案 0 :(得分: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则不然。