各位高手,小弟有个问题求救:
  我使用aodquery这个控件来访问数据库实现查询功能,
在Form上我是用combobox,Edit,和Button来实现的,用GBGrid本人做了个例子
要查询表BOOKS(书号,书名,...);
在combobox1设有书号,书名;combobox2设有'=','>'等值....在Edit填写符合条件的值;
ButtonClick的源程序如下:
if edit6.Text<>'' then
begin  if combobox1.Text='书号' then
     begin
       if combobox2.Text='='then
          begin
             ADOQuery1.Close;
             ADOQuery1.SQL.Clear;
             ADOQuery1.SQL.Add('select *from books where 书号 like :sh');
            ADOQuery1.Parameters.ParamByName('sh').Value:=edit6.Text;
            ADOQuery1.Open;
          
           end;
      end
   else
       begin       ADOQuery1.Close;
       edit1.Text:='';       end  ;       
    if combobox1.Text='书名'   then
   begin
       if combobox2.Text='='then
       begin
       ADOQuery1.Close;
       ADOQuery1.SQL.Clear;
       ADOQuery1.SQL.Add('select * from books where 书名 like :sm');
      ADOQuery1.Parameters.ParamByName('sm').Value:=edit1.Text;
      ADOQuery1.Open;
          
       end;   end;
end;
小弟加了三条记录,后两条记录的书名相同,书号不同;
程序顺利通过编译,但是经过简单的调试,发现可以对'书号'查询,却不能实现对'书名'的查询,结果并没有在GBGrid中显示,但是把
if combobox1.Text='书号' then
     begin
       if combobox2.Text='='then
          begin
             ADOQuery1.Close;
             ADOQuery1.SQL.Clear;
             ADOQuery1.SQL.Add('select *from books where 书号 like :sh');
            ADOQuery1.Parameters.ParamByName('sh').Value:=edit6.Text;
            ADOQuery1.Open;
          
           end;
      end
   else
       begin       ADOQuery1.Close;
       edit1.Text:='';       end  ;
这段删除后,就可以实现对'书名'的查询,结果在GBGrid显示
到底问题是出在哪里呢?我该怎么改动才能都能实现对'书号','书名'的查询呢??
请各位高手指出,不胜感激!!!

解决方案 »

  1.   

    if edit6.Text<>'' then
    begin
      if combobox1.Text='书号' then
         begin
           if combobox2.Text='='then
              begin
                 ADOQuery1.Close;
                 ADOQuery1.SQL.Clear;
                 ADOQuery1.SQL.Add('select *from books where 书号 like :sh');
                 ADOQuery1.Parameters.ParamByName('sh').Value:=edit6.Text;
                 ADOQuery1.Open;
               end;
          end
       else     //   不能写在这里, 写在这里代表当Combobox2不等于'='时关闭数据集ADOQUERY1;
           begin
             ADOQuery1.Close;
             edit1.Text:='';
           end  ;改为下面的就应该可以了吧:
    ----------------------------------------------------------------------------------------
    if combobox1.Text='书号' then
         begin
           if combobox2.Text='='then
              begin
                 ADOQuery1.Close;
                 ADOQuery1.SQL.Clear;
                 ADOQuery1.SQL.Add('select *from books where 书号 like :sh');
                 ADOQuery1.Parameters.ParamByName('sh').Value:=edit6.Text;
                 ADOQuery1.Open;
               end
               else      
                 begin
                   ADOQuery1.Close;
                   edit1.Text:='';
                 end  ;
         end;
      

  2.   

    ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      ADOQuery1.SQL.Add('select *from books where 书号 like :sh');
      
      ADOQuery1.active :=false;
      ADOQuery1.Parameters.ParamByName('sh').Value:=edit6.Text;
      ADOQuery1.active :=true;ADOQuery1不用OPEN