3

I'm slowly moving from ClientDataSet to FireDAC FDQuery components in my projects (Delphi 10 Seattle).

One trick I often use with CDS is to check record count on a range.

That is:

CDS.SetRange([Value1][Value2]);  
k := CDS.RecordCount;  
case k of  
  1 : DoSingleThing;  
  2 : DoDoubleThing;  
else  
  BailOnWrongCount;  
end;  

Because I need the entire set of data available at the same time, I use FetchOptions.Mode := fmAll when first opening the query.

Doing FDQuery.SetRange([Value1][Value2]); then calling FDQuery.RecordCount always returns the record count of the entire dataset (as per fmAll) - not the current range.

I'm having to loop through the range counting records manually.

Is there a simpler way to get the number of records in the current range?

Johan
  • 74,508
  • 24
  • 191
  • 319
edbored
  • 274
  • 4
  • 11

1 Answers1

5

Set FetchOptions.RecordCountMode to cmVisible: http://docwiki.embarcadero.com/Libraries/Seattle/en/FireDAC.Stan.Option.TFDFetchOptions.RecordCountMode

da-soft
  • 7,670
  • 28
  • 36
  • Once again the blindingly obvious. Thanks for that - having worked out setting FetchOptions.Mode appropriately, I completely missed RecordCountMode (or rather, had read about it some time ago and completely forgotten). Slowly getting ahead of this, and very appreciative of your support. – edbored Mar 29 '16 at 01:48
  • - just to be certain - does cmVisible return a different result if connected to a DBGrid? I think that's what originally confused me: it isn't the currently visible records in a grid, but is actually the sub set of all possible records that are currently accessible - whether filtered or limited by range - is that correct? – edbored Mar 29 '16 at 01:55
  • cmVisible means, the number of records, which are accessible through TDataSet navigational API (First, Next, Eof, etc) with all current filtering / limiting settings. cmVisible is not related to GUI or TDBGrid. May be better name will be cmAccessible, but that name may lead to other doubts ... – da-soft Mar 29 '16 at 09:58