Reputation: 9713
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
Upvotes: 3
Views: 10962
Reputation: 175748
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
Upvotes: 5