我想实现的功能是:
 1、在edit1编辑框内输入“姓名” 点击“查询”按钮进行模糊查询,其它edit编辑框显示查询出此姓名的相关信息;
 2、点击“下一位”,编辑框显示模糊查询出的下一条记录。我的遇到的困难是:
1、当查询记录为空时,系统提示下面错误:
  Project Project1.exe raised exception class EVariantTypeCastError with message 'Could not    convert variant of type (Null) into type (String)'. Process stopped. Use Step or Run to continue.
  怎么以提示信息的方式跳过这个错误提示?
2、点击“下一位”,编辑框显示模糊查询出的下一条记录,这个按钮功能代码怎么写?
这是我写的“查询”按钮代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.Text<>'' then
  begin
    adoquery1.Close;
    adoquery1.SQL.Clear;
    adoquery1.SQL.Text := 'select isnull(tDept,'''') as tDept,isnull(cDuty,'''')as cDuty,'
      +'isnull(cName,'''')as cName,isnull(iInLine,'''')as iInLine,isnull(iExtLine,'''')as iExtLine,'
      +'isnull(iHandset,'''')as iHandset,isnull(iOthContWise,'''')as iOthContWise'
      +' from addresslist where cName like +''%'+trim(edit1.Text)+'%''';
    adoquery1.ExecSQL;
    adoquery1.Active:= true;
    if adoquery1.FieldByName('cName').Value ='' then
    begin
      edit2.Text:= '';
      edit3.Text:= '';
      edit4.Text:= '';
      edit5.Text:= '';
      edit6.Text:= '';
      edit7.Text:= '';
      edit8.Text:= '';
    end
    else
      begin
        edit2.Text:= adoquery1.fieldbyname('cName').Value;
        edit3.Text:= adoquery1.fieldbyname('tDept').Value;
        edit4.Text:= adoquery1.fieldbyname('cDuty').Value;
        edit5.Text:= adoquery1.fieldbyname('iInLine').Value;
        edit6.Text:= adoquery1.fieldbyname('iExtLine').Value;
        edit7.Text:= adoquery1.fieldbyname('iHandset').Value;
        edit8.Text:= adoquery1.fieldbyname('iOthContWise').Value;
      end
  end
  else
    showmessage('名字不能为空');
end;

解决方案 »

  1.   


    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
      if Edit1.Text <>'' then 
      begin 
        adoquery1.Close; 
        adoquery1.SQL.Clear; 
        adoquery1.SQL.Text := 'select isnull(tDept,'''') as tDept,isnull(cDuty,'''')as cDuty,' 
          +'isnull(cName,'''')as cName,isnull(iInLine,'''')as iInLine,isnull(iExtLine,'''')as iExtLine,' 
          +'isnull(iHandset,'''')as iHandset,isnull(iOthContWise,'''')as iOthContWise' 
          +' from addresslist where cName like +''%'+trim(edit1.Text)+'%'''; 
        adoquery1.ExecSQL; 
        adoquery1.Active:= true; 
        if adoquery1.FieldByName('cName').Value ='' then 
        begin 
          edit2.Text:= ''; 
          edit3.Text:= ''; 
          edit4.Text:= ''; 
          edit5.Text:= ''; 
          edit6.Text:= ''; 
          edit7.Text:= ''; 
          edit8.Text:= ''; 
        end 
        else 
          begin 
            edit2.Text:= Format('s%',adoquery1.fieldbyname('cName').Value); 
            edit3.Text:= Format('s%',adoquery1.fieldbyname('tDept').Value); 
            edit4.Text:= Format('s%',adoquery1.fieldbyname('cDuty').Value); 
            edit5.Text:= Format('s%',adoquery1.fieldbyname('iInLine').Value); 
            edit6.Text:= Format('s%',adoquery1.fieldbyname('iExtLine').Value); 
            edit7.Text:= Format('s%',adoquery1.fieldbyname('iHandset').Value); 
            edit8.Text:= Format('s%',adoquery1.fieldbyname('iOthContWise').Value); 
          end 
      end 
      else 
        showmessage('名字不能为空'); 
    end;
      

  2.   


    //下一条记录
    procedure btnNextClick(Sender: TObject);
    begin
      With ADOQuery1 do
      begin
        Next;
        edit2.Text:= Format('s%',adoquery1.fieldbyname('cName').Value); 
        edit3.Text:= Format('s%',adoquery1.fieldbyname('tDept').Value); 
        edit4.Text:= Format('s%',adoquery1.fieldbyname('cDuty').Value); 
        edit5.Text:= Format('s%',adoquery1.fieldbyname('iInLine').Value); 
        edit6.Text:= Format('s%',adoquery1.fieldbyname('iExtLine').Value); 
        edit7.Text:= Format('s%',adoquery1.fieldbyname('iHandset').Value); 
        edit8.Text:= Format('s%',adoquery1.fieldbyname('iOthContWise').Value); 
      end;
    end;//最后一条记录
    procedure btnNextClick(Sender: TObject);
    begin
      With ADOQuery1 do
      begin
        Last;
        edit2.Text:= Format('s%',adoquery1.fieldbyname('cName').Value); 
        edit3.Text:= Format('s%',adoquery1.fieldbyname('tDept').Value); 
        edit4.Text:= Format('s%',adoquery1.fieldbyname('cDuty').Value); 
        edit5.Text:= Format('s%',adoquery1.fieldbyname('iInLine').Value); 
        edit6.Text:= Format('s%',adoquery1.fieldbyname('iExtLine').Value); 
        edit7.Text:= Format('s%',adoquery1.fieldbyname('iHandset').Value); 
        edit8.Text:= Format('s%',adoquery1.fieldbyname('iOthContWise').Value); 
      end;
    end;//第一条记录
    procedure btnNextClick(Sender: TObject);
    begin
      With ADOQuery1 do
      begin
        First;
        edit2.Text:= Format('s%',adoquery1.fieldbyname('cName').Value); 
        edit3.Text:= Format('s%',adoquery1.fieldbyname('tDept').Value); 
        edit4.Text:= Format('s%',adoquery1.fieldbyname('cDuty').Value); 
        edit5.Text:= Format('s%',adoquery1.fieldbyname('iInLine').Value); 
        edit6.Text:= Format('s%',adoquery1.fieldbyname('iExtLine').Value); 
        edit7.Text:= Format('s%',adoquery1.fieldbyname('iHandset').Value); 
        edit8.Text:= Format('s%',adoquery1.fieldbyname('iOthContWise').Value); 
      end;
    end;//上一条记录
    procedure btnNextClick(Sender: TObject);
    begin
      With ADOQuery1 do
      begin
        Prior;
        edit2.Text:= Format('s%',adoquery1.fieldbyname('cName').Value); 
        edit3.Text:= Format('s%',adoquery1.fieldbyname('tDept').Value); 
        edit4.Text:= Format('s%',adoquery1.fieldbyname('cDuty').Value); 
        edit5.Text:= Format('s%',adoquery1.fieldbyname('iInLine').Value); 
        edit6.Text:= Format('s%',adoquery1.fieldbyname('iExtLine').Value); 
        edit7.Text:= Format('s%',adoquery1.fieldbyname('iHandset').Value); 
        edit8.Text:= Format('s%',adoquery1.fieldbyname('iOthContWise').Value); 
      end;
    end;
      

  3.   

    edit8.Text:= Format('s%',adoquery1.fieldbyname('iOthContWise').Value); 不要这样写。用edit8.Text:= VarToStr(adoquery1.fieldbyname('iOthContWise').Value); 
    试图将Null转换成字符串就会出现上面的错误。VarToStr把Null转换成''