procedure TForm56.ComboBox1Change(Sender: TObject);
begin
  if Trim(ComboBox1.Text)<>''then
  begin
    with DataModule1.ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select distinct');
      SQL.Add(Trim(ComboBox1.Text));
      SQL.Add('from 商品基础信息表');
      Open;
    end;
    ComboBox2.Clear;
    while Not DataModule1.ADOQuery1.Eof do
    begin
      ComboBox2.Items.Add(DataModule1.ADOQuery1.Fields[0].value);
      DataModule1 .ADOQuery1.Next;
    end;
  end;
end;procedure TForm56.SpeedButton1Click(Sender: TObject);
  begin
    with DataModule1.ADOQuery2 do
    begin
      Close;
      SQL.Clear;
      if CheckBox2.Checked = False then
      begin
        Sql.Text:='select * from 商品基础信息表  where 1=1';
        if (ComboBox1.ItemIndex>=0) and (ComboBox2.ItemIndex>=0) then
            Sql.Text:=Sql.Text+' and '+Trim(ComboBox1.Text)+'='''+Trim(ComboBox2.Text)+''' ';
        if (ComboBox3.ItemIndex>=0) and (ComboBox4.ItemIndex>=0) and (ComboBox7.ItemIndex>=0) then
            Sql.Text:=Sql.Text+Trim(ComboBox7.Text)+' '+Trim(ComboBox3.Text)+'='''+Trim(ComboBox4.Text)+''' ';
        if (ComboBox5.ItemIndex>=0) and (ComboBox6.ItemIndex>=0) and (ComboBox8.ItemIndex>=0) then
            Sql.Text:=Sql.Text+Trim(ComboBox8.Text)+' '+Trim(ComboBox5.Text)+'='''+Trim(ComboBox6.Text)+''' ';      end
      else
         Sql.Text:='select * from 商品基础信息表  where 1=1';
        if (ComboBox1.ItemIndex>=0) and (ComboBox2.ItemIndex>=0) then
            Sql.Text:=Sql.Text+' and '+Trim(ComboBox1.Text)+'='''+Trim(ComboBox2.Text)+''' ';
        if (ComboBox3.ItemIndex>=0) and (ComboBox4.ItemIndex>=0) and (ComboBox7.ItemIndex>=0) then
            Sql.Text:=Sql.Text+Trim(ComboBox7.Text)+' '+Trim(ComboBox3.Text)+'='''+Trim(ComboBox4.Text)+''' ';
        if (ComboBox5.ItemIndex>=0) and (ComboBox6.ItemIndex>=0) and (ComboBox8.ItemIndex>=0) then
            Sql.Text:=Sql.Text+Trim(ComboBox8.Text)+' '+Trim(ComboBox5.Text)+'='''+Trim(ComboBox6.Text)+''' ';
      Open;
    end;
    if DataModule1 .ADOQuery2.RecordCount>0 then
    DataSource1.DataSet := DataModule1 .ADOQuery2;
end;
以上语句可正常实现自定义组合条件查询,但把'='''替换成Trim(ComboBox12.text),其中ComboBox12的item属性设置为=、>、<、>=、<=后,查询'开票日期'可正常查询,但是查询'用车单位'后就出现'project delivery.exe raised exception class EOleException with message ‘列名’元德实业’无效.‘. process stopped use step or run to continue. 
请高手给予指点。谢谢先!

解决方案 »

  1.   

    说的不清楚。不知道'用车单位'是哪个ComboBox里的值。
    不过这样的问题很简单应该就可以单步调试找到原因了。
      

  2.   

    ComboBox1选中开票日期、用车单位,ComboBox12选中=、>=,ComboBox2列表中会自动出现所对应的字段值,比如说元德实业,然后单击查询按钮就提示现'project delivery.exe raised exception class EOleException with message ‘列名’元德实业’无效.‘. process stopped use step or run to continue. 如果选中开票日期就可以正常查询,不知道为什么在ComboBox1字段值里面选中用车单位,ComboBox12字段值里面选中=,ComboBox2字段值里面选中元德实业就会出现上面的错误提示。(ComboBox1里面的字段值是按照开票日期,用车单位依次排列的)
      

  3.   

    但把'='''替换成Trim(ComboBox12.text),
    该替换成Trim(ComboBox12.text)+''''
    吧?
    我建议lz 用QuotedStr()来给字符串加引号。你那么一大堆,怎么看,怎么昏。