程序如下:
在执行到query1.Fields.Fields[0].Value程序报错"List Index out of bounds(0)"
请问如何解决?????
用TQuery和TTable控件如何在表中取值?????
procedure TForm2.Button1Click(Sender: TObject);
var
  no,gnum:integer;
  gprice:real;
  gno,gowner,sno,gname:string;
begin
    if pagecontrol1.ActivePage=tabsheet1 then
  begin
     gno:=edit1.Text;
     gnum:=strtoint(edit2.Text);
     gname:=edit3.Text;
     gowner:=edit4.Text;
     sno:=edit5.Text;
     gprice:=strtofloat(edit7.Text);
     no:=strtoint(edit6.Text);
     table1.InsertRecord([no,gno,gnum,gprice,gowner,sno,datetimepicker1.date]);
     table1.Active:=false;
     table1.TableName:='good';
     table1.Active:=true;
     query1.Close;
     query1.SQL.Clear;
     query1.SQL.Add('select gnum from good where gno=:gno;');
     if query1.Fields.Fields[0].Value=Null then
    //执行if语句时程序报错"List Index out of bounds(0)"
     begin
       table1.InsertRecord([gno,gname,gnum,gprice,gowner,sno]);
     end
     else
     begin
       table1.SetFields([gno,gname,gnum+query1.fields[0].value,gprice,gowner,sno]);
     end;
  end;
   form3.ShowModal;
end;

解决方案 »

  1.   

    query1没有open就访问,不出错就怪了。
      

  2.   

    query1没有open就访问
        query1.SQL.Clear;
         query1.SQL.Add('select gnum from good where gno=:gno;');
         query1.open;
         if query1.Fields.Fields[0].Value=Null then
        //执行if语句时程序报错"List Index out of bounds(0)"
         begin
      

  3.   

    不是啊,我也遇到同样的问题:
    var st:string;
    ....  
      qry.Close;
      qry.SQL.Clear;
      qry.SQL.Add('select * from ksid');
      qry.ExecSQL;
      st:=qry.Fields.Fields[1].AsString;
      qry.Close;
      showmessage(st);这是用来测试的,但执行到st:=qry.Fields.Fields[1].AsString;也是报错:List Index out of bounds(0)。想得头都大了!
      

  4.   

    sorry,报的是这个错List Index out of bounds(1)
      

  5.   

    我靠,找出原因了,但还是想不解——为什么用open就行,用execsql就报错呢?
      

  6.   

    查询QUEYR不打开是不能用的 
    EXECSQL是执行语句
    它是执行条件呀!
      

  7.   

    open是用来执行有返回结果的SQL语句的如:select * from table1;
    ExecSQL是用来执行没有返回结果的SQL语句的如:insert into table1 (field1, field2....);
      

  8.   

    strayerctx(一夜风雨) :
         谢谢!那些所谓的参考书根本就没提到这个问题,害我差点跳楼——嘿嘿,跳到楼顶作运动而已!楼主,谢了!你的问题应该ok了吧?!