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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I quickly find data in a dbGrid?

Database Programming

How do I quickly find data in a dbGrid?

by  svanels  Posted    (Edited  )
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


Code:
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top