想要实现的效果:程序在打开新建按钮后,编号自动加上一
思路:在点击新建后,程序自动移至最后一条纪录,并且把这条纪录的编号取出,赋予一个变量,然后变量+1,再回写到数据库里面代码:procedure TForm1.ToolButton1Click(Sender: TObject);
var
m:string;
n:integer;
 begin
while not adotable1.Eof do   //检测数据表指针是否已指向末位
begin
adotable1.Next;
end;
begin
adotable1.Active:=true;
with adotable1 do
begin
m:=FieldValues['订单号'];    //复制上一条记录
n:=(strtoint(m)+1);
edit20.Text:=inttostr(n)
end;
end;但我代码执行下来不对,它只是把第一条纪录的数值取出,而不是我要的最后一条,哪位知道为什么会出现这个情况的?

解决方案 »

  1.   

    var
    m:string;
    n:integer;
     begin
    adotable1.Active:=true;//--------1
    while not adotable1.Eof do   //检测数据表指针是否已指向末位
    begin
    adotable1.Next;
    end;
    begin
    //  adotable1.Active:=true;//-----------2
    with adotable1 do
    begin
    m:=FieldValues['订单号'];    //复制上一条记录
    n:=(strtoint(m)+1);
    edit20.Text:=inttostr(n)
    end;
    end;1处:adotable1  未打开,没有记录  故adotable1.Eof 为true
    应该先打开  adotable1
      

  2.   

    adotable1.open;
    while not adotable1.Eof do   //检测数据表指针是否已指向末位
    begin
    adotable1.Next;
    end;
      

  3.   

    这是因为对EOF和BOF的错误理解,table的EOF 和BOF表示当前记录指针已经在表体之外了,并非最后一条记录和第一条记录,EOF之后再active,则表肯定定位在第一条记录。而且你的算法也很奇怪,while ...next ...完全是浪费时间和资源,完全不需要,更直接的写法是。
    table1.open;
    table1.last;
    然后取值即可
      

  4.   

    procedure TForm1.ToolButton1Click(Sender: TObject);
    var
    m:string;
    n:integer;
    begin
       with adotable1 do
       begin
          Open;
          Last;
          m:=FieldValues['订单号'];    //复制上一条记录
          n:=(strtoint(m)+1);
          edit20.Text:=inttostr(n)
       end;
    end;