Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TLocate problem

Status
Not open for further replies.

MLNorton

Programmer
Nov 15, 2009
134
US
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?
 
This is just a guess...
But it looks like you are using an ADOTable rather than an ADOQuery. Doing the search with the table is probably just hitting the first 'Smith' it hits in the table (The TADOTable is handling the sort vs and TADOQuery returning an ordered result set). If you use an ADOQuery, add the ORDER BY lastname, firstname to the end of the SQL. This may solve the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top