Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

The solution in VB for the base process is @ "If then" VB Script code (or Python) equivalent to SQL's IN ('x','y',z') expression in ArcGIS Field Calculator question? works great but I was trying to nest 'if' statements and can't.

So pre-logic code of

if [grid_code] = 1 then 
f2= 2
end if
if [grid_code] = 2 then
f2=3.5
end if

...etc. but this doesn't work? what am I doing wrong?

I tried the second 'if' before the 'end if' and also tried 'elif' but these give script errors.

Also any idea why I get a warning message about "The calculated value is invalid for the row with objectID=1" and when you click click 'yes' to continue it works fine? Can I suppress this?

Best,

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

For VB, something like this should work, I think:

Pre-logic script code:

Dim f2 As Double
If [grid_code] = 1 Then
  f2 = 2
ElseIf [grid_code] = 2 Then
  f2 = 3.5
End If

asp_code =

f2

In Python, I think it would be something like this (though I don't have ArcGIS 10 so I can't check):

Pre-logic script code:

def my_func(my_field):
    if my_field == 1:
        f2 = 2
    elif my_field == 2:
        f2 = 3.5
    return f2

asp_code =

my_func(!grid_code!)

The error message saying that the "calculated value is invalid" is a bit strange. Perhaps double-check that your asp_code field is of float or double data type (not integer), and that you have all of the options in the grid_code field covered by your if statements? Sorry if that's too obvious, but it's all I can think of at the moment. Good luck!

share|improve this answer
    
+1 great answers. I'd lean towards using the Python solution, since it has more of a future than VBA. –  Mike T Oct 27 '11 at 19:32
    
Thanks James - the python code was almost perfect. I had to include f2 in the asp_code = re_score(!VEGTYPE_NU!,f2) and f2=99 def re_score(my_field,f2): in the pre-logic ... anyway it Runs in less that a minute with 45 my_field values in a dataset with 31,000 polygons. –  GeorgeC Nov 4 '11 at 5:42
add comment

For Python syntax, I would suggest that you would want something like this with nested if ... elif statements. As for the [grid_code] does it need to be enclosed in brackets? or is it a field? if it is a field, then I suspect that replacing [] with double quotes would do the trick

...you need a def (aka function call name passing a field name to the following
if [grid_code] == 1: 
  f2= 2
elif [grid_code] == 2:
  f2=3.5
return f2
share|improve this answer
    
sorry I should have added the first line is "dim f2"...in VB. Using python is fine as well. Basically 'grid_code' is a field in the table and the f2 values get placed in another field called 'asp_code'. Maybe what I missed is the 'return f2'. Will try and see... –  GeorgeC Oct 26 '11 at 23:13
    
also what do you mean by needing a def? is it just def (f2, grid_code) as the first line? I can't try this now as I am away from my main desktop. best, –  GeorgeC Oct 27 '11 at 0:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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