vba ADODB.Connection 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1759767/
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
ADODB.Connection undefined
提问by WGroleau
Reference Excel VBA to SQL Server without SSIS
在没有 SSIS 的情况下将 Excel VBA引用到 SQL Server
After I got the above working, I copied all the global variables/constants from the routine, which included
完成上述工作后,我从例程中复制了所有全局变量/常量,其中包括
Const CS As String = "Driver={SQL Server};" _ & "Server=****;" _ & "Database=****;" _ & "UID=****;" _ & "PWD=****" Dim DB_Conn As ADODB.Connection Dim Command As ADODB.Command Dim DB_Status As String进入另一个电子表格中的类似模块。我也复制到同一个模块
Sub Connect_To_Lockbox() If DB_Status <> "Open" Then Set DB_Conn = New Connection DB_Conn.ConnectionString = CS DB_Conn.Open ' problem! DB_Status = "Open" End If End Sub我添加了相同的参考(ADO 2.8)
The first spreadsheet still works; the seccond at DB_Conn.Open pops up "Run-time error '-214767259 (80004005)': [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified" Removing the references on both, saving files, re-opening, re-adding the references doesn't help. The one still works and the other gets the error.
第一个电子表格仍然有效;DB_Conn.Open 处的第二个弹出“运行时错误‘-214767259 (80004005)’:[Microsoft][ODBC 驱动程序管理器] 未找到数据源名称且未指定默认驱动程序”删除两者上的引用,保存文件,重新-打开,重新添加引用无济于事。一个仍然有效,另一个得到错误。
?!?
?!?
回答by Deep
I observed the same error message and in my case nothing had changed. I wondered if my odbc driver needed to be reinstalled (based on what i read online). In any case, restarting excel did the trick. Sometimes the solution is much simpler. :-)
我观察到相同的错误消息,在我的情况下没有任何变化。我想知道是否需要重新安装我的 odbc 驱动程序(基于我在网上阅读的内容)。无论如何,重新启动 excel 就成功了。有时解决方案要简单得多。:-)
回答by SnakeWasTheNameTheyGaveMe
When the error pops up, check your "locals" windows to see what the CS holds. View > Locals Window
当错误弹出时,检查您的“本地人”窗口以查看 CS 保存的内容。视图 > 本地窗口
回答by Stevoni
Problem:Your constant isn't found by the compiler.
问题:编译器找不到您的常量。
Solution:With the constant being located in a separate module, you'll need to set it as Public for the other code to see it.
解决方案:由于常量位于单独的模块中,您需要将其设置为 Public 以便其他代码查看它。
Proof:
In order to prove this theory you can do the following:
Open a new Excel spreadsheet
Go to the VBA designer and add a new module
In this module put:
证明:
为了证明这个理论,您可以执行以下操作: 打开一个新的 Excel 电子表格 转到 VBA 设计器并添加一个新模块在此模块中放置:
Const TestString As String = "Test String"
Then add the following code to ThisWorkbook:
然后将以下代码添加到 ThisWorkbook:
Public Sub TestString()
MsgBox (TestString)
End Sub
After adding this return to the workbook and add a button, selecting "TestString" as the macro to run when clicked.
将此返回添加到工作簿并添加一个按钮后,选择“TestString”作为单击时运行的宏。
Click the button and a blank message box will appear. Go back to the VBA designer and change the const in Module1 to Public Click the button on the spreadsheet and you should now see "Test String" in the message box.
单击该按钮,将出现一个空白消息框。返回 VBA 设计器,将 Module1 中的 const 更改为 Public 单击电子表格上的按钮,现在您应该会在消息框中看到“Test String”。
回答by Christian
I realize that this question is really old. But for the record I want to document my solutions for the error here: It was a data related error in a spreadsheet! A column was formatted as date and contained a value 3000000. Changing the Format to numbers solved the Error 80004005.
我意识到这个问题真的很老了。但为了记录,我想在这里记录我对错误的解决方案:这是电子表格中与数据相关的错误!一列被格式化为日期并包含一个值 3000000。将格式更改为数字解决了错误 80004005。