刚学用 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 ,才可以用啊。在线等..........
在 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 ,才可以用啊。在线等..........
[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'等等的错误提示
TForm1.
这个函数你可以在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;
------------------------------------
三楼写的和我一样在private里面声明的像TForm1类里面的一个方法,而在外面在 implementation 的前面声明的一般是一些通用函数,比如像刚开始学程序时写的求两个数的乘积之类的函数,因为你写的过程中要访问TForm1类中定义的内容,所以应当声明为TForm1类里面的成员函数
前面加一个form1
function Form1.Sum_Page():Integer
你定义的函数是普通方法 却调用了tform的类成员adoquery,当然会报错拉。
把你要定义的方法定义成tform的类方法就ok拉