INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Database Programming

How do I quickly find data in a dbGrid? by svanels
Posted: 9 Dec 03

If you are using tables and a RDBMS like interbase there is a work around.

Imagine you need to edit a table and need fast search capability. This example assumes that you have Local Interbase installed.

Drop a TDatabase, a table, dbGrid, datasource, a label and an editbox on a form

Set the databaseName to dbxx and Aliasname to IBLOCAL

Set the table properties:

DatabaseName = dbxx
Name = Customer
IndexFieldNames = cust_no;country (sepeperate the field names by ;)

Connect the datasource to the table, and the dbgrid to the datasource

call the editbox edtSearch and the label lblsearch

The events that needed are: OnTitleClick for the dbgrid, formcreate and onchange for the editbox

This way clicking on the title and using the editbox, the cursor will jump to the nearest match in the grid.
the code is listed below



procedure TForm1.FormCreate(Sender: TObject);
begin
  table1.Open;
  lblSearch.Caption := 'search on CustNo';
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  table1.IndexFieldNames := column.FieldName;
  lblsearch.Caption := ' search on ' + column.Title.Caption;
end;

procedure TForm1.edtSearchChange(Sender: TObject);
begin
  if (edtSearch.Text <> '') then
    try table1.FindNearest([edtSearch.text]);
    except
      on EDatabaseError do;
      on EConvertError do;
    end;
end;


This works very well with interbase on all fields and I think with other RDBMS (sql-server etc.) For desktop databases (access, paradox) it will only work if indexes are placed on the table.
Clicking on a non-indexed field will give an error message that there is no index for the field.

Queries unfortunately don't have the IndexFieldName property

Regards

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close