Query executes FilterRecord only once

1

I have a query in tQuery that has the need to do FilterRecord. After displaying the data I do an internal search in the query for a more specific item, such as the name, but when I search the query it promptly performs the search correctly, when I remove letters, it does not pass through FilterRecord again, even using Query. Refresh.

Does anyone know how I can get TQuery to run FilterRecord again?

    
asked by anonymous 19.06.2015 / 14:46

2 answers

1

Good @LuizVichiatto, as promised in the previous response comment I did an example which works perfectly.

Step 1: Put a DataSource, DbGrid, Edit and a DataSet in my Form in my case I used the ClientDataSet, but it could be any other one that supports the Filter property.

Step 2: Set the events as below

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBClient, Grids, DBGrids;

type
  TForm1 = class(TForm)
    cdsBusca: TClientDataSet;
    EditBusca: TEdit;
    Label1: TLabel;
    cdsBuscaNome: TStringField;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure EditBuscaChange(Sender: TObject);
    procedure cdsBuscaFilterRecord(DataSet: TDataSet; var Accept: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i: integer;
begin
  cdsBusca.CreateDataSet;
  cdsBusca.Append;
  cdsBuscaNome.Value := 'Monica';
  cdsBusca.Append;
  cdsBuscaNome.Value := 'Eduardo';
  cdsBusca.Append;
  cdsBuscaNome.Value := 'Renato Russo';
  cdsBusca.Post;
end;

procedure TForm1.EditBuscaChange(Sender: TObject);
begin
  cdsBusca.Filtered := False;
  cdsBusca.Filter := '';
  cdsBusca.Filtered := True;
end;

procedure TForm1.cdsBuscaFilterRecord(DataSet: TDataSet;
  var Accept: Boolean);
begin
   if EditBusca.Text = '' then
   begin
      cdsBusca.Filtered := False;
      Exit;
   end;
   Accept := Pos(EditBusca.Text, cdsBuscaNome.AsString) > 0;
end;

end.

HereIaddedthenameEdmilson,onlytofiltereverythingthatcontainedtheletter"AND"

    
07.07.2015 / 17:09
1

Well, as was said in the comments above, to call the FilterRecords event must be added some condition in the filter property of the DataSet example:

TQuery.Filtered := False;
TQuery.Filter := //Condição;
TQuery.Filtered := True;

About typing a letter and filtering you could put the following condition in the FilterRecords event, forcing the filter is clear:

procedure TForm1.TQueryFilterRecord(Dataset: TDataset; var Accept: Boolean);
begin
  Accept := Pos(EditBusca.Text, DataSet.FieldByName(NomeItem).AsString) > 0;
end;

An easier and recommended solution is to check if the Like operator is supported for the database that you are using if it is, you can do the following:

TQuery.Filter := 'NomeCampo LIKE '+ quotedStr('%') +
EditBusca.Text + quotedStr('%');
TQuery.Filtered := True;

Reference: Data.DB.TDataSet.Filter

    
28.06.2015 / 21:08