Function returning missing value

Function returning missing value

Post by Bjn Helge » Sat, 01 Nov 2003 18:39:33


Hi! I have a function that is computing a sum under different conditions:
----------------------
Private Function Function1() As Integer

'Dim statements

If Function2 = 0 Then
'Calls Function2 and computes an expression (sum)
Function1=....
.......

ElseIf Function2 = 1 Then
'Calls Function2 and computes a different expression
Function1=....
.......
ElseIf Function2 >1 Then
'Calls Function2 and Function1 should return no value
......
End If
End Function
-----------------------
If I set
Function1 = vbEmpty the value 0 is returned, but I want a missing value to
be returned to a (numerical) field in a table. How do I accomplish this?

Thanks for any help!

Bjorn
 
 
 

Function returning missing value

Post by Rick Roths » Sat, 01 Nov 2003 23:44:28

> Private Function Function1() As Integer

What do you mean "If I set Function1 = vbEmpty the value 0 is returned"?
Function1 is declared as a function... you don't assign values to functions,
they return values to you. Although your declaration doesn't show one, are
you perhaps referring to passing an argument to a function (that is not done
using an equal sign though)? I would note that whatever you meant
originally, Function1 is declared as returning an Integer... and that's what
it will do (unless it triggers an error instead).

Rick - MVP

 
 
 

Function returning missing value

Post by Phill. » Tue, 04 Nov 2003 21:34:54


. . .
. . .

Bjn,

The value vbEmpty /only/ applies to Variants.

If you really need to return an "Empty" value, change the function to
one, as in

Private Function iFunction1() As Variant ' Integer

HTH,
Phill W.
 
 
 

Function returning missing value

Post by Rick Roths » Tue, 04 Nov 2003 22:38:02

> > Private Function Function1() As Integer
to
functions,
done
what

Duh! You are **returning** a value from a function... Wow! Did I ever
misread your question originally or what! Okay, forget the part about not
assigning values to functions; but the part about the function's declaration
still holds... you declared the function as an Integer... it's going to
return an Integer. When the function is created, its name acts like a
variable; a memory location is chosen for returning the function's value in
an it is initialized just like a variable it... Integers are initialized to
0 (as are other numbers, Strings are initialized to ""). If you can't find a
suitable Integer value to use as "empty" (say -1, or maybe -32767), then
you'll have to declare you function as Phil indicated... a Variant.

Rick - MVP
 
 
 

Function returning missing value

Post by Bjn Helge » Wed, 05 Nov 2003 18:20:41

Phill and Rick: Thank you for the responses! I'm a beginner in VB
programming, and your responses was helpful.

Bjn
"Phill. W" < XXXX@XXXXX.COM > skrev i melding
 
 
 

Function returning missing value

Post by asperamanc » Thu, 06 Nov 2003 00:00:05

>

In addition, vbEmpty is only a constant that literally contains '0'
If you need to return 'Empty', write it

Function1 = Empty 'not vbEmpty

When you want to check the return value, write it like this:

If (IsEmpty(myReturnValue)) Then
...

Of course, in this case you have to declare myReturnValue as Variant
as well, otherwise VB will implicitly convert 'Empty' to zero or false

A completely different way to do it would be to allow Function1 to
return more than one variable. One would be for the actual calculation
result, the other would give status information (e.g. calculation
succeeded)
You could accomplish that by adding a 'ByRef' parameter to Function1,
which you set to the appropriate value inside the function, then check
later on.

Hope that helps

Robert
 
 
 

Function returning missing value

Post by norepl » Thu, 06 Nov 2003 08:56:15

A minor suggestion

If Function2 = 0 Then
...
ElseIf Function2 = 1
...


This will call Function 2 multiple times. I'm not sure if this is what
you are trying to achieve, but it might be better to do

vntAnswer = Function2
If vntAnswer = 0 Then

elseif vntAnswer = 1 then
...
 
 
 

Function returning missing value

Post by Bjn Helge » Tue, 11 Nov 2003 17:00:54

Samuel, I agree with you that this is a more effective way of programming! I
have no need to call the function more than once.

"Samuel Hon" < XXXX@XXXXX.COM > skrev i melding



conditions:
to