问题
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