Rowset does not support scrolling backward
Asked Answered
P

2

6

I am trying to query a MySQL database with the below code:

'declare the variables 
Dim Connection
Dim Recordset
Dim SQL

'declare the SQL statement that will query the database
SQL = "SELECT * FROM CUSIP"

'create an instance of the ADO connection and recordset objects
Set Connection = CreateObject("ADODB.Connection")
Set Recordset = CreateObject("ADODB.Recordset")

'open the connection to the database
Connection.Open "DSN=CCS_DSN;UID=root;PWD=password;Database=CCS"

Recordset.CursorType=adOpenDynamic

'Open the recordset object executing the SQL statement and return records 

Recordset.Open SQL,Connection
Recordset.MoveFirst

If Recordset.Find ("CUSIP_NAME='somevalue'") Then
    MsgBox "Found"
Else
    MsgBox "Not Found"
End If


'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing

Whenever I execute the above I get an error 'rowset does not support scrolling backward', any suggestions?

Prestonprestress answered 2/1, 2013 at 12:45 Comment(1)
according to msdn.microsoft.com/en-us/library/ee275542%28v=bts.10%29.aspx you don't use .Find correctly.Crossed
A
7

adOpenDynamic is not declared in VBScript and therefore equals Empty, which gets converted to 0 when you assign the CursorType property.
0 is adOpenForwardOnly, and forward only does not support moving backwards, an ability the Find method wants.

You should replace adOpenDynamic with its literal value:

Recordset.CursorType = 2 'adOpenDynamic

To avoid this class of errors altogether, place Option Explicit as the first line of your script.

Ailin answered 2/1, 2013 at 12:53 Comment(5)
That did the trick ! Thanks !! btw would you know why 'recordset.recordcount' returns -1 when there are 5 rows in the database and cursortype changed to dynamic ?Prestonprestress
@TelsonAlva Because it is documented to do so: The cursor type of the Recordset object affects whether the number of records can be determined. The RecordCount property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and either -1 or the actual count for a dynamic cursor, depending on the data source.Ailin
Oh, i understand now ! So i need to change the cursor type to static in order to get the count. Otherwise since it is dynamic there are possibilities for the record to change dynamically. Thanks !Prestonprestress
One last question, my Find statement is not returning a match. Any reasons ? i have set the exact string which is under the coloum CUSIP_NAME but still i get 'Not Found'Prestonprestress
@TelsonAlva Because, as Ekkehard.Horner noted, you're not using Find properly. It's a sub and it does not return a value. It moves to a record. If it doesn't find one, .EOF becomes true.Ailin
K
0

That is because the rowset does not permit backward moves; as the error message suggests. Your code is not using them; so you should replace the line

Recordset.CursorType=adOpenDynamic with Recordset.CursorType=adOpenForwardOnly (or the equivalent value 0)

Better leave the line altogether; the default is forward cursor.

Kone answered 2/1, 2013 at 12:50 Comment(1)
You got it the other way round. The Find method wants ability to move backwards.Ailin

© 2022 - 2024 — McMap. All rights reserved.