本人要用多个combobox来选择以后(有的可选,有的可不选),在DBGrid中显示出符合条件的选项。问题是:不知道如果让它判断出如果combobox(其中一个)为空值时,就跳过该空的combobox。本人用的是SQL语句。
    
    datamodule4.adoquery1.Close;
    datamodule4.ADOQuery1.SQL.Clear;
   begin
     if (combobox6.ItemIndex<>-1) then
        a:=combobox6.Items[combobox6.itemindex];
        if (combobox8.ItemIndex<>-1) then
            c:=combobox8.Items[combobox8.itemIndex];
            if (combobox9.ItemIndex<>-1) then
               d:=combobox9.Items[combobox8.itemIndex];
               if (combobox10.ItemIndex<>-1) then
                  e:=combobox10.Items[combobox8.itemIndex];
                  if (combobox11.ItemIndex<>-1) then
                     f:=combobox11.Items[combobox8.itemIndex];
                     if (combobox12.ItemIndex<>-1) then
                        g:=combobox12.Items[combobox8.itemIndex];
                        
datamodule4.ADOQuery1.SQL.Add('select * from 学生信息 where 性别 like'''+a+''' or 民族 like'''+c+'''or 学生类型 like'''+d+''' or 院系 like'''+e+''' or 政治面貌 like'''+f+''' or 专业 like'''+g+'''');

解决方案 »

  1.   

    那你还是拼成sql语句的字符串再datamodule4.ADOQuery1.SQL.Add(sqltext);
      

  2.   

    你可以这样做
    var
      Strginsql:String;
    begin
      Stringsql:='select * from table where 2>1'  //这里2>1是为了不判断AND语句 
      if a<>'' then 
      Stringsql:=StringSql+' and 民族='+''''+aaaa+'''';
        if b<>'' then 
      Stringsql:=StringSql+' and 民族='+''''+bbbb+'''';
      if c<>'' then 
      Stringsql:=StringSql+' and 民族='+''''+cccc+'''';
      if d<>'' then 
      Stringsql:=StringSql+' and 民族='+''''+dddd+'''';
      if a<>'' then 
      Stringsql:=StringSql+' and 民族='+''''+aaaa+'''';  
    剩下的往里边加。多少个都可以。
    end;