error in function when field is null (1 Viewer)

Leo_Polla_Psemata

Registered User.
Local time
Yesterday, 21:45
Joined
Mar 24, 2014
Messages
364
Hi
I am trying to create the below function.
I managed to bring the correct result, however, the field EDF, might contain data or zero or empty.
While with data or zero, i get the result i want, when empty, the function returns error.
Is there any way i could work it around ?

Code:
Public Function DemrCalc(DISF As Date, INME As Date, EDF As Long, sztp As String) As String

I have tried this but still i get error if EDF is empty
Code:
            If IsEmpty(EDF) Or EDF = 0 Then
                PA = 12
            Else
                PA = 12 + EDF
            End If
 

Josef P.

Well-known member
Local time
Today, 06:45
Joined
Feb 2, 2023
Messages
832
Code:
Public Function DemrCalc(DISF As Date, INME As Date, EDF As Long, sztp As String) As String
You cannot pass Null to a long parameter.

Variant 1: Variant instead of Long
Code:
Public Function DemrCalc(DISF As Date, INME As Date, EDF As Variant, sztp As String) As String
...
     if isnull(EDF) ....
     ' or
     PA = 12 + Nz(EDF, 0)
...

Variant 2: use Nz-Function when calling the function
Code:
Public Function DemrCalc(DISF As Date, INME As Date, EDF As Long, sztp As String) As String
...
'with call
DemrCalc DISFvalue, INMEValue, Nz(EDFvalue, 0), ...

BTW: Unless absolutely necessary, I would use ByVal instead of ByRef for the parameters.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:45
Joined
Feb 19, 2002
Messages
43,352
ByVal doesn't help.
To have an optional argument, you must define the argument as optional. Defining it as a variant rather than long might work and allow you to pass a null.
 

Josef P.

Well-known member
Local time
Today, 06:45
Joined
Feb 2, 2023
Messages
832
ByVal doesn't help.
? Where does it say that ByVal should allow Null?

I suspect a similar call like that:
Code:
with aRecordsetWithFieldsForDemrCalc
...
      DemrCalc !DISF.Value, !INME.Value, !EDF.Value, !sztp.Value
....
end with
If !EDF.Value is null, the parameter with type Long will not work.
=>
variant 1 from #2: parameter: EDF As Variant + Nz(EDF, 0) or check with Isnull(EDF) inside function
or with variant 2 from #2:
Code:
with aRecordsetWithFieldsForDemrCalc
...
      DemrCalc !DISF.Value, !INME.Value, Nz(!EDF.Value, 0), !sztp.Value
....
end with
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:45
Joined
Jan 20, 2009
Messages
12,853
Personally I would not use a UDF for such a trivial operation. It will slow down the query which would otherwise be processed by the engine.

use Nz-Function when calling the function
For the same reason it is far better to use Is Not Null than wrapping the argument in NZ() when calling a function that will not accept a Null.
 

Users who are viewing this thread

Top Bottom