在执行的时候老是说在and或者=附近有问题,是不是哪儿写错了阿var
condition : string;
sql : string;begin
//执行学生信息查询
    if (checkbox1.Checked = false) and (checkbox2.Checked = false) and (checkbox3.Checked = false)
     and (checkbox4.Checked = false) and (checkbox5.Checked = false) and (checkbox6.Checked = false)
     and (checkbox7.Checked = false) and (checkbox8.Checked = false)then
      begin
        MessageBox(self.Handle, '请选择要打印的内容!', '提示', mb_IconInformation + mb_Ok);      end;
    condition:=' ';
    if CheckBox1.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'studentID='+edit1.Text;
      end;
    if CheckBox2.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'name='+edit3.Text;
      end;
    if CheckBox3.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'classID='+combobox1.Text;
      end;
    if CheckBox4.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'grade='+combobox2.Text;
      end;
    if CheckBox4.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'instituteID='+combobox3.Text;
      end;
    if CheckBox5.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'departmentID='+combobox4.Text;
      end;
    if CheckBox6.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'major='+combobox5.Text;
      end;
    if CheckBox6.Checked=true then
      begin
        if condition<>' ' then
          begin
            condition:=condition+' and ';
          end;
        condition:=condition+'campus='+combobox6.Text;
      end;
    sql:='select * from students where '+condition;
    edit1.Text:=sql;
   ADODataSet1.Close;
    ADODataSet1.CommandText:=sql;
    ADODataSet1.Open;

解决方案 »

  1.   

    好复杂的一人东西啊,你可以把整个SQL查询语句写出来,按照你程序里写的,再去找,那样会很快
      

  2.   

    if (checkbox1.Checked = false) and (checkbox2.Checked = false) and (checkbox3.Checked = false)
         and (checkbox4.Checked = false) and (checkbox5.Checked = false) and (checkbox6.Checked = false)
         and (checkbox7.Checked = false) and (checkbox8.Checked = false)then
    最后是不是)和then间少了个空格
      

  3.   

    代码其实很简单,就是复选框选择查询的条件,在学生信息表里面查询,可能我写的复杂了。单步执行说是sql语句执行到and的时候出错,不知道sql语句哪儿写错了??
      

  4.   

    看了半天终于看出来了,第一个条件写错了,不应该加 and 因为这样语句成了
    select * from tablename where and conditions
    :) 删掉那个and 就行了
      

  5.   

    所有的if condition<>' ' then都不用要
    只需要把
    sql:='select * from students where '+condition;
    改成
    sql:='select * from students where 1=1 and ' +   condition;
    能大量减少代码行数
      

  6.   

    if CheckBox1.Checked=true then
    这个你不能写成
    if CheckBox1.Checked then 
    吗?
    看着so别扭
      

  7.   

    你把所有的钩打上,然后用showmessage把这个字符串显示出来,再贴到这里来,那样会快很多,也容易帮你分析
      

  8.   

    天!!!
    “= false”你也会写???
    什么叫逻辑变量啊?
    老师救命。
      

  9.   

    //改成
    //sql:='select * from students where 1=1 and ' +   condition;//
    //能大量减少代码行数长知识