# If statement in Field Calculator

7962
6
05-07-2013 07:25 AM
New Contributor
Hello,

I am having problems with if statements in the Field calculator. This is my first time trying to code something in the field calculator. I am familiar with coding as I have used java and visual basic before. I created contours and I have a field that is set to 1 for all contours that are not an interval of 5 and set to 2 for all 5 foot contours. I have created another Field called Layer that I want to populate with a string for Minor and a different string for major contours. Can anyone help with this?
Tags (3)
1 Solution

Accepted Solutions
Esri Esteemed Contributor
You can achieve this using python.  Here is an example:

[ATTACH=CONFIG]24101[/ATTACH]
6 Replies
Esri Esteemed Contributor
Hi Greg,

What constitutes the Layer field to have the Minor string, and Major string?  If the other field is 1, should the Layer field have a value of 'Minor'??
New Contributor
Yes I am trying to set the Layer field to 'Minor' if the other field = 1 and set the layer field to 'Major' if the other field = 2
Esri Esteemed Contributor
You can achieve this using python.  Here is an example:

[ATTACH=CONFIG]24101[/ATTACH]
MVP Honored Contributor
In VB Script you would do the following:

Parser: VB Script

Use Codeblock: checked

Codeblock:
```If [Other_Field] = 1 Then
Output = "Minor"
ElseIf [Other_Field] = 2 Then
Output = "Major"
Else
Output = [Layer] '  Use the existing value of the field being calculated so that it won't change.
End If```

expression: Output
New Contributor

I've been on a 2 hour search for the right code to do my basic If/Then Statement and yours was the only one that worked, thank you!!

MVP Honored Contributor

Johanna: