Question on VB functions

  • Thread starter Thread starter James B
  • Start date Start date
J

James B

I'm having trouble with the user defined function in
access. I created a sumit function in modules and it
suppose to return the sum to the call variable. But some
how, I'm getting zero.

The code is:

In the module, I have this function

Public Function sumit(a as Long, b as Long)

sumit = a + b

end function

In one of the text box in the form I'm calling the sumit
function

..
..
..
Dim a as Long
Dim b as Long

a = 2
b = 3

Me.txtcal=sumit(a,b)

..
..
The function sumit did not return the correct answer and
Me.txtcal displays a zero. Help.......

Thanks
J.B.
 
James,

When you declare a function, you need to declare its return type. Perhaps
that is what is causing this failure. Try declaring your function as
follows and see if that helps:

Public Function sumit(a as Long, b as Long) as Long

hth,
 
Cheryl said:
When you declare a function, you need to declare its return type. Perhaps
that is what is causing this failure. Try declaring your function as
follows and see if that helps:

Public Function sumit(a as Long, b as Long) as Long

That's not really necessary Cheryl. If the type of a
variable or function is not explicitly declared, Access
defaults to type Variant, which should work in this case.
 
James said:
I'm having trouble with the user defined function in
access. I created a sumit function in modules and it
suppose to return the sum to the call variable. But some
how, I'm getting zero.

The code is:

In the module, I have this function

Public Function sumit(a as Long, b as Long)

sumit = a + b

end function

In one of the text box in the form I'm calling the sumit
function
.
Dim a as Long
Dim b as Long

a = 2
b = 3

Me.txtcal=sumit(a,b)

.
.
The function sumit did not return the correct answer and
Me.txtcal displays a zero. Help......


Somehow, I'm leaning towards this being a scoping issue. Do
you have:
Option Explicit
in every one of your modules? If not, add it at the top of
the modules and see if Compile and Save All Modules (Debug
menu) finds any errors.
 
I agree. If not, Me.textcalc = a + b would make a lot more sense.

Jake

----- TC wrote: -----

My guess is that we are simply not seeing the code that he is actually
running :-)

TC


Marshall Barton said:
James said:
access. I created a sumit function in modules and it
suppose to return the sum to the call variable. But some
how, I'm getting zero.
function
.
Dim a as Long
Dim b as Long
.
The function sumit did not return the correct answer and
Me.txtcal displays a zero. Help......
you have:
Option Explicit
in every one of your modules? If not, add it at the top of
the modules and see if Compile and Save All Modules (Debug
menu) finds any errors.
Marsh
MVP [MS Access]
 
Back
Top