vba 使用 Office 2007 在 Visual Basic 上创建“Access.Application”对象时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26040190/
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
Error creating " Access.Application" Object on Visual Basic with Office 2007
提问by EBalla
I have created a application that takes an Excel file and inserts it into my access database table. I used Microsoft Access 15.0 Object Library in my computer . But when I executed the application it in an another computer with Access 2007 it stops immediately. This is the code:
我创建了一个应用程序,它接受一个 Excel 文件并将其插入到我的访问数据库表中。我在我的计算机中使用了 Microsoft Access 15.0 对象库。但是,当我在另一台装有 Access 2007 的计算机上执行该应用程序时,它会立即停止。这是代码:
Private Sub xlsTomdb()
On Error GoTo err_handler
Dim oAccess As Access.Application
Set oAccess = CreateObject("Access.Application")
oAccess.Visible = False
oAccess.OpenCurrentDatabase App.Path + "\archivi.mdb", True
oAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "EXCEL", App.Path + "\Export\Final.xls", True
oAccess.Quit
err_handler:
MsgBox "The code failed at line " & Erl, vbCritical
End Sub
The msgBox shows "The code failed as line 0". When I removed the Error Handler the program continues the execution normally. While running the program on another PC with Office 2007 it terminates at this function with runtime error [-2147467259 (80004005) microsoft odbc microsoft access driver type mismatch in expression]
msgBox 显示“代码作为第 0 行失败”。当我删除错误处理程序时,程序会继续正常执行。在另一台装有 Office 2007 的 PC 上运行该程序时,它会在此函数处终止并出现运行时错误 [-2147467259 (80004005) microsoft odbc microsoft access driver type mismatch in expression]
This function is executed on button click event. Is there any reason not to open access 2007, because I used Access 2013 while building it?
该函数在按钮点击事件上执行。是否有任何理由不开放 Access 2007,因为我在构建它时使用了 Access 2013?
Can anyone help?
任何人都可以帮忙吗?
采纳答案by Krish
EBalla:
埃巴拉:
"Microsoft Access 15.0 Object" or DAO 15 library belongs to office 2013. Earlier versions will not know about this library. if your target machines are using office 2007 you need to add the reference "Microsoft Access 12.0 Object library" into your project. You are experiencing compatibility issue rather than code issue.
“Microsoft Access 15.0 对象”或 DAO 15 库属于 office 2013。早期版本不知道此库。如果您的目标计算机使用的是 Office 2007,则需要将引用“Microsoft Access 12.0 对象库”添加到您的项目中。您遇到的是兼容性问题而不是代码问题。
also check this out: http://allenbrowne.com/ser-38.html
回答by Deco
Just substitute the follow sentence... Its work fine with any interop referenced in Project...
只需替换以下句子......它可以与项目中引用的任何互操作正常工作......
from:
从:
Dim oAccess As Access.Application
Set oAccess = CreateObject("Access.Application")
to:
到:
Dim oAccess As object
Set oAccess = CreateObject("Access.Application")