Results 1 to 7 of 7

Thread: [RESOLVED] FILL ARRAY problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Resolved [RESOLVED] FILL ARRAY problem

    pseudo code:

    DIM MIO_ARRAY() AS VARIANT

    ...
    C = 0
    Do While Not RS.EOF

    C = C + 1
    COLA = RS.Fields("COD").Value
    MIO_ARRAY(C) = COLA
    C=C+1
    COLB = RS.Fields("COD1").Value
    MIO_ARRAY(C) = COLB
    ....

    COLZ = RS.Fields("COD78").Value
    MIO_ARRAY(C) = COLZ



    rs.movenext

    loop
    ....


    have error: Subscript out of range

    NOTE:
    The numbers of recordset are variable
    Last edited by luca90; Feb 5th, 2021 at 04:52 AM.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,444

    Re: FILL ARRAY problem

    Aircode
    Code:
    rs.movelast
    ReDim MIO_ARRAY(1 To 2*rs.Recordcount)
    rs.movefirst
    Do While Not rs.eof
    'blablablabla
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,068

    Re: FILL ARRAY problem

    Building the array dynamically is an easy cheat

    Code:
    Sub push(ary, value) 'this modifies parent ary object
        On Error GoTo init
        Dim x As Long
        x = UBound(ary) 'Error If Not initalized
        ReDim Preserve ary(UBound(ary) + 1)
        ary(UBound(ary)) = value
        Exit Sub
    init: ReDim ary(0): ary(0) = value
    End Sub
    
    Private Sub Form_Load()
        Dim x() As Variant
        push x, 1
        push x, 2
        Debug.Print UBound(x) & " x(1) = " & x(1)
    End Sub
    I would probably build a class to hold the columns with public properties for each field so you had intellisense.
    Hold each class instance in a collection
    and then have a class.Init(rs) method to have it load itself from the current record.

    Code:
    Dim records as new collection
    dim record as CRecord
    
    while not rs.eof
      set record = new CRecord
      record.init(rs)
      records.add record
      rs.movenext
    wend
    
    'class CRecord
    '------------------------
    public COD as Variant
    
    friend Sub Init(rs as recordset)
        me.COD = rs("COD").value
        ...
    end sub
    Last edited by dz32; Feb 5th, 2021 at 09:01 AM.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: FILL ARRAY problem

    "Do While Not End" is a silly double-negative. Use "Do Until End" instead.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: FILL ARRAY problem

    Quote Originally Posted by dilettante View Post
    "Do While Not End" is a silly double-negative. Use "Do Until End" instead.
    Never used...
    Example please.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: FILL ARRAY problem

    Just replace "While" with "Until" and then drop the "Not" from the expression which follows.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: FILL ARRAY problem

    Quote Originally Posted by dilettante View Post
    Just replace "While" with "Until" and then drop the "Not" from the expression which follows.
    Do Until RS.EOF

    ...

    loop

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