Im using this code to filter my table:
Table.Filtered := False;
Table.Filter := \'[\' + Field_Search + \'] LIKE \'\'%\' + Edit_Search.Text + \'%\'\'\';
Tabl
Table.Filtered := False;
Table.Filter := Field_Search + ' LIKE ' + QuotedStr('*' + Edit_Search.Text + '*');
Table.Filtered := True;
you should use this :
DataModule.Table.Filtered := False;
DataModule.Table.Filter := 'Field_Name' + ' LIKE ' + QuotedStr(Edt_SearchByCode.Text +'%');
DataModule.Table.Filtered := True;
and will work like a Magic and no use of TQuery any more .... and if you want Matching does not take case-sensitivity into account. you should use this code instead:
DataModule.Table.Filtered := False;
DataModule.Table.FilterOptions := [foCaseInsensitive];
DataModule.Table.Filter := 'Field_Name' + ' LIKE ' + QuotedStr(Edt_SearchByCode.Text +'%');
DataModule.Table.Filtered := True;
I am using mostly with TEdit here is the code
if (EditSerarch.Text <> '') then
begin
FDQQuery.Filtered := false;
FDQQuery.Filter:= 'Name LIKE'+ QuotedStr('%'+EditSerarch.Text+'%') +
' OR Company LIKE '+ QuotedStr('%'+EditSerarch.Text+'%') +
' OR Phone LIKE '+ QuotedStr('%'+EditSerarch.Text+'%') +
' OR Mobile LIKE '+ QuotedStr('%'+EditSerarch.Text+'%');
FDQQuery.Filtered:= True;
end else FDQQuery.Filtered := false;
I recommend to use a SQL Query to perform this operation.
Example:
query1.SQL.Text:='Select * FROM table_name WHERE field like '+ QuotedStr(edit1.text+'%');
query1.Active:=true;
The following Code will work as Like as well:
if Edit1.Text <>'' then
begin
Query1.Filter :='FieldName ='+quotedstr('*'+ edit1.Text +'*');
Query1.Filtered:=true;
end
else
begin query1.Filtered :=false; end;
A TTable.Filter
isn't a SQL query. LIKE
isn't supported (neither is IN
). The supported operators are =
, <>
, >
, <
, >=
, '<=,
AND,
NOTand
OR`, according to the documentation
For more complicated filtering, use the TDataSet.OnFilterRecord event:
procedure TForm1.Table1FilterRecord(Dataset: TDataset; var Accept: Boolean);
begin
// Don't remember if D7 supports DataSet[FieldName] syntax; if not,
// use DataSet.FieldByName instead, or a persistent field.
Accept := Pos(Edit_Search.Text, DataSet[SearchField].AsString) > 0;
end;