vba 尝试使用 DAO 从外部 Access (Outlook/Excel) 连接到有效数据库会生成 3343 无法识别的数据库格式错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15448338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attempt to connect to a valid database from outside Access (Outlook/Excel) using DAO generates a 3343 unrecognized database format error
提问by user2176765
Thanks for your site. Wonderful information.
感谢您的网站。精彩的信息。
In a nutshell, I'm trying to execute the following code from Outlook (2007), although it fails in Excel as well. Works great INSIDE Access!
简而言之,我试图从 Outlook (2007) 执行以下代码,尽管它在 Excel 中也失败了。内部访问效果很好!
Sub Test
Dim db As DAO.Database
Dim rs As DAO.Recordset
Const dbPath As String = "C:\Users\e574651.GLOBAL\Documents\Northwind 2007.accdb"
On Error Resume Next
Set db = DAO.OpenDatabase(dbPath)
'Set rs = db.OpenRecordset("customers")
Debug.Print Err.Number, Err.Description
End Sub
3343 Unrecognized database format 'C:\Users\e574651.GLOBAL\Documents\Northwind 2007.accdb'.
3343 无法识别的数据库格式“C:\Users\e574651.GLOBAL\Documents\Northwind 2007.accdb”。
I can access (no pun intended) this database all day long using ADO, and I suspect the problem lies with the following ADO statement:
我可以使用 ADO 整天访问(没有双关语)这个数据库,我怀疑问题在于以下 ADO 语句:
ADOConn.Provider = "Microsoft.ACE.OLEDB.12.0"
ADOConn.Provider = "Microsoft.ACE.OLEDB.12.0"
How do I provide this functionality using DAO?
如何使用 DAO 提供此功能?
I have included a reference to the DAO 3.6 library in my VBA preferences. I've included the other Microsoft 12.0 library references, so I've either clobbered something or omitted something.
我在 VBA 首选项中包含了对 DAO 3.6 库的引用。我已经包含了其他 Microsoft 12.0 库引用,所以我要么破坏了某些东西,要么忽略了某些东西。
Any assistance will be greatly appreciated.
任何帮助将不胜感激。
Thanks!
谢谢!
回答by Fionnuala
The most recent DAO libraries are in the format :
最新的 DAO 库采用以下格式:
Microsoft Office x.x Access Database Engine Object Library
So get rid of the 3.6 reference and use a more recent library. Then, an example:
所以摆脱 3.6 参考并使用更新的库。然后,举个例子:
Sub XLAccess()
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim sDb As String
Dim sSQL As String
Dim qdf As QueryDef
sDb = "Z:\Docs\Test.accdb"
Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase(sDb)
''A stored query would be better
sSQL = "Parameters p1 Text, p2 Datetime; " _
& "INSERT INTO Table1 (AText,ADate) Values ([p1],[p2])"
Set qdf = db.CreateQueryDef("", sSQL)
qdf.Parameters!p1 = "ABC"
qdf.Parameters!p2 = #1/17/2013#
qdf.Execute dbFailOnError
Debug.Print qdf.RecordsAffected
End Sub
回答by ENDEESA
Use a more recent version or the latest access databaseon your references.
在您的参考文献中使用更新的版本或最新的访问数据库。
For example: Inside your Visual Basics Window = Go to Tools > References >Microsoft Office 14.0 Access Database Engine Object Library.
例如:在您的 Visual Basics 窗口中 =转到 Tools > References > Microsoft Office 14.0 Access Database Engine Object Library。
Then use the following to open your database:
然后使用以下命令打开您的数据库:
Dim database_data As DAO.Database
Dim rsData As DAO.Recordset
Dim field_index As Integer
Set database_data = DAO.OpenDatabase("demo1.accdb")
At times, it might be necessary too type out the full path to your database file, e.g. "C:\User\Documents\projects\demo1.accdb"
有时,可能还需要输入数据库文件的完整路径,例如“C:\User\Documents\projects\demo1.accdb”