开发者

function return 0 value in Ms Access

I created a function in Ms Access and called it to the sub procedure in the form, but it returns 0. This is the code in the function:

Public Function Sum(a, b) As Double
    Dim total
    total = a + b
End Function

The code in the sub procedure in the form is:

Private Sub cmdDisplay_Click()
    Dim a As Double
    Dim b As Double开发者_开发知识库

    a = Val(Text0)
    b = Val(Text2)

    MsgBox (Sum(a, b))
End Sub

it displays 0 in every time I tested the button which it should have been added a and b together. Please help


To return a value you must assign to the function name, which behaves just like a local variable typed to the functions return type;

Public Function Sum(a, b) As Double
    Dim total
    total = a + b
    Sum = total '//sum is the function name and a variable of type double
End Function

or better (if you really need a sum function):

Public Function Sum(a as double, b as double) As Double
    Sum = a + b
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜