Query runs Filterrecord only once

Asked

Viewed 476 times

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 more specific item, for example the name, but when I search in the query it promptly executes the search correctly, when I remove letters does not go through Filterrecord novamnte, even using Query.Refresh.

Does anyone know how to get Tquery to run Filterrecord again ?

  • Filterrecord is only executed when there is something in the property Filter and a different record becomes active. If you need to re-read the records, change the Tquery SQL.

  • Then it is necessary to put a filter. And if the filter is already in the query, but need at the time of display on the grid, in case a search inside what is listed in Gridview, as for example the name. So it is not necessary to do OPEN and CLOSE in the query every time.

  • Tquery is not designed for this type of behavior. If you want to filter the content that has already been searched and shown in Dataset, you will have to use another component that allows this type of manipulation, such as Tclientdataset.

2 answers

1


Good @Luizvichiatto, as I promised in the comment of the previous reply I made an example that works perfectly.

Step 1: put in the Form a Datasource, Dbgrid, Edit and a Dataset in my case I used Clientdataset, but it could be anyone else that supports the Filter property.

Step 2: Configure 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.

inserir a descrição da imagem aqui

Here I added the name Edmilson, just to filter out everything that contained the letter "And" inserir a descrição da imagem aqui

  • Hello, Wellington. Thank you, I will do the tests and inform you of the result. att Luiz!

  • Fine, it works as directed. Thanks for the tip. Just got a problem, a dll such as Midas that is not updating, I believe it is another issue.

1

Well, as stated in the above comments, to call the Filterrecords event must be added some condition in the filter property of the example Dataset:

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

On the question of typing a letter and filtering you could put the following condition on Filterrecords event, forcing the filter of course:

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’re using if you are, you can do the following:

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

Reference: Data.DB.Tdataset.Filter

  • There are no paragraphs, the 4 spaces are always to define a code block.

  • This is already being done, the problem is that I run the query, it processes Filterrecord, when I fill an Edit.text, but if I change Edit.text it does not run Filterrecord again

  • @Luizvichiatto, have you tried using your code in the Onchange event of this Edit.text? I say force the passage on Filtterrecord when you change what’s on Edit.

  • ola Wellington, yes and the result is the same.

  • @Luizvichiatto, later I send you an example working, because now I’m at work

  • @Luizvichiatto, I already put the example in another answer, was malz the delay was too busy, vlw

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.