vba 获取运行时错误 91 [未设置对象变量或块变量]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20592845/
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
getting run time error 91 [object variable or with block variable not set]
提问by user3103991
i am trying the code below to pull from access db, but getting error 91, please suggest how to remove the error.
我正在尝试使用下面的代码从访问数据库中提取,但出现错误 91,请建议如何删除错误。
Private Sub CommandButton1_Click()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConn As String
Set con = New ADODB.Connection
con.Mode = adModeReadWrite
If con.State = adStateClosed Then
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\temp\db2.mdb;Persist Security Info=False;"
con.ConnectionString = strConn
con.Open
End If
Dim startRow As Integer
***Set rs.ActiveConnection = con***
rs.Open "select * from tbl_name"
startRow = 3
Do Until rs.EOF
Cells(startRow, 4) = rs.Fields(0).Value
rs.MoveNext
startRow = startRow + 1
Loop
rs.Close
Set rs = Nothing
con.Close
Set con = Nothing
End Sub
回答by Joe
You call:
你打电话:
rs.ActiveConnection = con
rs.Open
without first creating an instance of the RecordSet:
无需先创建 RecordSet 的实例:
Set rs = New ADODB.RecordSet
回答by Catalin B
I had this error while testing an application.
我在测试应用程序时遇到了这个错误。
In my particular case, there was a problem with the Oracle Home used by the application. Therefore, the data source wasn't reachable.
在我的特殊情况下,应用程序使用的 Oracle Home 存在问题。因此,无法访问数据源。