How to coding patient search app in Delphi?

92 Views Asked by At

I'm working on a patient search app. I have a problem with ADOQuery.Active which does not deactivate when I delete a word in the search bar.

This is my code:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Mask, scControls,
  scDBControls, scGrids, scDBGrids, scGPControls, Data.DB, Data.Win.ADODB;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    DataSource1: TDataSource;
    Edit1: TEdit;
    scDBGrid1: TscDBGrid;
    ADOQuery1: TADOQuery;
    ADOQuery1PATIENTId: TAutoIncField;
    ADOQuery1NAME_PAT: TStringField;
    ADOQuery1PRENOM_PAT: TStringField;
    procedure Edit1Change(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if Edit1.Text = '' then
    ADOQuery1.Filtered := false
  else
  begin
    ADOQuery1.Active := true;
    ADOQuery1.Filtered := False;
    ADOQuery1.Filter := 'NAME_PAT' + ' LIKE ' + QuotedStr(Edit1.Text + '%');
    ADOQuery1.Filtered := True;
  end;
end;

end.

After clean TEdit, I set ADOQuery1.Active := false

1

There are 1 best solutions below

0
Softacom On

The problem here is you are not affecting the right properties according to your first description. Generally in database applications, you don't need to deactivate the dataset, just write an event for disabling the filter 'Filtered := False' that will be triggered when the filter text input is cleared (by deleting the filter string manually or a "clear text" button). But, if you need to deactivate the dataset, then you have to set that property in your code. Like this:

procedure TForm1.Edit1Change(Sender: TObject); 
begin
  if Edit1.Text = '' then 
  begin
    ADOQuery1.Filtered := False;
    ADOQuery1.Active := False;
  end
  else
  begin
    ADOQuery1.Active := True;  
    ADOQuery1.Filtered := False; 
    ADOQuery1.Filter := 'NAME_PAT' + ' LIKE ' + QuotedStr(Edit1.Text + '%');
    ADOQuery1.Filtered := True;
  end; 
end;

Here is some useful links from Delphi documentation about the "TDataSet.Active" property and how to set filters in datasets:

https://docwiki.embarcadero.com/Libraries/Alexandria/en/Data.DB.TDataSet.Active https://docwiki.embarcadero.com/Libraries/Alexandria/en/Data.DB.TDataSet.Filter https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Setting_the_Filter_Property