home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1996 August / VPR9608A.BIN / del20try / install / data.z / FINDCUST.PAS < prev    next >
Pascal/Delphi Source File  |  1996-05-08  |  3KB  |  99 lines

  1. unit FindCust;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Grids, DBGrids, StdCtrls, Buttons, Mask, ExtCtrls;
  8.  
  9. type
  10.   TfmFindCust = class(TForm)
  11.     DBGrid1: TDBGrid;
  12.     ComboBox1: TComboBox;
  13.     Edit1: TEdit;
  14.     Label1: TLabel;
  15.     Label2: TLabel;
  16.     Bevel1: TBevel;
  17.     cbUseFilter: TCheckBox;
  18.     Button1: TButton;
  19.     Button2: TButton;
  20.     procedure ComboBox1Change(Sender: TObject);
  21.     procedure Edit1Change(Sender: TObject);
  22.   end;
  23.  
  24. var
  25.   fmFindCust: TfmFindCust;
  26.  
  27. implementation
  28.  
  29. uses DM;
  30.  
  31. {$R *.DFM}
  32.  
  33. procedure TfmFindCust.ComboBox1Change(Sender: TObject);
  34. begin
  35.   { Make the field we're locating in the leftmost field. }
  36.   DM2.CustLookup.FieldByName(ComboBox1.Text).Index := 0;
  37.  
  38.   { In order to use FindNearest, we have to be indexed based on the field
  39.     we're doing the find in.  If we're using Locate, it doesn't matter what
  40.     the current index is, so we will set it to company, which is the default
  41.     in the Customer form. }
  42.   if ComboBox1.Text = 'CustNo' then
  43.     DM2.CustLookup.IndexName := ''
  44.   else
  45.     DM2.CustLookup.IndexName := 'ByCompany';
  46.  
  47.   { Let the user choose whether or not to use a filter if they're going to be
  48.     searching based on a field that has no index. }
  49.   cbUseFilter.Enabled := Pos('|'+ComboBox1.Text+'|','|CustNo|Company|') = 0;
  50.   if cbUseFilter.Enabled then
  51.     cbUseFilter.SetFocus
  52.   else
  53.     Edit1.SetFocus;
  54.   Edit1.Text := '';
  55. end;
  56.  
  57. procedure TfmFindCust.Edit1Change(Sender: TObject);
  58. var
  59.   b: Boolean;
  60. begin
  61.   { Now, we begin to find the field, as the user types, i.e.,
  62.     Incrementally.  FindNearest is used in the two indexed fields,
  63.     Company and CustNo; Locate is used for all the non-indexed
  64.     fields.  Actually, Locate could be used in all cases (instead
  65.     of FindNearest). because Delphi is "smart" enough to use an
  66.     index if one exists. }
  67.   with DM2.CustLookup do
  68.   begin
  69.     if ComboBox1.Text = 'Company' then
  70.       findnearest([Edit1.Text])
  71.     else if ComboBox1.Text = 'CustNo' then
  72.       try
  73.         if Edit1.text <> '' then
  74.           FindNearest([StrToFloat(Edit1.text)]);
  75.       except
  76.         raise Exception.create(Edit1.text+' is not a valid customer number');
  77.       end
  78.     else {Some non-indexed field.}
  79.     begin
  80.       { Possibly apply the filter so we see only records matching this value. }
  81.       b := (Locate(ComboBox1.Text, Edit1.Text,[loCaseInsensitive,
  82.         loPartialKey])) and (cbUseFilter.Checked);
  83.       if b then
  84.       begin
  85.         { Get an expression such as State = 'CA'. }
  86.         Filter := ComboBox1.Text + ' = ' + '''' + Edit1.Text + '''';
  87.         Filtered := True;
  88.         if RecordCount = 0 then {Filter is possibly too restrictive}
  89.           Filter := ComboBox1.Text + ' >= ' + '''' + Edit1.Text + '''';
  90.       end
  91.       else
  92.         Filtered := False;
  93.     end;
  94.     Refresh;
  95.   end;
  96. end;
  97.  
  98. end.
  99.