函数在Ms Access中返回0值

时间:2011-06-22 10:27:50

标签: ms-access vba

我在Ms Access中创建了一个函数,并将其调用到表单中的子过程,但它返回0。 这是函数中的代码:

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

表单中子过程中的代码是:

Private Sub cmdDisplay_Click()
    Dim a As Double
    Dim b As Double

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

    MsgBox (Sum(a, b))
End Sub

每次我测试按钮时都会显示0,它应该一起添加a和b。请帮忙

1 个答案:

答案 0 :(得分:5)

要返回一个值,必须将其赋值给函数名,其行为就像键入函数返回类型的局部变量一样;

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

或更好(如果你真的需要和函数):

Public Function Sum(a as double, b as double) As Double
    Sum = a + b
End Function
相关问题