procedure TForm1.Button5Click(Sender: TObject);
var
  index,i,j:Integer;
  FilePath,Code,County,formula:string;
  DB:Database;
  DAOQuery2:TDAoquery;
  function getprice(code,county,currentstr:string):string; //这里的code传入的是库编码,对数据库进行一次查询返回county
  begin
   with DAOQuery2 do
    begin
    Close;
    SQL.Clear;
    SQL.Add('select '+County+' from '+currentstr+' where 材料编码 in (select 材料编码 from MonDYB where 库编码='+QuotedStr(code)+')');
     Open;
    result:=FieldByName(County).AsString;
    end;
  end;
begin
   with DAOQuery1 do
     begin
      Close;
      SQL.Clear;
      SQL.Add('select * into MonthPrice from Monformwork');// 复制临时表monthprice 
       Execute(0);
      SQL.Clear;
      SQL.Add('select * from MonthPrice');//怪临时表记录集
       open;
      First;//取第一条记录
      while not eof do 
        begin
         Code:=FieldByName('库编码').AsString;//取第一条记录集中的所需要的一个字段做为下面函数的参数
          for i:=4 to fieldcount-1 do          //对这条记录的的第五个字段至第最后一个字段循环进行处理
           begin
            County:=Fields[i].FieldName;      //取第五个字段做为函数的参数
              DAOQuery2.Create(Application);    //动态建立一个query
            DAOQuery2.Database:=DAODatabase1; 
            with DAOQuery2 do                 //进行第五个字段的处理 
              begin
               Close;
               SQL.Clear;
               SQL.Add('select 转换公式 from MonDYB where 库编码='+QuotedStr(code));
               Open;
               formula:=FieldByName('转换公式').AsString;
               index:=pos('{',formula);
                 while index > 0 do
                   begin
                    j:=pos('}',formula);
                    formula:=copy(formula,1,index-1)+getprice(copy(formula,index+1,j-index-1),County,currentstr)+copy(formula,j+1,length(formula)-j);
                    index:=pos('{',formula);
                    Close;
                    SQL.Clear;
                    SQL.Add('update Monthprice set '+County+'='+formula+' where 库编码='+QuotedStr(code));
                    Execute(0);
                   end;
              end;
           end;
        end;
      next;
     end;
end;帮我看看哪里出问题了?

解决方案 »

  1.   

    这部分代码在忙什么呢
    Close; 
          SQL.Clear; 
          SQL.Add( 'select * into MonthPrice from Monformwork ');// 复制临时表monthprice  
           Execute(0); 
          SQL.Clear; 
          SQL.Add( 'select * from MonthPrice ');//怪临时表记录集 
           open; 
      

  2.   

    记得前几天你提过问题的,你用的是access,access没有存储过程,av错误就是地址错误
      

  3.   

    我用了嵌套使用了DAOquery ,应该是这里出了问题,是不是涉及到释放?
      

  4.   

    DAOQuery2.Create(Application);    //动态建立一个query 
    //这句应该这样写
    DAOQuery2:=TDAoquery.Create( Owner ); 
    try
      ....
    finally
      DAOQuery2.free;
    end;
    另外你应该把DAOQuery2创建过程放到循环以外来做,在循环里频繁的创建与销毁对象效率非常低
      

  5.   

    恩,谢谢月亮又帮我带路,今天又进步了一点^-^
    上面那个动态创建为什么不能那样写?好象ADOquery可以是不是?
      

  6.   

    //你也可以这样用
    with TDAoquery.Create(owner) do
    begin
       .....
       Destroy;
    end;
      

  7.   

    with TDAoquery.Create(owner) do
    begin
       .....
       Destroy;
    end;这样效率会高些吗?
      

  8.   

    其实你的需求没有必要动态创建TDAoquery
      

  9.   

    如果我用放组件的办法,要不要再free了?
      

  10.   

    地址错误通常是引用了一个以释放的地址上的数据,在动态创建组件的时候一定记得销毁create 事件创建组件那么就要在destroy的时候销毁这个组件,
    临时创建的组件最好使用
    创建过程
    try
      ...
    finally
      销毁
    end;
      

  11.   

    procedure TForm1.Button5Click(Sender: TObject); 
    var 
      index,i,j:Integer; 
      FilePath,Code,County,formula:string; 
      DB:Database; 
      DAOQuery2:TDAoquery; 
      function getprice(code,county,currentstr:string):string; //这里的code传入的是库编码,对数据库进行一次查询返回county 
      begin 
       with DAOQuery2 do 
        begin 
        Close; 
        SQL.Clear; 
        SQL.Add( 'select  '+County+ ' from  '+currentstr+ ' where 材料编码 in (select 材料编码 from MonDYB where 库编码= '+QuotedStr(code)+ ') '); 
        Open; 
        result:=FieldByName(County).AsString; 
        end; 
      end; 
    begin 
       with DAOQuery1 do 
         begin 
          Close; 
          SQL.Clear; 
          SQL.Add( 'select * into MonthPrice from Monformwork ');// 复制临时表monthprice  
          Execute(0); 
          SQL.Clear; 
          SQL.Add( 'select * from MonthPrice ');//取临时表记录集 
           open; 
          First;//取第一条记录 
           while not eof do  
            begin 
             Code:=FieldByName( '库编码 ').AsString;//取第一条记录集中的所需要的一个字段做为下面函数的参数 
              for i:=4 to fieldcount-1 do          //对这条记录的的第五个字段至第最后一个字段循环进行处理 
               begin 
                County:=Fields[i].FieldName;      //取第五个字段做为函数的参数 
                with DAOQuery2 do                 //进行第五个字段的处理  
                  begin 
                   Close; 
                   SQL.Clear; 
                   SQL.Add( 'select 转换公式 from MonDYB where 库编码= '+QuotedStr(code)); 
                   Open; 
                   formula:=FieldByName( '转换公式 ').AsString; 
                   index:=pos( '{ ',formula); 
                     while index  > 0 do 
                       begin 
                        j:=pos( '} ',formula); 
                        formula:=copy(formula,1,index-1)+getprice(copy(formula,index+1,j-index-1),County,currentstr)+copy(formula,j+1,length(formula)-j); 
                        index:=pos( '{ ',formula); 
                        Close; 
                        SQL.Clear; 
                        SQL.Add( 'update Monthprice set  '+County+ '= '+formula+ ' where 库编码= '+QuotedStr(code)); 
                        Execute(0); 
                       end; 
                  end; 
               end; 
            end; 
          next; 
         end; 
    end; 我在界面上放了一个daodatabase1 一个daoquery1 一个daoquery2 都指向database1 程序改为上面那样不动态创建
    还是会出现标题那个地址错误而改用
    DAOQuery2.Create(Application);    //动态建立一个query  
    //这句应该这样写 
    DAOQuery2:=TDAoquery.Create( Owner );  
    try 
      .... 
    finally 
      DAOQuery2.free; 
    end; 
    这样就行,不会出现错误还有要把创建过程放到循环外来做要怎么做?
    try 
    ...
    finally
    这是没办法搬到循环外的
      

  12.   

    安装一个EurekaLog,启用后,你在发生AV时,就可以知道错误在哪一行源代码了。