如果form上有4个复选框,根据每个复选框的状态需要运行不同的SQL程序,这样就需要16套SQL程序,有没有什么简单的方法??(我用的是Tquery)

解决方案 »

  1.   

    sqlStr := '';
    if checkBox1.Checked then
      sqlStr := sqlStr + ' and Age between 10 and 20';
    if checkBox2.Checked then
      sqlStr := sqlStr + ' and Sex = ' + QuotedStr('男');
    if...
    if...if sqlStr = '' then 
      ADOQuery1.SQL.Text := 'select * from Table1';
    else begin
      n := Pos('and', sqlStr);
      ADOQuery1.SQL.Text := 'select from Table1 where ' + Copy(sqlStr, n+3,200);
    end;
      

  2.   


    with ADOQuery1 do 
     begin
     close;
     sql.clear;
     sql.add('select * from YourTableName where ID is not null ');
     if CheckBox1.Checked then 
       sql.add('and .....');
     if CheckBox2.Checked then 
       sql.add('and .....');
     ....
     open;
     end;