问题描述
编辑:这个问题的答案可以在接受的答案的评论中找到。
The answer to this question can be found within the comments of the accepted answer.
我试图从我的excel文件。我现在有这样的代码:
I am attempting to open an Access database from a button click within my excel file. I currently have this code:
Private Sub bttnToAccess_Click()
Dim db As Access.Application
Set db = New Access.Application
db.Application.Visible = True
db.OpenCurrentDatabase "C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb"
End Sub
这似乎很简单,然后Access关闭几乎立即下降。如果重要,Access文件有一个AutoExec宏,通过打开的几个测试本身运行。
This seems to work briefly and then Access shuts down almost immediately. If it matters, the Access file has an AutoExec macro that runs through a few tests itself on open.
推荐答案
不要尝试然后打开Access应用程序;只需使用数据访问技术之一创建一个连接对象:
- OLE-DB或
- ODBC。
Don't try to open the Access application then; just create a connection object using one of the Data Access technologies: - OLE-DB or - ODBC.
GoogleODBC连接字符串或OLE-DB连接字符串可根据您的特定配置(和Access filetype)获取详细信息。
Google "ODBC Connection strings" or "OLE-DB Connection Strings" to get details depending on your particular configuration (and Access filetype).
可能ADODB是用于您的数据访问的最简单的当前库。
Probably ADODB is the easiest current library to use for your data access.
更新: / strong>
尝试从Access导入数据,然后使用数据 - >从访问向导。 Yu可以随时使用宏编录工具为您自动生成一些VBA代码,为您创建一些基础设施;在浏览VBA对象模型的新部分时,我经常使用这个。
Update:Try Importing the data from Access then using the Data -> From Access wizard. Yu can always use the Macro recoding facility to automatically generate some VBA code for you, that will create some infrastructure for you; I use this regularly when exploring new portions of the VBA object model.
更新 - 最终解决问题,从下面的评论
这可能是因为变量超出了范围;将 db
的声明移到函数之外,到模块级
Update - Final resolution of problem, from comments below
That may be because the variable goes out of scope; move the declaration of db
outside the function, to module level
这篇关于从Excel VBA打开访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!