I have an Access database of 3,200 records of names and addresses. The first field is LastName, I have a DBGrid that displays this data. The records are sorted in LastName,FirstName order. I have an Edit_Search box that calls the following code from an OnChange event:
procedure TForm_Member.Button1Click(Sender: TObject);
var
FieldVal: string;
FieldName: string;
Lo: TLocateOptions;
begin
With Form_Main do
begin
Lo := [];
Include(Lo,loPartialKey);
Include(Lo,loCaseInsensitive);
With Form_Main.ADOTable1 do
begin
FieldName := 'LastName';
FieldVal := Edit_Search.Text;
If ((Locate(FieldName, FieldVal,Lo)) = false) and
(length(Edit_Search.Text) > 0)
then
begin
MessageDlg('The requested record is not in the dataset.',mtWarning,
[mbOK], 0);
Edit_Search.Text := '';
end;
end;
end;
end;
The search locates the record following the desired record in most cases. If there are multiple records with the same name, such as ‘Smith’, the selected record is several beyond the first ‘Smith’ record. If I enter ‘Smith’, the 15th of 28 ‘Smiths’ is selected, not the first ‘Smith’.
When I select ‘White’ and type each letter, Locates works properly and locates “white’ prior to entering the letter ‘t’. When I enter ‘t’ the next record is selected.
What is my problem and what is the solution?
procedure TForm_Member.Button1Click(Sender: TObject);
var
FieldVal: string;
FieldName: string;
Lo: TLocateOptions;
begin
With Form_Main do
begin
Lo := [];
Include(Lo,loPartialKey);
Include(Lo,loCaseInsensitive);
With Form_Main.ADOTable1 do
begin
FieldName := 'LastName';
FieldVal := Edit_Search.Text;
If ((Locate(FieldName, FieldVal,Lo)) = false) and
(length(Edit_Search.Text) > 0)
then
begin
MessageDlg('The requested record is not in the dataset.',mtWarning,
[mbOK], 0);
Edit_Search.Text := '';
end;
end;
end;
end;
The search locates the record following the desired record in most cases. If there are multiple records with the same name, such as ‘Smith’, the selected record is several beyond the first ‘Smith’ record. If I enter ‘Smith’, the 15th of 28 ‘Smiths’ is selected, not the first ‘Smith’.
When I select ‘White’ and type each letter, Locates works properly and locates “white’ prior to entering the letter ‘t’. When I enter ‘t’ the next record is selected.
What is my problem and what is the solution?