Imagine you have a big table, you need to give the end user search capability and network trafic is a problem.
In this case we can use an TComboBox to determine which column to search and TEditBox for the search criteria.
table structure:
| CODE | NAME | LOCATION | GENDER |
Needed
1) a form
2) TQuery named MyQuery
3) TDBGrid or other db aware fields, TDataSource, DataModule etc.
4) TEditBox named edtSelect
5) TComboBox named cbSelect
6) TBitButton named bbtnSelect (to fire the query)
7) Some global variables
SString --> string
RecordsRetrieved --> integer
8) A statusbar (optional)
----------------------------------------------
The Query is a parametrized type.
Properties:
SQL = Select CODE, NAME, LOCATION, TYPE from
MYTABLE MYTABLE
where CODE like :SearchString
Active = false
Parameters --> SearchString --> ftString
As mentioned before, cbSelect will indicate the column searched.
In the sql statement the column is located after 'where'
procedure TForm1.bbtnSelectClick(Sender: TObject);
begin
inherited;
with Myquery do
begin
close;
SQL.Clear;
SQL.Add('SELECT CODE, NAME, LOCATION, TYPE');
SQL.Add('FROM MYTABLE MYTABLE');
case cbSelect.ItemIndex of
0: begin
SQL.Add('WHERE CODE LIKE :SearchString');
ParamByName('SearchString').AsString := SString;
end;
1: begin
SQL.Add('WHERE NAME LIKE :SearchString');
ParamByName('SearchString').AsString := SString;
end;
2: begin
SQL.Add('WHERE LOCATION LIKE :SearchString');
ParamByName('SearchString').AsString := SString;
end;
3: begin
SString := 'M';
edtSelect.Text:= 'M';
SQL.Add('WHERE GENDER = "M"');
end;
4: begin
SString := 'F';
edtSelect.Text:= 'F';
SQL.Add('WHERE GENDER = "F"');
end;
End; {case}
end;
------------------------------------------- Remarks
The user do not have to use wildcards to find for example edward, any character will satisfy the search. Leaving the edtSelect blank, will retrieve all the records.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.