home *** CD-ROM | disk | FTP | other *** search
- unit FindCust;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Grids, DBGrids, StdCtrls, Buttons, Mask, ExtCtrls;
-
- type
- TfmFindCust = class(TForm)
- DBGrid1: TDBGrid;
- ComboBox1: TComboBox;
- Edit1: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- Bevel1: TBevel;
- cbUseFilter: TCheckBox;
- Button1: TButton;
- Button2: TButton;
- procedure ComboBox1Change(Sender: TObject);
- procedure Edit1Change(Sender: TObject);
- end;
-
- var
- fmFindCust: TfmFindCust;
-
- implementation
-
- uses DM;
-
- {$R *.DFM}
-
- procedure TfmFindCust.ComboBox1Change(Sender: TObject);
- begin
- { Make the field we're locating in the leftmost field. }
- DM2.CustLookup.FieldByName(ComboBox1.Text).Index := 0;
-
- { In order to use FindNearest, we have to be indexed based on the field
- we're doing the find in. If we're using Locate, it doesn't matter what
- the current index is, so we will set it to company, which is the default
- in the Customer form. }
- if ComboBox1.Text = 'CustNo' then
- DM2.CustLookup.IndexName := ''
- else
- DM2.CustLookup.IndexName := 'ByCompany';
-
- { Let the user choose whether or not to use a filter if they're going to be
- searching based on a field that has no index. }
- cbUseFilter.Enabled := Pos('|'+ComboBox1.Text+'|','|CustNo|Company|') = 0;
- if cbUseFilter.Enabled then
- cbUseFilter.SetFocus
- else
- Edit1.SetFocus;
- Edit1.Text := '';
- end;
-
- procedure TfmFindCust.Edit1Change(Sender: TObject);
- var
- b: Boolean;
- begin
- { Now, we begin to find the field, as the user types, i.e.,
- Incrementally. FindNearest is used in the two indexed fields,
- Company and CustNo; Locate is used for all the non-indexed
- fields. Actually, Locate could be used in all cases (instead
- of FindNearest). because Delphi is "smart" enough to use an
- index if one exists. }
- with DM2.CustLookup do
- begin
- if ComboBox1.Text = 'Company' then
- findnearest([Edit1.Text])
- else if ComboBox1.Text = 'CustNo' then
- try
- if Edit1.text <> '' then
- FindNearest([StrToFloat(Edit1.text)]);
- except
- raise Exception.create(Edit1.text+' is not a valid customer number');
- end
- else {Some non-indexed field.}
- begin
- { Possibly apply the filter so we see only records matching this value. }
- b := (Locate(ComboBox1.Text, Edit1.Text,[loCaseInsensitive,
- loPartialKey])) and (cbUseFilter.Checked);
- if b then
- begin
- { Get an expression such as State = 'CA'. }
- Filter := ComboBox1.Text + ' = ' + '''' + Edit1.Text + '''';
- Filtered := True;
- if RecordCount = 0 then {Filter is possibly too restrictive}
- Filter := ComboBox1.Text + ' >= ' + '''' + Edit1.Text + '''';
- end
- else
- Filtered := False;
- end;
- Refresh;
- end;
- end;
-
- end.