procedure TForm3.Button1Click(Sender: TObject);
var sqlstr:string;
begin
adoquery1.Edit;
sqlstr:='select * from userinfo';
if (combobox1.Text='精确查询') and (edit1.Text<>'') then
begin
 //adoquery1.SQL.Text:='select * from  userinfo where username='''+edit1.Text+''' and  sex='''+combobox2.Text+''' and  department='''+combobox3.Text+'''' ;  sqlstr:=sqlstr+'where and username='+quotedstr(edit1.text);
  if combobox2.Text<>'' then  sqlstr:=sqlstr+'and sex='+ quotedstr(combobox2.text);  if combobox3.Text<>'' then
  begin
  sqlstr:=sqlstr+'and department='+quotedstr(combobox3.text);
  end;
  ADOQuery1.Close ;
  ADOQuery1.SQL.Clear ;
  ADOQuery1.SQL.Add(sqlstr);
  ADOQuery1.Open ;  if adoquery1.RecordCount=0  then  showmessage('没有符合的记录')
  else
   showmessage('纪录已经查找到了');
   edit1.text:='';
  end;
if combobox1.Text='模糊查询' then
   ADOQuery1.Close ;
   ADOQuery1.SQL.Clear ;
   ADOQuery1.SQL.Add('select * from userinfo');
   ADOQuery1.SQL.Add('where username like'''+edit1.text+'%''');
   ADOQuery1.Open ;

解决方案 »

  1.   

    sqlstr:=sqlstr+'where and username='+quotedstr(edit1.text);
    少了一个空格 应该是: 
    sqlstr:=sqlstr+' where and username='+quotedstr(edit1.text);
      

  2.   

    其实你可以用
    case combobox1.itemsindex of
    0:begin  ...  end;
    1:begin  ...  end;
    这样貌似条理清楚点
      

  3.   

    liyaohuang您好,现在我加了空格后,提示‘语法错误(操作符丢失),在查询表达式 ’and username='a ''中.,
      

  4.   

    'select * from userinfo where username='''+edit1.Text+''' and sex='''+combobox2.Text+''' and department='''+combobox3.Text+'''' ;这种方式就写错了,是因为字符常量两边引号不对。
    'select * from userinfo where username='+''''+edit1.Text+''''+' and sex='+''''+combobox2.Text+'''' +'and department='+''''+combobox3.Text+'''' ;其他的写法类似。
      

  5.   


      ADOQuery1.Close ;
      ADOQuery1.SQL.Clear ;
      ADOQuery1.SQL.Add(sqlstr);
      ADOQuery1.Open ;
    这句话。。你试试在sqlstr两边加引号或者是ADOQUERY1.SQL.TEXT:='sqlstr';还有函数 QuotedStr把字符串S转换成为引用的字符串。单引号" ' " 将被插入到字符串s的最前和最后。你这里quotedstr(edit1.text)运行后应该是'edit1.text'而在delphi的SQL语句中引用输入框的数据应该是'''+edit.text+''' 你试试我也是新手。大家互相学习哈
      

  6.   

    注意語法及關鍵字:'select * from userinfo where [username]='+''''+edit1.Text+''''+' and sex='+''''+combobox2.Text+'''' +'and department='+''''+combobox3.Text+'''' ;
      

  7.   

    sqlstr:=sqlstr+' where and username='+quotedstr(edit1.text);
    请问where之后为什么要加and,
    sqlstr:=sqlstr+' where username='+quotedstr(edit1.text);这样就ok。
      

  8.   

       以后用SQL2000进行跟踪,这样是最有效的找错方法.