Retuning null from a function? - Douglas J. Steele |
28-Sep-08 11:19:23
|
The only data type that can handle Null is Variant, so you'd have to change
your declaration to
Public function GradeComp(G1 as string, G2 as string) as Variant
Assuming you have a ValidGrade function that returns True or False, you
could then use:
Public function GradeComp(G1 as string, G2 as string) as integer
Dim G1value as integer
Dim G2value as integer
If ValidGrade(G1) And ValidGrade (G2) Then
G1Value = GradeToInt(G1)
G2Value = GradeToInt(G2)
GradeComp = G2Value - G1Value
Else
GradeComp = Null
End If
End Function
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please) |
 |
| |
Retuning null from a function? - John Spencer |
28-Sep-08 03:48:47
|
IF the grades you are passing in can be null then you need to use
Variants for G1 and G2. If you don't passing in Null will error.
If you want to return either an integer or Null you have to define the
result as null. Again if you try to set a string variable to null you
will get an error.
That said you should be able to test G1 and G2 to see if they meet the
specified values and then execute your code or set the return value to null.
Public function GradeComp(G1 as Variant, G2 as Variant) as Variant
Dim G1value as integer
Dim G2value as integer
If G1 >= "A" and G1 <= "G" and G2 >= "A" and G2 <= "G" THEN
G1Value = GradeToInt(G1) ' use the UDF GradeToInt function
G2Value = GradeToInt(G2) ditto
GradeComp = G2Value - G1Value 'return the difference in points
Else
GradeComp = Null
End If
end function
'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'==================================================== |
 |
| |
Retuning null from a function? - Howard |
29-Sep-08 02:02:15
|
Thank you Douglas, just what I neded
Howard |
 |
| |
Retuning null from a function? - Howard |
29-Sep-08 02:03:20
|
Thanks John, I can see why I need to protect my GradeToInt function as well
Howard |
 |
| |