How do I create and populate an array with the fields of
a recordset?
Printable View
How do I create and populate an array with the fields of
a recordset?
Use the fields collection. Assuming your recordset is called "rst".
P.Code:Dim myFields(rst.Fields.Count - 1) As String
For i = 0 To rst.Fields.Count -1
myFields(i) = rst.Fields.Name
Next i
Thanks. Can I use that array in any other routine?
Why not use the recordset as an array...
I.e. disconnect the recordset from the database
Then you have all the added functionality of a recordset
like eof,bof,movenext,movelast,movefirst etc
Because I want to display fields in the rst and I'm having
problems displaying such on a form with scrollbars, so I
thought I would use an array.
When I tried Paulw code, I got an "Expected array" error
when I used the array in a different subroutine
e.g.. when I declare the array in sub GetArray, and try to
access it in sub Scroll, i get the error.
[code]
Sub GetArray()
Dim rr(rst.Fields.Count - 1) As String
For i = 0 To rst.Fields.Count - 1
rr(i) = rst.recno
Next i
End Sub
Sub Scroll()
For a = 1 To rst.RecordCount
Print rr(a)
Next
End Sub
The array is out of scope. If you want to access the arrary in other fuctions, you have to dim it public or global.
Or better still, put it all in aclass and use Class methods...
Cheers,
P.
Thanks y'all, much obliged