Results 1 to 9 of 9

Thread: FetchArray function - 2 dimensional arrays

  1. #1

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    FetchArray function - 2 dimensional arrays

    VB Code:
    1. Function FetchArray(strQuery As String, conn As ADODB.Connection) As Variant
    2. Dim resBuff() As Variant
    3. Dim nRS As ADODB.Recordset
    4. Dim fld As ADODB.Field
    5.  
    6. Dim iRow As Integer, iField As Integer
    7.  
    8. Set nRS = New ADODB.Recordset
    9. nRS.CursorLocation = adUseServer
    10.  
    11. nRS.Open strQuery, conn
    12. nRS.MoveFirst
    13.  
    14. iRow = -1
    15. iField = -1
    16.  
    17. Do Until nRS.EOF
    18.     iRow = iRow + 1
    19.     For Each fld In nRS.Fields
    20.         iField = iField + 1
    21. [b]        resBuff(iRow, iField) = fld.Value[/b]
    22.         Debug.Print resBuff(iRow, iField)
    23.     Next
    24. Loop
    25. End Function

    It seems to have trouble on ^ that line.

    I tried resBuff(iRow)(iField) = fld.Value but I can't even remember how to do 2d arrays in vb now.

    I was hoping to re create some of the PHP->mysql functions in vb because i hate recordsets

    The idea of the above is to load a query into an array.

    Any ideas?

  2. #2
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    VB Code:
    1. Function FetchArray(strQuery As String, conn As ADODB.Connection) As Variant
    2. 'got to now how many fields you got in the rs
    3.    Dim resBuff(1, 20) As Variant
    4.    Dim nRS As ADODB.Recordset
    5.    Dim fld As ADODB.Field
    6.    
    7.    Dim iRow As Integer, iField As Integer
    8.    
    9.    Set nRS = New ADODB.Recordset
    10.    nRS.CursorLocation = adUseServer
    11.    
    12.    nRS.Open strQuery, conn
    13.    nRS.MoveFirst
    14.    
    15.    iRow = -1
    16.    iField = -1
    17.    
    18.    If nRS.RecordCount Then
    19.       nRS.MoveLast
    20.       nRS.MoveFirst
    21.    End If
    22.    ReDim resBuff(nRS.RecordCount, 20)
    23.    Do Until nRS.EOF
    24.        iRow = iRow + 1
    25.        For Each fld In nRS.Fields
    26.            iField = iField + 1
    27.            resBuff(iRow, iField) = fld.Value
    28.            Debug.Print resBuff(iRow, iField)
    29.        Next
    30.    Loop
    31. End Function
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  3. #3

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Array already dimensioned

  4. #4

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Also, it returns recordcount as -1 but i know there are rows returned

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Why make it so difficult?
    VB Code:
    1. Function FetchArray(strQuery As String, conn As ADODB.Connection) As Variant
    2.    Dim nRS As ADODB.Recordset
    3.    
    4.    Set nRS = New ADODB.Recordset
    5.    nRS.CursorLocation = adUseServer
    6.    
    7.    nRS.Open strQuery, conn
    8.    FetchArray = nRS.GetRows
    9.    nRS.Close
    10.    Set nRS = Nothing
    11. End Function

    EDIT: added some cleanup code
    Frans

  6. #6

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    It doesn't return it in 2 dimensions though

    I wanted:

    res(0, 2) to return the 3rd field in the first row, similar to PHP

    etc

    res(1, 1) 2nd row 2nd field

    any more ideas?

  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    GetRows should return a two dimensional array, but in reverse order then you want. The first dimension is the field, and the second dimension the rownumber, so res(0,2) should return the first field in the third row.
    If you want it the other way round, you would have to populate the array yourself. Because you can only redim Preserve the second dimension, you would have to get an accurate recordcount before you start to populate the array. Not all data providers support the recordcount property though (that is why you get -1 for recordcount).
    Frans

  8. #8

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    How do i get the ubound of the second array then?

    ubound(myarray(1)) didn't seem to work?

  9. #9

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Solved;

    UBound(myArray, 2) did the trick

    Thanks for the help FransC

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