Link to home
Start Free TrialLog in
Avatar of dgormley
dgormley

asked on

return SQL results into array

Say you dont know how many items are in your database and you want to return the results into an array. I understand you have to count the number of records then use something like i = 1 to x where x is the number of records counted.
What I want, which my brain wont let me concentrate enough to figure out is to return the results of a query into an array where I can use it several times within one asp page.

Who cares to help?
Avatar of dfu23
dfu23

ASKER CERTIFIED SOLUTION
Avatar of SquareHead
SquareHead

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, to get a count of "records" or elements of an array, you needn't do a loop. Simply do this:

numRecs = UBound(myArray, 2) + 1

UBound is a VBScript function that returns the index of the last element in an array. The "2" parameter specifies the y-direction or "row" direction of a 2-d array (if you want a field count, do the same thing but use 1 as your param). We add 1 to the UBound value because VBScript arrays are 0-based, so your total records will be 1 more than what UBound returns.

Also, using UBound, you can loop through your array without caring about when you've gotten to the last record:

for i = 0 to UBound(myArray, 2)
...do stuff
next

GoofyDawg
@GoofyDawg,
this would work even if the base is not 0
num = Ubound(arr) - LBound(arr) + 1
@dfu23 -- sorry, didn't mean to duplicate your suggestion, I neglected to follow your link above...

-- SquareHead
Avatar of dgormley

ASKER

Thanks everyone for submitting answers, I chose Squarehead since he/she typed what I was looking for. The links did help but after reading squarehead's my brain clicked and poof, i solved it. Thanks.
Eh, life goes on ...