DelphiFAQ Home Search:

Allow Incremental Searching in your TDataSets

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

If you implement your search box (against a TDataSet) with a regular Locate() or FindKey() call requires that the user has typed in the exact search expression.
It is much more handy to jump to the first match based on a partial search.
E.g. if your user searches for the month 'August' in the list of 12 months' names, the user would type in 'A' and the cursor would jump to 'April'. Then the user would type the second letter.. 'u' and 'August' would be selected.

This is called 'Incremental Searching'.

The following piece of code shows how to do it - put it as the onChange event handler of your form's edit box.

procedure TForm1.Edit1Change(Sender: TObject);
begin
  // empty? then do nothing!
  if Edit1.Text='' then
    exit;
	
  //goto nearest match
  with Table1
  do
  begin
    SetKey;
    FieldByName('Month').AsString := Edit1.Text;
    GotoNearest;
  end;
end;

Comments:

2007-04-06, 15:55:40
anonymous from New Zealand  
There's a much easier way to do this that can be used with engines other than the BDE.

Use the filter property of the dataset and insert a SQL 'LIKE' statement, then apply the filter.

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: