getting run time error 91 [object variable or with block variable not set]

大城市里の小女人 提交于 2019-12-08 12:30:38

问题


i am trying the code below to pull from access db, but getting error 91, please suggest how to remove the error.

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

回答1:


You call:

rs.ActiveConnection = con
rs.Open

without first creating an instance of the RecordSet:

Set rs = New ADODB.RecordSet



回答2:


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.



来源:https://stackoverflow.com/questions/20592845/getting-run-time-error-91-object-variable-or-with-block-variable-not-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!