settin up a query with an array of cases enabled by checkboxes

i want to set up a form with several possible fields to query, behind every field theres a checkbox to enable this part of the query, there r 5 fields each with a checkbox, i should be possible to anable one of the five as well as all 5 checkboxes so thre query is build with all the strings to look for in the specific dbfields, i was able to set up the plane query and to add the query code for one or two of the search fields but when it comes to the 3rd and more fields (checked) then the trouble starts, i think my code must be full of mistakes (wrong usage of if...then, begin, end...), thanks for helping
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should write a base querey like this:
Select *
From xxx, yyy, qqq
and then add a line to you're query.sql per each checkbox that is checked like this:
if chb1.checked then
  query1.sql.Add('aaa=fff and')
unit temp;


  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Db, DBTables;

  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button1: TButton;
    Query1: TQuery;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    { Public declarations }
    Function BuildWhereClause : String;

  Form1: TForm1;


{$R *.DFM}

Function TForm1.BuildWhereClause : String;
  TempString : String;

// say you have 5 edit boxes, edit1,2,3,4,5 where edit1 is an integer
// and the other 4 are strings then:

  TempString := '';
  //add the "where" to the clause if there at least on box is checked
  if (CheckBox1.Checked = True) or (CheckBox2.Checked = True) or
     (CheckBox2.Checked = True) or (CheckBox3.Checked = True) or
     (CheckBox5.Checked = True) then TempString := TempString + ' where ';

  // build the and part of the clause
  if CheckBox1.Checked = True then
    TempString := TempString + ' FieldName = ' + Edit1.Text + ' and ';
  if CheckBox2.Checked = True then
    TempString := TempString + ' FieldName = ''' + Edit2.Text + ''' and ';
  if CheckBox3.Checked = True then
    TempString := TempString + ' FieldName = ''' + Edit3.Text + ''' and ';
  if CheckBox4.Checked = True then
    TempString := TempString + ' FieldName = ''' + Edit4.Text + ''' and ';
  if CheckBox5.Checked = True then
    TempString := TempString + ' FieldName = ''' + Edit5.Text + ''' and ';

    // Delete the last "and" at the end of tempString
  TempString := Copy(TempString,1,Length(TempString)-4);
  result := TempString;

// run Query
procedure TForm1.Button1Click(Sender: TObject);
  SelectClause,WhereClause,EndClause : String;
  QueryString : String;
// Split the query into 3 parts, a select clause, a where cluase and a end clause
  SelectClause := ' Select * from TableName ';
  WhereClause := BuildWhereClause;
  EndClause := ' order by Name ';

// add the three parts to form one
  QueryString := SelectClause + WhereClause + EndClause;

  // clear your query, add your string and run the query !



// Rob ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.