5

Dear experts, I’m trying to filter the result in dbgrid connected to adoquery, depending on user selection of 4 checkboxes, user can select one or more fileds to filter the data accordingly I have this code, and I don’t know how to pass "and/ or not pass" it if user select two or more checkboxes.

Vw_Activity.SQL.Text:='select * from Vw_Activity where ';
if CBEmployee.Checked then
begin
Vw_Activity.SQL.Add('Emp_Name_Ar=:x');
Vw_Activity.Parameters.ParamByName('x').Value:=emp Name.Text;
end;


if CBTask.Checked then
begin
Vw_Activity.SQL.Add('Category_Name=:y');
Vw_Activity.Parameters.ParamByName('y').Value:=Pro blemCat.Text;
end;

if CBIncharge.Checked then
begin
Vw_Activity.SQL.Add('Support_name_En=:h');
Vw_Activity.Parameters.ParamByName('h').Value:=Sup portstaff.Text;
end;


if CBstatus.Checked then
begin
Vw_Activity.SQL.Add('Request_Status=:k');
Vw_Activity.Parameters.ParamByName('k').Value:=Req uestStatus.Text;
end;

Vw_Activity.Active:=true;

waitting your help

1 Answer 1

5

you can rewrite your sql sentence to (check the final 1=1)

select * from Vw_Activity where 1=1

and then add each condition like this

Vw_Activity.SQL.Text:='select * from Vw_Activity where 1=1 ';
if CBEmployee.Checked then
begin
  Vw_Activity.SQL.Add('AND Emp_Name_Ar=:x');
  Vw_Activity.Parameters.ParamByName('x').Value:=emp Name.Text;
end;


if CBTask.Checked then
begin
  Vw_Activity.SQL.Add('AND Category_Name=:y');
  Vw_Activity.Parameters.ParamByName('y').Value:=Pro blemCat.Text;
end;

if CBIncharge.Checked then
begin
  Vw_Activity.SQL.Add('AND Support_name_En=:h');
  Vw_Activity.Parameters.ParamByName('h').Value:=Sup portstaff.Text;
end;


if CBstatus.Checked then
begin
  Vw_Activity.SQL.Add('AND Request_Status=:k');
  Vw_Activity.Parameters.ParamByName('k').Value:=Req uestStatus.Text;
end;

Vw_Activity.Active:=true;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.