var
  str:array [0..7] of string;
  sSql:string; 
  i:integer;
begin
  for i:=0 to 7 do 
   str[i]:='';
  sSql:='select * from table where 1=1 '
  if edit1.text <>'' then
    str[0]:= ' and field1 link '''+edit1.text +'%''';
  if edit2.text<>'' then
    str[1]:=' and filed2 ='''+edit2.text+'''';
  if cbTime.check then
    str[2]:=' and filed3 between '''+ datetostr(tpk1.date)+'' and ' +datetostr(tpk2.date);  .....  for i:=0 to 7 do
   ssql:=sql+str[i];end;
有什么搞不掂再讨论

解决方案 »

  1.   

    动态生成SQL语句,在传给TADOQUERY 就可以了
      

  2.   

    你可以用一个ListBox来显示表的所有的字段,再用一个ListBox来显示查找条件、一个Eidt来输入值。在选择字段和条件的时候动态生成Sql,再重新执行查询就可以了。
      

  3.   

    同意楼上。最好根据sql的写法使用控件。例如要有:查询字段(dblookupbox),数据源(dblookupbox),运算符(combobox),取值(dblookupbox或edit),当然,复杂一点还可以提供括号(checkbox)
      

  4.   

    newyj(老鬼) 的方法只有 and 的情况,而没有 or 的情况logapi(logapi) 
    yingwei(樱薇) 
    cobi(小新国际) 三位说得太粗略,不知能否再详尽一些,注意看一下Tquery组件的sql builder里类似功能,那是非常灵活的,希望有哪位高手能写出通用代码让大家共享,敝下在此先行谢过!!!
      

  5.   

      {象我举的例子里 动态生成8个条件
      如果都是and 'select * from .. where 1=1'
      如果都是or  'select * from .. where 0=1'
      连接子句时比较方便
       sSql:='select * from table  '
      if edit1.text <>'' then
        if cb1.check then
          str[0]:= ' and field1 link '''+edit1.text +'%''';
        else
          str[0]:= ' or field1 link '''+edit1.text +'%''';
      if edit2.text<>'' then
        if cb2.check then
          str[1]:=' and filed2 ='''+edit2.text+'''';
         else
          str[1]:=' or filed2 ='''+edit2.text+'''';
      if cbTime.check then
        if cb3.check then
          str[2]:=' and filed3 between '''+ datetostr(tpk1.date)+'' and ' +datetostr(tpk2.date);
        else
          str[2]:=' or filed3 between '''+ datetostr(tpk1.date)+'' and ' +datetostr(tpk2.date);
      ...
      // 找到第一句非空子条件
      for i:=0 to 7 do
        if str[i]<>'' then
        begin
          iFirst:=i;
         //str 保证and 和or 前只有一个空个;or 至少有两个空格
         str[ifrist]:=copy(str[ifirst],5,length(str[ifirst]);
         str[ifrist]:=' '+str[ifrist];
         sSql:=sSql+' where '+str[ifirst];       break;
        end;  for i:=ifrist+1 to 7 do
        sSql:=ssql+str[i]