刚学用 delphi 和 sql server 联系在一个 form 中,有 adoquery1 的组件。
在 form 刚被执行时,需要统计一下文件的份数(select count(fs) as fs form PrintT).当用户新增一笔资料时,也需要再统计一次文件的份数,让一个 label 显示出来 。因此,我写了个 function ,以此来简化程序。可总出现错误:function Sum_Page():Integer; //自定义 函数implementation{$R *.dfm}function Sum_Page():Integer;
begin
  with adoquery1 do
    begin
      Close;
      sql.Clear ;
      sql.Add('select count(fs) as fs from PrintT');
      Open;
      result:=StrToInt(fields[0].Text) ;
    end;
end;请问,我是不是要在 function 中先定义一下 adoquery1 ,才可以用啊。在线等..........

解决方案 »

  1.   

    谢谢 2 楼不过,不定义的话,就会出现 如:
      [Error] PrintT.pas(54): Undeclared identifier: 'adoquery1'
      [Error] PrintT.pas(56): '(' expected but ';' found
      [Error] PrintT.pas(57): Undeclared identifier: 'sql'
      [Error] PrintT.pas(58): Missing operator or semicolon
      [Error] PrintT.pas(59): Undeclared identifier: 'Open'
      [Error] PrintT.pas(60): Undeclared identifier: 'fields'
      [Error] PrintT.pas(60): '(' expected but ')' found
      [Error] PrintT.pas(61): Statement expected, but expression of type 'Text' found
      [Fatal Error] Print.dpr(5): Could not compile used unit 'PrintT.pas'等等的错误提示
      

  2.   

    function TForm1.Sum_Page():Integer;
    TForm1.
      

  3.   

    这样用不可以
    这个函数你可以在TForm1声明里面private里面声明下面实现用function TForm1.Sum_Page():Integer;
    begin
      with adoquery1 do
        begin
          Close;
          sql.Clear ;
          sql.Add('select count(fs) as fs from PrintT');
          Open;
          result:=StrToInt(fields[0].Text) ;
        end;
    end;
      

  4.   

    你不是已经在窗体上放了一个 adoquery控件了?不放就先定义一下.
      

  5.   

    谢谢 dreamover(梦醒了) 我不清楚 3 楼的写法是不是和 dreamover(梦醒了)  写法一致,但我表示万分感谢。另,我想知道,哪些自定义的 函数 或 过程 应写在 implementation 的前面,哪些应写在 TForm1声明里面private里面声明 里?
      

  6.   

    另,我想知道,哪些自定义的 函数 或 过程 应写在 implementation 的前面,哪些应写在 TForm1声明里面private里面声明 里?
    ------------------------------------
    三楼写的和我一样在private里面声明的像TForm1类里面的一个方法,而在外面在 implementation 的前面声明的一般是一些通用函数,比如像刚开始学程序时写的求两个数的乘积之类的函数,因为你写的过程中要访问TForm1类中定义的内容,所以应当声明为TForm1类里面的成员函数
      

  7.   

    因为你引用了form的控件所以要在function Sum_Page():Integer
    前面加一个form1
    function Form1.Sum_Page():Integer
      

  8.   

    easy
    你定义的函数是普通方法 却调用了tform的类成员adoquery,当然会报错拉。
    把你要定义的方法定义成tform的类方法就ok拉