Reputation: 19733
To enhance performance and resources, I've just started to use getRows() on a few of my scripts. I have just come across an issue, which I'd like to ask about.
I was doing this to get the recordset and to get the count:
If NOT rs.EOF Then
arrResultSet = rs.GetRows()
arrRowCount = UBound(arrResultSet,2)
End If
But then I realised I was missing a record so I added 1 to my count:
If NOT rs.EOF Then
arrResultSet = rs.GetRows()
arrRowCount = UBound(arrResultSet,2) + 1
End If
But now I get an error later in my script when I try accessing the data array which is purely down to adding one to my count:
For iCounter = 0 to arrRowCount
...some code...
If LCase(Trim(peopleWord)) = LCase(Trim(arrResultSet(1,iCounter))) Then
...some code...
Next
Microsoft VBScript runtime error '800a0009'
Subscript out of range: 'lcase(...)'
Any help greatly appreciated.
Upvotes: 3
Views: 14702
Reputation: 32851
Your For
is going from the index of 0 to the index of the arrRowCount
.
So, for example, if you have three records, you are going from 0 to 3, which is 4, right? IIRC, we used to do this: For iCounter = 0 to arrRowCount - 1
Edit: Perhaps this example will help you. This web page details why using GetRows
yields a performance improvement, so I think you're on the right track. I have included the entire code sample, but you are interested in the part at the end. It has less code, and fewer variables, than you are using. It looks cleaner, simpler.
' Establish the connection object
strConn = "[connection string goes here]"
set dbConn = Server.CreateObject("ADO.Connection")
dbConn.Open strConn
' Establish the recordset object
set rsCustomers = Server.CreateObject("ADO.Recordset")
set rsCustomers.ActiveConnection = dbConn
' Load the recordset object based on supplied query
strSQL = "SELECT RecID, FirstName, LastName FROM Customers"
rsCustomers.Open strSQL
' Push the results into a two-dimensional array
dataArray = rsCustomers.GetRows()
' Cleanup the objects. We do it here instead of at the end because the data
' has already been placed into an array. This is an advantage in that we can release
' memory sooner.
rsCustomers.Close
set rsCustomers = nothing
dbConn.Close
set dbConn = nothing
' Retrieve the records performing business logic where necessary
jMax = ubound(dataArray, 2)
for j = 0 to jMax
'Additional business logic here
next
Upvotes: 6