home *** CD-ROM | disk | FTP | other *** search
- {$A+,B-,D+,F-,G+,I+,K+,L+,N+,P+,Q+,R+,S+,T-,V+,W-,X+,Y+}
- {$M 25600,4096}
-
- { Description:
- This example shows how to use filters to limit the result
- set of a table. Filters perform a similar function to
- ranges, but more powerful operations are supported.
- }
- unit Main;
-
- interface
-
- uses
- SysUtils, Forms, Dialogs, DbiTypes, DbiProcs, DbiErrs, DBGrids, DB,
- DBTables, DBCtrls, Filter, Controls, StdCtrls, Grids, ExtCtrls, Classes;
-
- type
- TMainForm = class(TForm)
- BiolifeTable: TTable;
- DataSource1: TDataSource;
- DBGrid1: TDBGrid;
- DBImage1: TDBImage;
- DBMemo1: TDBMemo;
- Panel1: TPanel;
- BeginLbl: TLabel;
- EndLbl: TLabel;
- AddFilterBtn: TButton;
- DropFilterBtn: TButton;
- OpenDialog1: TOpenDialog;
- procedure AddFilterBtnClick(Sender: TObject);
- procedure DropFilterBtnClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- end;
-
- var
- MainForm: TMainForm;
-
- implementation
-
- {$R *.DFM}
-
- { METHOD: AddFilterBtnClick
- PURPOSE: Creates and add a filter to the BIOLIFE.DB Table.
- }
- procedure TMainForm.AddFilterBtnClick(Sender: TObject);
- var
- BeginValue, EndValue: string[5]; { Beginning and ending filter values; temp}
- Values: tValues; { Beginning and ending filter values }
- Code: Integer;
-
- begin
- BeginValue := '';
- EndValue := '';
- { Get the two values for the filter expression }
- if InputQuery('Add Filter to Field "Species No"',
- 'Enter a greater than or equal to value',
- BeginValue) = True then
- begin
- val(BeginValue, Values[1], Code);
- if InputQuery('Add Filter to "Species No"',
- 'Enter a less than or equal to value',
- EndValue) = True then
- begin
- val(EndValue, Values[2], Code);
- try
- { Place the filter onto the table }
- AddFilter(Values, BiolifeTable.Handle);
- { Update delphi controls to show the filtered table }
- BiolifeTable.Refresh;
- { Place the cursor on the first record of the table }
- BiolifeTable.First;
- except
- on Error:EDatabaseError do
- begin
- MessageDlg(Error.Message, mtError, [mbOK], 0);
- Exit;
- end;
- end;
- { Show the filter range }
- BeginLbl.Caption := Format('Begin Filter: %s', [BeginValue]);
- EndLbl.Caption := Format('End Filter: %s', [EndValue]);
- { Switch the button enabling }
- DropFilterBtn.Enabled := True;
- AddFilterBtn.Enabled := False;
- { Set focus to the table grid }
- DBGrid1.SetFocus;
- end;
- end;
- end;
-
- { METHOD: DropFilterBtnClick
- PURPOSE: Deactivates and drops the filter.
- }
- procedure TMainForm.DropFilterBtnClick(Sender: TObject);
- begin
- try
- { Deactivate and drop the filter }
- RemoveFilter(BiolifeTable.Handle, hFilter);
- except
- on Error:EFilterError do
- begin
- MessageDlg(Error.Message, mtError, [mbOK], 0);
- Exit;
- end;
- end;
- { Update delphi controls to show the filtered table }
- BiolifeTable.Refresh;
- { Place the cursor on the first record of the table }
- BiolifeTable.First;
- { Show the filter range }
- BeginLbl.Caption := 'Begin Filter: None';
- EndLbl.Caption := 'End Filter: None';
- { Switch the button enabling }
- DropFilterBtn.Enabled := False;
- AddFilterBtn.Enabled := True;
- { Set focus to the table grid }
- DBGrid1.SetFocus;
- end;
-
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- try
- BiolifeTable.Open;
- except
- { If for some reason, the application could not open the table }
- on E:EDatabaseError do
- begin
- MessageDlg('Filter example error: ' + E.Message +
- '. Delphi must be installed and have an alias called' +
- ' "DBDEMOS" which points to the delphi\demos\data ' +
- 'directory.', mtError, [mbOk], 0);
- if MessageDlg('Would you like to browse for the ' +
- 'delphi\demos\data\biolife files?', mtConfirmation,
- [mbYes, mbNo], 0) = mrYes then
- begin
- if OpenDialog1.Execute then
- begin
- try
- BiolifeTable.DatabaseName := '';
- BiolifeTable.TableName := OpenDialog1.FileName;
- BiolifeTable.Open;
- except
- on E:EDatabaseError do
- begin
- MessageDlg(E.Message + '. Application Terminating', mtError,
- [mbOk], 0);
- Application.Terminate;
- end;
- end;
- end
- else
- begin
- MessageDlg('Application terminating', mtError, [mbOK], 0);
- Application.Terminate;
- end;
- end
- else
- Application.Terminate;
- end;
- else
- begin
- MessageDlg('Filter example error: ' + Exception(ExceptObject).Message +
- '. Aplication terminating.', mtError, [mbOk], 0);
- Application.Terminate;
- end;
- end;
- end;
-
- end.
-