1

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

bissleboss
  • 13
  • 3

1 Answers1

2

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

Ken White
  • 123,280
  • 14
  • 225
  • 444
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • 1
    Please could you clarify to me the correct use with wildcards in my code above, i still can't understand the solution that you recommend to me above – bissleboss Jul 15 '21 at 17:11
  • What is it that you don't understand about the `Country LIKE 'F%'` I quoted from the article.? Btw, I think the change you have edited into your q is wrong - it stilll has the line `ref_Abonne:= QuotedStr (scGPEdit2.Text + '*')` where you incorrectly are still adding the ADO `*` wildcard, – MartynA Jul 15 '21 at 17:33
  • Thank you Mr @MartynA for your reply, Sorry i don't understand english quickly, But with your UPDATED Reply now i understand your Answer Above, thank you so much and your Example was so Usefull – bissleboss Jul 15 '21 at 20:53