[GIS] Using field calculator if statement to update another field using another field’s values

arcgis-desktopfield-calculatorif statementvbscript

Using VBA in field calculator I am trying to figure out the expression that if 3 conditions are met populate a field using another field's value and I cant get it to work! CALC_CLASS is a joined table and SPEED LIMIT is a Double type, MMS_TEMP is text.

enter image description here

I am using VBA and ARC 10.3

if [SPEED_LIMIT]=>91 and [SPEED_LIMIT] <= 100 and [MMS_TEMP]="53,000 or more" then
RD_BASELINE.CLASS_MMS= [CALC_CLASS_MMS.PSL_91_100]

elseif [SPEED_LIMIT]=>51 and [SPEED_LIMIT] <= 60 and [MMS_TEMP]="53,000 or more" then 
RD_BASELINE.CLASS_MMS= [CALC_CLASS_MMS.PSL_51_60]

else
RD_BASELINE.CLASS_MMS=[CALC_CLASS_MMS.PSL_1_40]                 

end if**                

Best Answer

I think what is happening here is that you have entered all your worflow in the pre-logic script and then told the calculator that RD_BASELINE.CLASS_MMS=RD_BASELINE.CLASS_MMS, which doesn't mean anything. This pre-logic script is run BEFORE the operation takes place. To make this work, you need to define a function in the pre-logic script code, and then call that function using the expression. I don't code much in VB, but in python, I would try:

def calculate_answer(speed_limit, mms_temp, psl_91_100, psl_51_60, psl_1_40):
    if speed_limit >= 91 and speed_limit <= 100 and mms_temp == "53,000 or more":
        out = psl_91_100
    elif speed_limit >= 51 and speed_limit <= 60 and mms_temp =="53,000 or more":
        out = psl_51_60
    else:
        out = psl_1_40 
    return out

Then, in the expression field, just enter:

calculate_answer([SPEED_LIMIT], [MMS_TEMP], [CALC_CLASS_MMS.PSL_91_100],  [CALC_CLASS_MMS.PSL_51_60], [CALC_CLASS_MMS.PSL_1_40])

You can find lots VB examples for the field calculator here if you want to stick to that language.

Related Question