我想利用ware(id,code,name,...)建立自动编号, 其中code是varchar类型,代码如下:procedure Teditsp.NewCode(Sender: integer);
var code: string;
begin
  Query1.Close;
  Query1.sql.text := 'select count(*) from ware';
  Query1.open;
  code := floattostr(100000 + Query1.fields[0].asfloat + Sender);
  Query1.Close;
  Query1.sql.text := 'select count(*) from ware where code=:code';
  Query1.Parameters[0].Value := code;
  Query1.open;
  if Query1.Fields[0].Value > 0 then
  begin
    Sender := sender + 1;
    Newcode(sender);
  end
  else
  begin
   spda.ADOTable2.FieldByName('code').value := code;
  end;
end;
但是程序运行到spda.ADOTable2.FieldByName('code').value := code;时发生一个错误:
access violation at address 0081AF2C in module...
不知是什么原因?谢谢!!

解决方案 »

  1.   

    你根本就没有code这个字段,你怎么就不自己检查检查你的代码呢,你就一个select count(*) ,从哪来的code字段名
      

  2.   

    spda窗体ADOTable2表有code这个字段啊,我检查了,没错哦?
      

  3.   

    spda.ADOTable2.FieldByName('code').value := code;
    中有不存在的对象,自己跟踪一下
      

  4.   

    是說你的SQL語句並未有Code這個字段值,select count(*)只是統計到個數而已...
      

  5.   

    ware(id,code,name,...)procedure Teditsp.NewCode(Sender: integer);不一致呀,是否应该是procedure Teditsp.ware(id,code,name,...);
      

  6.   

    把'select count(*) from ware where code=:code'
    改为'select count(*) as code from ware where code=:code'
    就可以了.
      

  7.   

    不好意思,看错了.以上代码无效,这个原因是内存读取错误.
    这部分代码看不到,猜测是你的spda对象没有创建(Create).
      

  8.   

    2种情况:
    1. spda窗体未创建
    2. ADOTable2无此字段
      

  9.   

    你的局部变量code没有被赋值啊,而且整个过程还是递归调用,必须要如此吗?
      

  10.   

    怪事,我把它修改成spda.ADOTable2Code.Value:= code;就没问题了。不解!!
      

  11.   

      spda.edit;
      spda.ADOTable2.FieldByName('code').value := code; 
      spda.post;==============按你上面说,说明数据类型对上了,呵呵,你看一下
    spda.ADOTable2Code 这个是定义的什么类型啊,你的数据表中定义的是什么类型。反正已知code 是string类型.