在一段查询数据库记录的代码中频繁使用了如下代码:
    if Edit1.Text='' then   //Edit1为查询的值
    begin
      ShowMessage('请输入查询值!');
      Edit1.SetFocus;
    end
    else
    begin      
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      ADOQuery1.SQL.Add('select ');//查询语句省略
      ADOQuery1.Open;
    end;
然后运行时如果输入了查询值就不会报错,如果查询值为空就会报错,报错内容为:ADOQuery1:Cannot perform this operation on a closed dataset
求教各位大哥,小弟先谢谢了!

解决方案 »

  1.   

    if trim(Edit1.Text)='' then   //Edit1为查询的值
        begin
          ShowMessage('请输入查询值!');
          Edit1.SetFocus;
          Exit;  //加这一句试试
        end
        else
        //.....感觉不像是这里的问题,这段代码所在过程是否还有其它代码?
      

  2.   

    晕,加上Exit竟然可以了。请问大哥为什么会出现那种情况?
      

  3.   

    大哥我把代码贴出来您帮忙看看那里操纵数据集时产生问题了
    procedure TCheckRoom.Original();   //private procedure Original();
    begin
      ComboBox1.Items.Clear;
      ComboBox1.Text:='';
      Edit1.Text:='';
      LabeledEdit1.Text:='';
      RadioButton7.Checked:=True;
      RadioButton8.Checked:=False;
      RadioButton9.Checked:=False;
      RadioButton7.Enabled:=False;
      RadioButton8.Enabled:=False;
      RadioButton9.Enabled:=False;
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
    end;procedure TCheckRoom.RadioButton3Click(Sender: TObject);
    begin
      Label1.Caption:='¿Í·¿µØµã£º';
      Original();
      ComboBox1.Visible:=True;
      ComboBox1.SetFocus;
      Edit1.Visible:=False;
      ADOTable1.Open;
      ADOTable1.First;
      while not ADOTable1.Eof do
      begin
        ComboBox1.Items.Add(ADOTable1.FieldByName('roomplace').AsString);
        ADOTable1.Next;
      end;
      ADOTable1.Close;
    end;procedure TCheckRoom.RadioButton1Click(Sender: TObject);
    begin
      Label1.Caption:='·¿¿ÍÐÕÃû£º';
      Original();
      ComboBox1.Visible:=False;
      Edit1.Visible:=True;
      Edit1.SetFocus;
    end;procedure TCheckRoom.RadioButton2Click(Sender: TObject);
    begin
      Label1.Caption:='¿Í·¿±àºÅ£º';
      Original();
      ComboBox1.Visible:=True;
      ComboBox1.SetFocus;
      Edit1.Visible:=False;
      ADOTable1.Open;
      ADOTable1.First;
      while not ADOTable1.Eof do
      begin
        ComboBox1.Items.Add(ADOTable1.FieldByName('roomid').AsString);
        ADOTable1.Next;
      end;
      ADOTable1.Close;
    end;procedure TCheckRoom.RadioButton4Click(Sender: TObject);
    begin
      Label1.Caption:='¿Í·¿µÈ¼¶£º';
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      ComboBox1.Items.Clear;
      ComboBox1.Text:='';
      Edit1.Text:='';
      RadioButton7.Checked:=True;
      RadioButton8.Checked:=False;
      RadioButton9.Checked:=False;
      RadioButton7.Enabled:=True;
      RadioButton8.Enabled:=True;
      RadioButton9.Enabled:=True;
      ComboBox1.Visible:=True;
      ComboBox1.SetFocus;
      Edit1.Visible:=False;
      ComboBox1.Items.Add('ÆÕͨ¼ä');
      ComboBox1.Items.Add('ºÀ»ª¼ä');
      ComboBox1.Items.Add('Öӵ㷿');
    end;procedure TCheckRoom.BitBtn2Click(Sender: TObject);
    begin
      Close;
    end;procedure TCheckRoom.BitBtn1Click(Sender: TObject);
    begin
      if RadioButton2.Checked then
      begin
        if ComboBox1.Text='' then
        begin
          ShowMessage('ÇëÊäÈë¿Í·¿±àºÅ£¡');
          ComboBox1.SetFocus;
        end
        else
        begin
          ADOQuery1.Close;
          ADOQuery1.SQL.Clear;
          ADOQuery1.SQL.Add('select roomid as ¿Í·¿±àºÅ,roomplace as ¿Í·¿µØµã,roomclass as ¿Í·¿µÈ¼¶,roomnum as ¿ÉסÈËÊý,roomprice as ¿Í·¿¼Û¸ñ,roomgoods as ¿Í·¿ÎïÆ·,roomnow as ¿Í·¿ÏÖ×´ from roomset where roomid='+ComboBox1.Text);
          ADOQuery1.Open;
        end;
      end;
      if RadioButton1.Checked then
      begin
        if Edit1.Text='' then
        begin
          ShowMessage('ÇëÊäÈë·¿¿ÍÐÕÃû£¡');
          Edit1.SetFocus;
          Exit;
        end
        else
        begin
          ADOQuery1.Close;
          ADOQuery1.SQL.Clear;
          ADOQuery1.SQL.Add('select type as ÈëסÀàÐÍ,name as ·¿¿ÍÐÕÃû,sex as ÐÔ±ð,guesttype as ·¿¿ÍÀàÐÍ,provetype as Ö¤¼þÀàÐÍ,');
          ADOQuery1.SQL.Add('unit as ¹¤×÷µ¥Î»,communicate as Í¨ÐŵØÖ·,touch as ÁªÏµ·½Ê½,promoney as Ô¤½»Ñº½ð,num as ÈËÊý,protime as Ô¤×¡Ê±¼ä,roomid as ¿Í·¿±àºÅ,booktime as µÇ¼Çʱ¼ä from bookroom where name='''+Edit1.Text+'''');
          ADOQuery1.Open;
        end;
      end;
      if RadioButton3.Checked then
      begin
        if ComboBox1.Text='' then
        begin
          ShowMessage('ÇëÊäÈë¿Í·¿µØµã£¡');
          ComboBox1.SetFocus;
        end
        else
        begin
          ADOQuery1.Close;
          ADOQuery1.SQL.Clear;
          ADOQuery1.SQL.Add('select roomplace as ¿Í·¿µØµã,roomid as ¿Í·¿±àºÅ,roomclass as ¿Í·¿µÈ¼¶,roomnum as ¿ÉסÈËÊý,roomprice as ¿Í·¿¼Û¸ñ,roomgoods as ¿Í·¿ÎïÆ·,roomnow as ¿Í·¿ÏÖ×´ from roomset where roomplace='''+ComboBox1.Text+'''');
          ADOQuery1.Open;
        end;
      end;
      if RadioButton4.Checked then
      begin
        if ComboBox1.Text='' then
        begin
          ShowMessage('ÇëÊäÈë¿Í·¿µÈ¼¶£¡');
          ComboBox1.SetFocus;
        end
        else
        begin
          if RadioButton7.Checked then
          begin
            ADOQuery1.Close;
            ADOQuery1.SQL.Clear;
            ADOQuery1.SQL.Add('select roomclass as ¿Í·¿µÈ¼¶,roomid as ¿Í·¿±àºÅ,roomplace as ¿Í·¿µØµã,roomnum as ¿ÉסÈËÊý,roomprice as ¿Í·¿¼Û¸ñ,roomgoods as ¿Í·¿ÎïÆ·,roomnow as ¿Í·¿ÏÖ×´ from roomset where roomclass='''+ComboBox1.Text+'''');
            ADOQuery1.Open;
          end;
          if RadioButton8.Checked then
          begin
            ADOQuery1.Close;
            ADOQuery1.SQL.Clear;
            ADOQuery1.SQL.Add('select roomclass as ¿Í·¿µÈ¼¶,roomid as ¿Í·¿±àºÅ,roomplace as ¿Í·¿µØµã,roomnum as ¿ÉסÈËÊý,roomprice as ¿Í·¿¼Û¸ñ,roomgoods as ¿Í·¿ÎïÆ·,roomnow as ¿Í·¿ÏÖ×´ from roomset where roomclass='''+ComboBox1.Text+''' and roomnow='''+'Õ¼ÓÃ'+'''');
            ADOQuery1.Open;
          end;
          if RadioButton9.Checked then
          begin
            ADOQuery1.Close;
            ADOQuery1.SQL.Clear;
            ADOQuery1.SQL.Add('select roomclass as ¿Í·¿µÈ¼¶,roomid as ¿Í·¿±àºÅ,roomplace as ¿Í·¿µØµã,roomnum as ¿ÉסÈËÊý,roomprice as ¿Í·¿¼Û¸ñ,roomgoods as ¿Í·¿ÎïÆ·,roomnow as ¿Í·¿ÏÖ×´ from roomset where roomclass='''+ComboBox1.Text+''' and roomnow='''+'¿ÕÏÐ'+'''');
            ADOQuery1.Open;
          end;
        end;
      end;
      LabeledEdit1.Text:=IntToStr(ADOQuery1.RecordCount);
    end;
      

  4.   

    查询为空时当然会抱错
    因为你已经ADOQuery1.Close
    但是LabeledEdit1.Text:=IntToStr(ADOQuery1.RecordCount);
    还在引用我也菜,不知说的对否
      

  5.   

    LabeledEdit1.Text:=IntToStr(ADOQuery1.RecordCount);我想错误可能出在最后这句了,当所有条件都不满足时,adoquery1为close状态,你访问RecordCount属性便会报错建议:每判断完一个条件adoquery1.open之后都加一句Exit
      

  6.   

    把LabeledEdit1.Text:=IntToStr(ADOQuery1.RecordCount);
    放在每个
    if RadioButton2.Checked then
      begin
        if ComboBox1.Text='' then
        begin
          ShowMessage('');
          ComboBox1.SetFocus;
        end
        else
        begin
          ADOQuery1.Close;
          ADOQuery1.SQL.Clear;
          ADOQuery1.SQL.Add****---*
          ADOQuery1.Open;
          LabeledEdit1.Text:=IntToStr(ADOQuery1.RecordCount);//放这里
        end;
      end;
    应该可以解决你的问题