Saturday, 12 November 2016

arcgis desktop - Handling NULL values when using CInt?


This expression works perfectly when integer fields are 0 instead of NULL but now I am working with a GDB that has many NULL values. I'm not sure how to force CInt to only run if field is not null.


Error message


Function FindLabel ( [SQFT_CONC] , [DEPTH_CONC] , [SQFT_ASPHT] , [DEPTH_ASHT]  )
Dim strInput
Dim dc
Dim da
dc = CInt([DEPTH_CONC])

da = CInt([DEPTH_ASHT])
val = dc + da
if (val = 10) then
strInput = "IT WORKS"
end if

FindLabel = strInput
End Function

Answer



I found the correct way do handle this. If the field is Null then it will count as a 0. This first way is shorter although I'm not sure if it will give the desired result in every scenario.



Function FindLabel ( [SQFT_CONC] , [DEPTH_CONC] , [SQFT_ASPHT] , [DEPTH_ASHT]  )
Dim strInput
Dim dc
Dim da
dc = CInt(0 & [DEPTH_CONC])
da = CInt(0 & [DEPTH_ASHT])
val = dc + da
if (val = 10) then
strInput = "IT WORKS"
end if


FindLabel = strInput
End Function

Alternatively....


To treat Null values as 0 check for Null and substitute a 0 into the variable.


Function FindLabel ( [SQFT_CONC] , [DEPTH_CONC] , [SQFT_ASPHT] , [DEPTH_ASHT]  )
Dim strInput
Dim dc
Dim da

if isNull([DEPTH_CONC]) then
dc = 0
else
dc = CInt([DEPTH_CONC])
end if
if isNull([DEPTH_ASHT]) then
da = 0
else
da = CInt([DEPTH_ASHT])
end if

val = dc + da
if (val = 10) then
strInput = "IT WORKS"
end if

FindLabel = strInput
End Function

To ignore records completely, step over them in an if statement if either value is Null


Function FindLabel ( [SQFT_CONC] , [DEPTH_CONC] , [SQFT_ASPHT] , [DEPTH_ASHT]  )

Dim strInput
Dim dc
Dim da
if not isNull([DEPTH_CONC]) and not isNull([DEPTH_ASHT]) then
dc = CInt([DEPTH_CONC])
da = CInt([DEPTH_ASHT])
val = dc + da
if (val = 10) then
strInput = "IT WORKS"
end if

end if
FindLabel = strInput
End Function

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...