Question : Access VBA probplem with GetRows

Hi

I am trying to get an array of the ID numbers of all records where the "Invoice" field is checked using the following code, but the line MyIds = rs.GetRows(rs.RecordCount) only seems to gather the first two out of 3 checked items. That is why the last line errors out

Private Sub btnInvoice_Click()

    Dim db As Database
    Dim rs As dao.Recordset
    Dim MyIds As Variant
 
    Set db = CurrentDb
    Set rs = db.OpenRecordset("Select ID from [Room Bookings] where Invoiced = true")
    'MyIds = rs.GetRows(100000) ' any number more than the max number you might get

    MyIds = rs.GetRows(rs.RecordCount)
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    ' you now have an array of ids in MyIDs - for testing use the next line to show how many
    MsgBox UBound(MyIds) + 1 '
   
    Dim i As Integer
    i = 2
    MsgBox MyIds(0, i)
   
End Sub

Answer : Access VBA probplem with GetRows

You might try .MoveLast and .MoveFirst to insure the recordset is filled:

Set rs = db.OpenRecordset("Select ID from [Room Bookings] where Invoiced = true")
rs.MoveLast
rs.MoveFirst

'/note I also use an arbitrarily large number instead of .Recordcount
MyIds = rs.GetRows(99999999)
Random Solutions  
 
programming4us programming4us