公式并不是“根据需要”求值或者调用(即,一个公式无法调用另一个公式来执行某些处理,即使是使用全局变量)。
例如:
Rem Formula PrintSquare Global x As Number x = 5 formula = "The square of " & CStr(x,0) & " is " & ToText ({@square <mailto:{@square>}, 0) & "." Rem Formula Square Global x As Number formula = x * x
在报表中使用公式 PrintSquare,您得到:
The square of 5 is 0.
在此示例中,公式 PrintSquare 使用公式 Square,因此 Crystal Reports 按顺序对它们进行求值。首先,对 Square 求值,然后再对 PrintSquare 求值。当对 Square 求值时,全局变量 x 的值为 0,因为这是未初始化的 Number 变量的值。
相反,自定义函数则根据需要求值或者调用。在上一个示例中使用自定义函数:
Rem Formula PrintSquare formula = "The square of " & CStr (5,0) & " is " & CStr(cf9Square (5),0) + "." Rem Custom Function cf9Square Function cf9Square (x As Number) cf9Square = x * x End Function
现在,如果您在报表中使用公式 PrintSquare,您将得到:
The square of 5 is 25.