1

Can anybody help me with this VB Script code for the field calculator in ArcGIS 10.2? Why in the final result are all the values 10?

a= 10
Select Case UCase( [PRIORITY] )
 Case "AUX 1": a = 22.5
 Case "AUX2": a = 25
 Case "PRIMARY": a = 17.5
 Case "AUX 2": a = 25
 Case "AUX 3": a = 30
 Case "AUX 4": a = 32.5
 Case "NA" : a = 5
 Case "N/A": a = 5
 Case "SECONDARY": a = 15
 Case "SECONDARY2": a = 15
 Case "Tertiary": a = 10
 Case "MALL": a = 27.5
End Select
  • 1
    Tertiary must be TERTIARY for that value to actually be used as a Case value. In case none of the values match your cases, you should add as the last case - Case Else: a = -1 so you can distinguish it from TERTIARY (unless you want that assumed for Null values and other words) Then you would need to find out what was actually in the Priority field. I would do that rather than set "a" up front. – Richard Fairhurst Jun 27 '15 at 23:33
  • 1
    I tried your code and it is working fine with given mock entries. The reason either your PRIORITY field does not have these values (obviously it returns 10 due to initial assignment not to Tertiary entry as explained by others) or might be whitespaces in the entries, which can be fixed by changing UCase( [PRIORITY] ) into Trim( UCase( [PRIORITY] )). In all cases it is the best practice to catch NOT IN CASE BLOCK values as suggested (i.e., a=-1 either initial assignment or in an else case). – fatih_dur Jun 28 '15 at 3:20
1

I would assume you're using this in the advanced field calculator (show codeblock)... In this case "Tertiary" would never be hit as you're getting the select case of the uppercase and that word contains lower case characters.

In this case a=10 at the start and none of the Case in the block are ever hit.

As to why none of these cases are ever hit depends on your data. As fatih_dir indicated you could have leading/trailing whitespace in your values, to try to overcome this use Trim:

a= 10
Select Case UCase(Trim([PRIORITY]))
 Case "AUX 1": a = 22.5
 Case "AUX2": a = 25
 Case "PRIMARY": a = 17.5
 Case "AUX 2": a = 25
 Case "AUX 3": a = 30
 Case "AUX 4": a = 32.5
 Case "NA" : a = 5
 Case "N/A": a = 5
 Case "SECONDARY": a = 15
 Case "SECONDARY2": a = 15
 Case "Tertiary": a = 10
 Case "MALL": a = 27.5
End Select

Note this will do nothing for Tab, Return etc.. again you can compensate for this by using Replace (I prefer to do it iteratively) vbTab, vbNewline with an empty string:

EmptyString = ""
CaseStr = UCase(Trim([PRIORITY]))
CaseStr = Replace(CaseStr,vbTab,EmptyString)
CaseStr = Replace(CaseStr,vbNewline,EmptyString)
Select Case CaseStr 
 Case "AUX 1": a = 22.5
 Case "AUX2": a = 25
 Case "PRIMARY": a = 17.5
 Case "AUX 2": a = 25
 Case "AUX 3": a = 30
 Case "AUX 4": a = 32.5
 Case "NA" : a = 5
 Case "N/A": a = 5
 Case "SECONDARY": a = 15
 Case "SECONDARY2": a = 15
 Case "Tertiary": a = 10
 Case "MALL": a = 27.5
End Select

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.