Reputation: 2573
I'm using Delphi 10.2. I'm trying to filter a ClientDataSet to generate a report of only new records. 'Idnm' is a string field containing a number.
The following code fails because Idnm is left justified and six digit numbers starting with 6 show up as being greater than '5123456'.
Var StartID : string;
...
StartID := '5123456';
ClientDataSet1.Filter := 'Idnm > ' + StartID;
ClientDataSet1.Filtered := true;
How can I format Idnm or StartID so that the filter works?
Upvotes: 1
Views: 455
Reputation: 30715
Unfortunately, using the Filter property of a TClientDataSet filter to do comparisons between integer values does not work properly because the underlying expression-processing does not readily support type-conversion from string representations to integer values. It is much more straightforward to leave the Filter property blank and use the OnFilterRecord
event to determine whether records match your integer filter criterion. Something like this:
var
iStartID : Integer; // a field of your form or datamodule
[...]
StartID := 5123456;
iStartID := StrToInt(StartID);
procedure TForm1.CDS1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
// set Accept to True if the current record matches the filter condition(s)
begin
// Set iStartID from your StartID value before activating the filter
// CDS1ID is a persistent field defined for the ID. Otherwise, use
// CDS1.FieldByName('ID').AsInteger
Accept := CDS1ID.AsInteger > iStartID;
end;
When you want to change the StartID, toggle CDS1.Filtered off (False), change the StartID/iStartID values and then turn Filtered back on (True).
Note, I've used the iStartID variable to avoid the performance overhead of doing CDS.FieldByName for each row in the dataset.
Upvotes: 3