Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

vba getting proper uboundin an array

access vba 2010

After i get a recordset count
I'm trying to put the items into an array.
The array keeps coming up with only one item.
even though i have 10 records ?
' put the items into an array for emailing each row.
                    dArrp = rs1.GetRows()
                    
                    ' just in case i need it
d = rs1.RecordCount

lngCount = UBound(dArrp, 2) + 1
                                
 For i = LBound(dArrp, 2) To UBound(dArrp, 2) ' my ubound always ends up being "one"  

Open in new window



Thanks
fordraiders
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi.

First you mustcheck if the recordset has data:

if not (rs1.bof and rs1.eof) then

Open in new window


Later you must fill the recordset
rs1.movelast
rs1.movefirst

Open in new window

and then you can get anytjing from the recordset.

Best regards.

Antonio (Barcelona, Spain)
GetRows need to know how many records to pull. Thus, to get all records:
rs1.MoveLast
rs1.MoveFirst
dArrp = rs1.GetRows(rs.RecordCount)
                    
lngCount = UBound(dArrp, 2) + 1

Open in new window

/gustav
Gustav, I will need to disagree...
https://msdn.microsoft.com/en-us/library/ms675120(v=vs.85).aspx
Rows
    Optional. A GetRowsOptionEnum value that indicates the number of records to retrieve. The default is adGetRowsRest.

which means that if you are on the last record, it will only get that one
using MoveFirst, it will get "the rest", no need to specify the number of records you want.

to test .BOF and .EOF is not needed, because if you "moved" the cursor to the last record, you know you have some records indeed.
Unable to reproduce the issue:
All the following give the same result:
    Dim trs1 As Recordset
    Dim ta As Variant
    
    Set trs1 = CurrentDb.OpenRecordset("a")
    trs1.MoveLast
    ta = trs1.GetRows()
    Debug.Print UBound(ta)
    
    Set trs1 = Nothing
    Set trs1 = CurrentDb.OpenRecordset("a")
    ta = trs1.GetRows()
    Debug.Print UBound(ta)
    
    Set trs1 = Nothing
    Set trs1 = CurrentDb.OpenRecordset("a")
    ta = trs1.GetRows(trs1.RecordCount)
    Debug.Print UBound(ta)

Open in new window

Avatar of Fordraiders

ASKER

thnx