Record Count

  • Thread starter Thread starter Gary Hull
  • Start date Start date
G

Gary Hull

I am using the following code to produce a message box if my
filter selects more than one record

If I step through the code every thing works fine.
When I just run the code I get the wrong record count


FilterCount = Me.RecordsetClone.ReordCount
If FilterCount > 1 Then
MsgBox "You have selected multi items"
End If
 
Gary said:
I am using the following code to produce a message box if my
filter selects more than one record

If I step through the code every thing works fine.
When I just run the code I get the wrong record count


FilterCount = Me.RecordsetClone.ReordCount
If FilterCount > 1 Then
MsgBox "You have selected multi items"
End If


The RecordCount property for all, except table type,
recordsets returns the number of records that have accessed.
Initially, that is often just the first record.

You need to add a MoveLast before referencing the count:

With Me.RecordsetClone
.MoveLast
If .RecordCount > 1 Then
MsgBox "You have selected multi items"
End If
End With
 
The RecordCount property for all, except table type,
recordsets returns the number of records that have accessed.
Initially, that is often just the first record.

You need to add a MoveLast before referencing the count:

With Me.RecordsetClone
.MoveLast
If .RecordCount > 1 Then
MsgBox "You have selected multi items"
End If
End With

I must admit I came across opinion that recordcount is not reliable if
you don't invoke MoveLast first, but as I didn't know about it and
wrote my prorgams without MoveLast they somehow always returned
correct number of records. (on Access 2003 SP2)
 
Morris said:
I must admit I came across opinion that recordcount is not reliable if
you don't invoke MoveLast first, but as I didn't know about it and
wrote my prorgams without MoveLast they somehow always returned
correct number of records. (on Access 2003 SP2)


Unreliable is an inaccurate way to describe this phenomenon.
It may appear that way because you can't always determine
how many records have been accessed, which happens in a
background, asynchronous process. It may very well be the
case that RecordCount returns the total number of records
for a recordset with a very small number of records, but you
can not rely on the background, asynchronous process always
having completed its activity by the time you reference the
RecordCount property.

Unless you use MoveLast or something else that forces all
the records to be accessed, the only thing you can be sure
of is that RecordCount will be 0 if the recordset has no
data or it will be greater than 0 if the recordset has data.

Note that a table type recordset always "knows" how many
records are in the dataset, so in this special case,
RecordCount always returns the total number of records.
 
Back
Top