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

DBGrid Filter using a keyword

Status
Not open for further replies.

CarrahaG

Programmer
Mar 25, 2007
98
0
0
AW
Hello,

Is there a way to do a filter in the DBGrid component using a part of a field or fields? For example, the following code only finds one record in which the contents of the field "DESC_LIN_1" matches the exactly what is typed in the TEdit Component.

procedure TfrmCPItemFile.edtKeywordKeyPress(Sender: TObject; var Key: Char);
var
myFilterString : string;

begin
if ord(Key) <> 8 then
myFilterEntry := myFilterEntry + StringOfChar(Key, 1)
else
myFilterEntry := AnsiMidStr(myFilterEntry, 1, length(myFilterEntry)-1);

myFilterString := 'DESC_LIN_1 = ' + CHR(39) + myFilterEntry + CHR(39);
DataModuleUnit.ADOtblITEM.Filter := myFilterString;
DataModuleUnit.ADOtblITEM.Filtered := true;
end;


What we would like to accomplish is the DBGrid component listing all records that meet a partial match between the contents of the TEdit and the contents of the "DESC_LIN_1" and "DESC_LIN_2" fields. In addition, we do not want case sensitivity to play a factor in the filtering.

Therefore, if "Helm" is typed in the TEdit component then we would like it to display the records in which "DESC_LIN_1" field has "Red Helmet" or "DESC_LIN_2" has "HELMET".

Regards,
Georges
 
you can use the filter & filtered property of the table...

on the OnChange Event of the TEdit add the following:

begin
with table1 do
begin
filter := 'i_name = '+quotedstr(edit1.Text+'*');
filtered := true;
end;
///this is applicable even your table is unindexed
// you can also you the setrange method
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top