Testing for Nulls fails

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a table where some of the values of MyField are null.

I am creating a function that depends on the value of Myfield which I pass
to a variable varMyField and am testing it with If IsNull(varMyVariable) but
it errors. Have tried setting the variable as a variant but no difference.

Function Test(varMyField as variant)

If isnull(varMyField) then
Dosomething,
Else
Dosomething Else
End if

End function

Any suggestions please?
 
It would help to know the error message produced. The code looks fine and
IsNull works with a variant. The only coding error I can see is that you
have declared the procedure as a function but have not specified what data
type is returned. If you do not intend to return a value from the procedure,
you should change it to a Sub.

Hope This Helps
Gerald Stanley MCSD
 
Gerald Stanley said:
The only coding error I can
see is that you have declared the procedure as a function but have
not specified what data type is returned. If you do not intend to
return a value from the procedure, you should change it to a Sub.

I'd take issue with the assertion that this is a coding error. First,
the default return type for a function is Variant, so it is not
syntactically required to specify the function's type. Second, in the
context of Access programming it can be very useful on occasion to
define a procedure as a Function instead of a Sub, even though you don't
intend to pass any return value. Doing so allows you to invoke the
procedure as a function expression in an event property line, for
example, which makes it a lot easier to set a common procedure to be
invoked by events in many different controls, without having to build a
separate event procedure for each control, just to call the common Sub.

So while it may well be that Snowsride should be using a Sub in this
case, it may also be that there's a valid reason for creating the proc
as a Function.
 
Snowsride said:
I have a table where some of the values of MyField are null.

I am creating a function that depends on the value of Myfield which I
pass to a variable varMyField and am testing it with If
IsNull(varMyVariable) but it errors. Have tried setting the variable
as a variant but no difference.

Function Test(varMyField as variant)

If isnull(varMyField) then
Dosomething,
Else
Dosomething Else
End if

End function

Any suggestions please?

There's nothing obviously wrong with the pseudocode you posted. Please
post the real code, the error message and number, and the calling code
or expression.
 
Apologies to all - I had not set the type to variant for one variable!

Thanks for the feedback

Snowsride
 
Back
Top