home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / sdkfilt.pak / MAIN.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1997-07-24  |  4.9 KB  |  170 lines

  1. {$A+,B-,D+,F-,G+,I+,K+,L+,N+,P+,Q+,R+,S+,T-,V+,W-,X+,Y+}
  2. {$M 25600,4096}
  3.  
  4. { Description:
  5.     This example shows how to use filters to limit the result
  6.     set of a table. Filters perform a similar function to
  7.     ranges, but more powerful operations are supported.
  8. }
  9. unit Main;
  10.  
  11. interface
  12.  
  13. uses
  14.   SysUtils, Forms, Dialogs, DbiTypes, DbiProcs, DbiErrs, DBGrids, DB,
  15.   DBTables, DBCtrls, Filter, Controls, StdCtrls, Grids, ExtCtrls, Classes;
  16.  
  17. type
  18.   TMainForm = class(TForm)
  19.     BiolifeTable: TTable;
  20.     DataSource1: TDataSource;
  21.     DBGrid1: TDBGrid;
  22.     DBImage1: TDBImage;
  23.     DBMemo1: TDBMemo;
  24.     Panel1: TPanel;
  25.     BeginLbl: TLabel;
  26.     EndLbl: TLabel;
  27.     AddFilterBtn: TButton;
  28.     DropFilterBtn: TButton;
  29.     OpenDialog1: TOpenDialog;
  30.     procedure AddFilterBtnClick(Sender: TObject);
  31.     procedure DropFilterBtnClick(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.   end;
  34.  
  35. var
  36.   MainForm: TMainForm;
  37.  
  38. implementation
  39.  
  40. {$R *.DFM}
  41.  
  42. { METHOD:  AddFilterBtnClick
  43.   PURPOSE: Creates and add a filter to the BIOLIFE.DB Table.
  44. }
  45. procedure TMainForm.AddFilterBtnClick(Sender: TObject);
  46. var
  47.   BeginValue, EndValue: string[5]; { Beginning and ending filter values; temp}
  48.   Values: tValues; { Beginning and ending filter values }
  49.   Code: Integer;
  50.  
  51. begin
  52.   BeginValue := '';
  53.   EndValue := '';
  54.   { Get the two values for the filter expression }
  55.   if InputQuery('Add Filter to Field "Species No"',
  56.                 'Enter a greater than or equal to value',
  57.                 BeginValue) = True then
  58.   begin
  59.     val(BeginValue, Values[1], Code);
  60.     if InputQuery('Add Filter to "Species No"',
  61.                   'Enter a less than or equal to value',
  62.                   EndValue) = True then
  63.     begin
  64.       val(EndValue, Values[2], Code);
  65.       try
  66.         { Place the filter onto the table }
  67.         AddFilter(Values, BiolifeTable.Handle);
  68.         { Update delphi controls to show the filtered table }
  69.         BiolifeTable.Refresh;
  70.         { Place the cursor on the first record of the table }
  71.         BiolifeTable.First;
  72.       except
  73.         on Error:EDatabaseError do
  74.         begin
  75.           MessageDlg(Error.Message, mtError, [mbOK], 0);
  76.           Exit;
  77.         end;
  78.       end;
  79.       { Show the filter range }
  80.       BeginLbl.Caption := Format('Begin Filter: %s', [BeginValue]);
  81.       EndLbl.Caption := Format('End Filter: %s', [EndValue]);
  82.       { Switch the button enabling }
  83.       DropFilterBtn.Enabled := True;
  84.       AddFilterBtn.Enabled := False;
  85.       { Set focus to the table grid }
  86.       DBGrid1.SetFocus;
  87.     end;
  88.   end;
  89. end;
  90.  
  91. { METHOD:  DropFilterBtnClick
  92.   PURPOSE: Deactivates and drops the filter.
  93. }
  94. procedure TMainForm.DropFilterBtnClick(Sender: TObject);
  95. begin
  96.   try
  97.     { Deactivate and drop the filter }
  98.     RemoveFilter(BiolifeTable.Handle, hFilter);
  99.   except
  100.     on Error:EFilterError do
  101.     begin
  102.       MessageDlg(Error.Message, mtError, [mbOK], 0);
  103.       Exit;
  104.     end;
  105.   end;
  106.   { Update delphi controls to show the filtered table }
  107.   BiolifeTable.Refresh;
  108.   { Place the cursor on the first record of the table }
  109.   BiolifeTable.First;
  110.   { Show the filter range }
  111.   BeginLbl.Caption := 'Begin Filter: None';
  112.   EndLbl.Caption := 'End Filter: None';
  113.   { Switch the button enabling }
  114.   DropFilterBtn.Enabled := False;
  115.   AddFilterBtn.Enabled := True;
  116.   { Set focus to the table grid }
  117.   DBGrid1.SetFocus;
  118. end;
  119.  
  120. procedure TMainForm.FormCreate(Sender: TObject);
  121. begin
  122.   try
  123.     BiolifeTable.Open;
  124.   except
  125.     { If for some reason, the application could not open the table }
  126.     on E:EDatabaseError do
  127.     begin
  128.       MessageDlg('Filter example error: ' + E.Message +
  129.                  '.  Delphi must be installed and have an alias called' +
  130.                  ' "DBDEMOS" which points to the delphi\demos\data ' +
  131.                  'directory.', mtError, [mbOk], 0);
  132.       if MessageDlg('Would you like to browse for the ' +
  133.                  'delphi\demos\data\biolife files?', mtConfirmation,
  134.                  [mbYes, mbNo], 0) = mrYes then
  135.       begin
  136.         if OpenDialog1.Execute then
  137.         begin
  138.           try
  139.             BiolifeTable.DatabaseName := '';
  140.             BiolifeTable.TableName := OpenDialog1.FileName;
  141.             BiolifeTable.Open;
  142.           except
  143.             on E:EDatabaseError do
  144.             begin
  145.               MessageDlg(E.Message + '.  Application Terminating', mtError,
  146.                          [mbOk], 0);
  147.               Application.Terminate;
  148.             end;
  149.           end;
  150.         end
  151.         else
  152.         begin
  153.           MessageDlg('Application terminating', mtError, [mbOK], 0);
  154.           Application.Terminate;
  155.         end;
  156.       end
  157.       else
  158.         Application.Terminate;
  159.     end;
  160.     else
  161.     begin
  162.       MessageDlg('Filter example error: ' + Exception(ExceptObject).Message +
  163.                  '.  Aplication terminating.', mtError, [mbOk], 0);
  164.       Application.Terminate;
  165.     end;
  166.   end;
  167. end;
  168.  
  169. end.
  170.