Results 1 to 5 of 5

Thread: Populate 2 dimensional array from 1 row in recordset

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 1999
    Location
    Greenville, MS, Washington
    Posts
    38
    I get error "subscript out or range" with the following code.

    Code:
        Dim sSQL As String
        Dim aryData(1, 5)
        Dim n As Integer, i As Integer
        
        sSQL = "SELECT * FROM Oxygen " & _
        "WHERE pond = 'A1A'"
        
        Set rsoxygen = GetRecordSet(sSQL)
        
        i = 0
        
        For n = 4 To 14 Step 2
            aryData(0, i) = aryData(0, rsoxygen(n))
            aryData(1, i) = aryData(1, rsoxygen(n + 1))
            i = i + 1
        Next

  2. #2
    Addicted Member pardede's Avatar
    Join Date
    Jan 2000
    Posts
    232
    It seem to me that your array is not big enought to hold the data want to put it in, it is dimensionised as (1,5) but you are trying to put 7 items in it (4 to 14 step 2, those are 7 items)...

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 1999
    Location
    Greenville, MS, Washington
    Posts
    38
    4,6,8,10,12,14 is 6 not 7


    This seems to work. But I get "bad function argument" when I try to use it to load mschart which is why I needed that array in the first place.

    Code:
    For n = 4 To 14 Step 2
            aryData(0, i) = rsoxygen(n)
            aryData(1, i) = rsoxygen(n + 1)
            i = i + 1
            
        Next
         
        MSChart1.ChartData = aryData

  4. #4

    Why not do it this way?

    He's right, but here is another option...

    ------------- JUST AN OPTION -------------

    I have used Arrays in my code all the time. But I think you might want to see if this way "suits your fancy".

    The ADO Recordset object has two methods that allow you to retrieve the information from the recordset in two formats.

    1) Array format = oRS.GetValues
    2) String format = oRS.GetString

    The first (1) is the way you want to get the information.

    Code:
    Dim oRS as ADODB.Recordset
    Dim sSQL as string
    Dim vValues as variant
    
    ssql = ""
    ssql = ssql & "Select LastName, FirstName from Names Where State = 'CA'"
    
    Set ors = new adodb.recordset
    ors.open ssql, oConn, adForwardOnly, adReadOnly
    vValues = ors.GetValues(x) ' If you put a value here for
        ' x, you will get that many records back. Otherwise 
        ' leave it out and it will return all records.
    if not ors is nothing then ors.close
    set ors = nothing
    Now you can look at the vValues array as vValues(Row, Field) (or its the other way around; can't remember off the top of my head)

    This is a very fast way of doing what you are already intending on doing with your other code.

    Have fun experimenting.
    Senior Systems Architect/Programmer

  5. #5

    My mistake

    I am sorry to have given you some back code. Please change the previous statement of

    1) Array format = oRS.GetValues
    2) String format = oRS.GetString

    to

    1) Array format = ors.GetRows
    2) String format = ors.GetString

    Sorry about that.
    Senior Systems Architect/Programmer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width