Board index » delphi » TTable Locate method - options

TTable Locate method - options

Could anyone be so kind as to shed some light on an intriguing ( well to me
anyhow!) little problem regarding the locate method. I am using this on a
primary indexed field with both caseinsensitive and partial key options
defined. Really it is a question of extending the partial key option so that
when text is typed in to an edit control for searching, instead of locating
on the basis of partial text from the beginning, the location can also be
done if the key field contains the letters typed into the edit control.
  I've toyed with the possibility of creating perhaps for example a
TStringlist object, then attempting to search for contained text but it
seems like using a sledgehammer to crack a nut. Am I perhaps missing a
fundamental way to do this - I am very new to Delphi and database
programming, very much teaching myself. So if anyone could offer help, I'd
appreciate it - hopefully one day the favour could be returned.

Pete Barrett

 

Re:TTable Locate method - options


In article <81pk6b$q...@forums.borland.com>, peter-barr...@talk21.com

Quote
(Peter Barrett) wrote:

---8<---
Quote
> when text is typed in to an edit control for searching, instead of
> locating
> on the basis of partial text from the beginning, the location can also
> be
> done if the key field contains the letters typed into the edit control.

---8<---

Hi Peter,

You need to use SQL to achive this. Here are the steps for a very simple
demo of this ...

1. Drop a TEdit, TQuery, TDataSource and a TDBGrid on a form.
2. Wire the TDBGrid to the TDatasource and the TDatasource to the TQuery.
3. Set the DatabaseName of the TQuery to DBDEMOS
4. In the OnChange event of the TEdit, write the following code ...

procedure TForm1.Edit1Change(Sender: TObject);
begin
  Query1.Close;
  Query1.SQL.Clear;
  Query1.SQL.Add('select * from Biolife.db');
  Query1.SQL.Add('where (Common_Name like "%' + Edit1.Text + '%")');
  Query1.Open;
end;

5. Save and run the app.

When you type in the Edit control, the query will be rerun for each
keystroke with the SQL necessary to do a partial match anywhere in the
Common_Name column.

Have a look in the help file LOCALSQL.HLP (should be in your BDE folder)
under "LIKE predicate" for an explanation of the SQL thery behind this
demo.

--
Carl

Re:TTable Locate method - options


Oi Peter,

Using SQL in a Query would be best suited for what you want, I guess. For
example :

Select * from yourtable
where yourfield like :parameter

In your code :

Query.Close;
Query.Params[0].value := '%' + Edit.Text+'%';
Query.Open;

// % stands for 'everything'
// Edit.Text contains your typped letter

elio

Other Threads