How to migrate from ADO Filtering code to Firedac

Viewed 215

I have this code:

datamodule1.tbabonne.Filter := '';
  if (scGPEdit2.Text) = '' then exit ;
  try
    ref_Abonne:= QuotedStr (scGPEdit2.Text + '*');
    if (scGPEdit2.Text <> '') then
      datamodule1.tbabonne.Filter:= Format('(ref_Abonne LIKE %s)', [ref_abonne])
    else
      datamodule1.tbabonne.Filtered := Trim((scGPEdit2.Text)) <> '' ;
  except
    abort;
  end;
  //edit1.Text := '';
  end;

My question is :
the code Above didn't work with Firedac while is working as charm in ADO

1 Answers

In FireDAC filters, the wildcards are _ for a single character and % for multiple characters - see http://docwiki.embarcadero.com/Libraries/Sydney/en/FireDAC.Comp.Client.TFDQuery.Filter which gives this example

You can use standard SQL wildcards such as percent (%) and underscore (_) in the condition when you use the LIKE operator. The following filter condition retrieves all Countries beginning with 'F'

Country LIKE 'F%'

So you need to adjust your line

ref_abonne:= QuotedStr (scGPEdit2.Text + '*');

accordingly, to use the LIKE operator and the % wildcard.

Just guessing but maybe ADO used the * wildcard and = operator to insulate e.g. VB users from SQL wildcards and syntax.

UpdateHere is a sample project which uses the FireDAC % wildcard and LIKE operator in a filter. Take careful note of the inline comments.

  TForm1 = class(TForm)
    //  Create a new VCL project and drop the following components
    //  onto it.  There is no need to set any of their properties
    FDMemTable1: TFDMemTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    edFilter: TEdit;
    //  Use the Object Inspector to create the following event handlers
    //  and add the code shown in the implementation section to them
    procedure FormCreate(Sender: TObject);
    procedure edFilterChange(Sender: TObject);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.edFilterChange(Sender: TObject);
begin
  UpdateFilter;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.DataSource := DataSource1;
  DataSource1.DataSet := FDMemTable1;

  //  Adjust the following line to suit the location of Employee.Fds on your system
  FDMemTable1.LoadFromFile('D:\D10Samples\Data\Employee.Fds');

  FDMemTable1.IndexFieldNames := 'LastName;FirstName';
  FDMemTable1.Open;
  FDMemTable1.First;

  //  Make the filter disregard string case
  FDMemTable1.FilterOptions := [foCaseInsensitive];

  UpdateFilter;
end;

procedure TForm1.UpdateFilter;
var
  FilterExpr : String;
begin
  FilterExpr := edFilter.Text;
  if FilterExpr <> '' then
    FilterExpr := 'LastName Like ' + QuotedStr(FilterExpr + '%');
  FDMemTable1.Filter := FilterExpr;
  FDMemTable1.Filtered := FDMemTable1.Filter <> '';
end;

Then just compile and run

Related