
{                                                                              }
{ FRM_Filters                                                                  }
{                                                                              }

unit
  FRM_Filters;

{$I IB_Directives.inc}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
{$ifdef IBO_VCL60_OR_GREATER}
  Variants,
{$endif}
  Dialogs, DB, IBODataset, Grids, DBGrids, IB_Components, StdCtrls,
  IB_Script, IB_Process, IB_Samples;

type
  TForm1 = Class(TForm)
    DBGrid2: TDBGrid;
    dbFilters: TIBODatabase;
    qryMaster: TIBOQuery;
    updMaster: TIBOUpdateSQL;
    dsMaster: TDataSource;
    qryDetail: TIBOQuery;
    dsDetail: TDataSource;
    updDetail: TIBOUpdateSQL;
    CheckBox1: TCheckBox;
    DBGrid1: TDBGrid;
    Button1: TButton;
    scFilters: TIB_Script;
    Memo1: TMemo;
    procedure qryMasterAfterScroll(DataSet: TDataSet);
    procedure CheckBox1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure qryDetailFilterRecord(DataSet: TDataSet;
      var Accept: Boolean);
    procedure qryDetailNewRecord(DataSet: TDataSet);
    procedure Button1Click(Sender: TObject);
    procedure dbFiltersAfterConnect(Sender: TIB_Connection);
    procedure dbFiltersBeforeConnect(Sender: TIB_Connection);
    procedure scFiltersMacroSubstitute(Sender: TComponent;
      const ATextBlock: String; var ATextResult: String);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ReadDefaultPath( dbFilters, 'Filters' );
  dbFilters.Open;
  qryMaster.Open;
  qryDetail.Open;
end;

procedure TForm1.dbFiltersBeforeConnect(Sender: TIB_Connection);
begin
  if NeedToCreateDataBase then
    scFilters.Execute;
end;

procedure TForm1.dbFiltersAfterConnect(Sender: TIB_Connection);
begin
  WriteDefaultPath;
end;

procedure TForm1.scFiltersMacroSubstitute(Sender: TComponent;
  const ATextBlock: String; var ATextResult: String);
begin
  DefaultMacroSubstitute( ATextBlock, ATextResult );
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := dbFilters.endWithConfirm = mrOk;
end;

Procedure TForm1.qryMasterAfterScroll(DataSet: TDataSet);
begin
  if qryMaster.FieldByName('ID').AsString <> '' Then
  begin
    qryDetail.Filter := 'MASTER_ID=' + qryMaster.FieldByName('ID').AsString;
    qryDetail.Filtered := True;
  end;
end;

Procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  qryMaster.Close;
  qryDetail.Close;
  if CheckBox1.Checked Then
    begin
      qryMaster.CachedUpdates := True;
      qryMaster.UpdateObject := updMaster;
      qryDetail.CachedUpdates := True;
      qryDetail.UpdateObject := updDetail;
    end
  else
    begin
      qryMaster.CachedUpdates := False;
      qryMaster.UpdateObject := Nil;
      qryDetail.CachedUpdates := False;
      qryDetail.UpdateObject := Nil;
    end;
  qryMaster.Open;
  qryDetail.Open;
end;

procedure TForm1.qryDetailFilterRecord(DataSet: TDataSet;
  var Accept: Boolean);
begin
  Accept := qryDetail.FieldByName( 'MASTER_ID' ).AsString =
            qryMaster.FieldByName( 'ID' ).AsString;
end;

procedure TForm1.qryDetailNewRecord(DataSet: TDataSet);
begin
  qryDetail.FieldByName( 'MASTER_ID' ).AsString :=
    qryMaster.FieldByName( 'ID' ).AsString;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  dbFilters.ApplyUpdates([]);
end;

end.


