return SQL results into array

Posted on 2005-04-13
Last Modified: 2010-08-05
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?
Question by:dgormley
    LVL 14

    Expert Comment

    LVL 18

    Accepted Solution

    Easiest way is to use the ADO GetRows method...

    Set oRS = oConn.Execute("Select * from members")

    if not(oRS.EOF) then

    arrayRS = oRS.GetRows()


    arrayRS is now a 2-dimesional array...
    LVL 18

    Expert Comment

    LVL 12

    Expert Comment

    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) stuff

    LVL 8

    Expert Comment

    this would work even if the base is not 0
    num = Ubound(arr) - LBound(arr) + 1
    LVL 18

    Expert Comment

    @dfu23 -- sorry, didn't mean to duplicate your suggestion, I neglected to follow your link above...

    -- SquareHead

    Author Comment

    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.
    LVL 14

    Expert Comment

    Eh, life goes on ...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now