Select by Location + Field Calculator

09-26-2011 05:32 AM
New Contributor III
Hello all.  I'm trying to improve on my automation skills but have hit a road block.  I'm trying to update features in a specific area and replace all SLOPE values of 999 with 0.

I am using model builder and can successfully run select by location to identify the drain features within the defined boundary polygon.  However, I am running into problems using Field Calculator to replace the attributes of those specific drains.

For VB I was using


and I receive a VBA Error code saying "Expected ')'" End of statement as I understand?

I've also tried running it through a python script I had found and tried:

That is returning a syntax error.

I am new to running code and am just jumping in but this would help tremendously to other field classes where I have been stuck using Find&Replace.

Thank you
0 Kudos
3 Replies
New Contributor III
Hi Nick,

I think the problem here is that your values might be stored as numbers.
!SLOPE!.replace('999', '0') 

would work perfectly well with strings. The thing is, replace is a string method and so needs an input that is such.

Your easiest (though sloppiest IMO) option would be to do something like:

which converts your number to a string, executes the replace method, and then converts the result back into an integer (or float, or whatever you need.)

Something more structured that would allow more flexibility in your logic would involve using the Codeblock in the Field Calculator, where the Code Block would be:
def quick_check(val):
    if val == 999:
        return 0
    return val

and your main expression would be

In this way, you're passing the value in SLOPE to the function above, which returns 0 if it's 999, or the regular value if it's not.

Hope this helps!
0 Kudos
New Contributor III
What would be the VB script to select and replace the integer values?
0 Kudos
New Contributor III
I'm asking because I keep getting an improper use of Null error when trying to populate the field
0 Kudos