1

I am trying to use one field's contents ([Condition]) as a condition for populating another field ([Intact]). I try to run the following code in the Field Calculator, but it never works (and is not very helpful to help me understand why, giving me the "999999" error code).

This is what went into the "Pre-Logic Script Code" box:

    Dim x
    If [Condition] = "Very good" Then
      x = 1
    elseif [Condition] = "Reasonably good" Then
      x = 1
    elseif [Condition] = "Poor" Then
      x = 0
    elseif [Condition] = "Renovated" Then
      x = 1
    elseif [Condition] = "Very poor" Then
      x = 0
    elseif [Condition] = "Good" Then
      x = 1
    else 
      x = 0
    end if

And simply x went into the "Intact =" box.

Is there any idea why this code does not work? Again, I'm trying to populate the "Intact" field with 0s or 1s depending on the value of the "Condition" field in the same feature class.

2

You cannot assign an output directly to a field in the calculation text, only by selecting the field as the output field of the calculation. [Intact] needs to be changed to Intact (a variable) and the output needs to be to Intact in the field output portion.

Parser: VB Script

Use Codeblock: Checked

Pre-Logic Codeblock:

If IsNull([Condition]) Then
  Intact = 0
elseif [Condition] = "Very good" Then
  Intact = 1
elseif [Condition] = "Reasonably good" Then
  Intact = 1
elseif [Condition] = "Poor" Then
  Intact = 0
elseif [Condition] = "Renovated" Then
  Intact = 1
elseif [Condition] = "Very poor" Then
  Intact = 0
elseif [Condition] = "Good" Then
  Intact = 1
else 
  Intact = 0
end if

Intact Field: Intact

If this still produces an error try the Select Case syntax instead (it is much more efficient):

Pre-Logic Codeblock:

If IsNull([Condition]) Then
  Intact = 0
Else
  Select Case [Condition]
    Case "Very good", "Reasonably good", "Renovated", "Good"
      Intact = 1
    Case "Poor", "Very poor"
      Intact = 0
    Case Else
      Intact = 0
  End Select
End If
  • Still didn't work. How many spaces to I have to indent the "Intact" after the elseif line? – Mhoram Dec 5 '14 at 17:13
  • VB does not care about white space or indents. You may have Null values in the Condition field, which would cause an error. I have corrected the calculation to take that possibility into consideration. – Richard Fairhurst Dec 5 '14 at 17:16
  • Thanks! The code worked as Richard Fairhurst suggested. The reason for the initial failure was an accidental extra skipped line in the bottom box before the lonely x there. – Mhoram Dec 5 '14 at 17:22
1

I am assuming you are running this as a field calculate from a table? You don't reference the field you want to change in the pre-logic script section, you apply a variable to it which you compute a value for (in your case a 1 or a 0).

Add Dim x to the beginning of the code in the pre-logic script code box then replace all "[Intact] = " with "x = "

As you are running this field calculate on the intact field then type in the bottom box just x.

  • We posted at about the same time and gave essentially the same answer, so I awarded your answer a point as well. – Richard Fairhurst Dec 5 '14 at 17:13
  • I've edited my original post, showing my new code (which still doesn't work). Still not sure what's going on with it. – Mhoram Dec 5 '14 at 17:16

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.