<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic 'If...Then' Code for Field Calculator in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697136#M23108</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I completed a Model that overlays several polygon layers, clips out a specified section, dissolves according to certain attributes and sums the acreage.&amp;nbsp; What I want to do is add another Calculate Field entry into my Model, but I can't get the Field Calculation correct.&amp;nbsp; The Fields I want to select from are [CAUVLCType] and [MUSYM].&amp;nbsp; The fields are coded-value domains and descriptions.&amp;nbsp; What I've tried is similar to Label expressions:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Function&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if ([CAUVLCType] = 'CROP' AND [MUSYM] = 'NpA') then&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[Soil_Acreage] * 460&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;elseif ([CAUVLCType] = 'WOOD' AND [MUSYM] = 'NpA') then&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[Soil_Acreage] * 100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;End if&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;End Function&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can someone provide me a couple lines of code that will work on a copy/paste basis which I can repeat and enter different field descriptions as needed?!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 14 Nov 2011 18:37:32 GMT</pubDate>
    <dc:creator>JeffreyNemecek</dc:creator>
    <dc:date>2011-11-14T18:37:32Z</dc:date>
    <item>
      <title>'If...Then' Code for Field Calculator</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697136#M23108</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I completed a Model that overlays several polygon layers, clips out a specified section, dissolves according to certain attributes and sums the acreage.&amp;nbsp; What I want to do is add another Calculate Field entry into my Model, but I can't get the Field Calculation correct.&amp;nbsp; The Fields I want to select from are [CAUVLCType] and [MUSYM].&amp;nbsp; The fields are coded-value domains and descriptions.&amp;nbsp; What I've tried is similar to Label expressions:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Function&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if ([CAUVLCType] = 'CROP' AND [MUSYM] = 'NpA') then&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[Soil_Acreage] * 460&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;elseif ([CAUVLCType] = 'WOOD' AND [MUSYM] = 'NpA') then&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[Soil_Acreage] * 100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;End if&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;End Function&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can someone provide me a couple lines of code that will work on a copy/paste basis which I can repeat and enter different field descriptions as needed?!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Nov 2011 18:37:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697136#M23108</guid>
      <dc:creator>JeffreyNemecek</dc:creator>
      <dc:date>2011-11-14T18:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: 'If...Then' Code for Field Calculator</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697137#M23109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I completed a Model that overlays several polygon layers, clips out a specified section, dissolves according to certain attributes and sums the acreage.&amp;nbsp; What I want to do is add another Calculate Field entry into my Model, but I can't get the Field Calculation correct.&amp;nbsp; The Fields I want to select from are [CAUVLCType] and [MUSYM].&amp;nbsp; The fields are coded-value domains and descriptions.&amp;nbsp; What I've tried is similar to Label expressions:&lt;BR /&gt;&lt;BR /&gt;Function&lt;BR /&gt;if ([CAUVLCType] = 'CROP' AND [MUSYM] = 'NpA') then&lt;BR /&gt;[Soil_Acreage] * 460&lt;BR /&gt;elseif ([CAUVLCType] = 'WOOD' AND [MUSYM] = 'NpA') then&lt;BR /&gt;[Soil_Acreage] * 100&lt;BR /&gt;End if&lt;BR /&gt;End Function&lt;BR /&gt;&lt;BR /&gt;Can someone provide me a couple lines of code that will work on a copy/paste basis which I can repeat and enter different field descriptions as needed?!&lt;BR /&gt;&lt;BR /&gt;Thanks, Jeff&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Drop the Function brackets.&amp;nbsp; They are not used in the Field Calculator.&amp;nbsp; Also, if you are using VB Script, use double quotes not single quotes for literal strings.&amp;nbsp; Finally you have to return a variable value to the expression in order to output your calculated values.&amp;nbsp; Try these settings (use the expression builder in the Field Calculator tool):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Parser:&amp;nbsp; VB Script&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Dim Output&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If ([CAUVLCType] = "CROP" AND [MUSYM] = "NpA") then&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Output = [Soil_Acreage] * 460&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;elseif ([CAUVLCType] = "WOOD" AND [MUSYM] = "NpA") then&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Output = [Soil_Acreage] * 100&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;End if&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&amp;nbsp; Output&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Nov 2011 03:34:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697137#M23109</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2011-11-15T03:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: 'If...Then' Code for Field Calculator</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697138#M23110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have been trying to follow the logic but unable. Please help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dim [Dense95] &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;IF [JobDensity] &amp;gt;= [Mean] + [SD] * [z164] THEN&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; [Dense95] = 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ELSE&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; [Dense95] =&amp;nbsp; 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;end IF&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What am I doing wrong? [Dense95] is the field where I want the output to auto populate.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lester&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 Apr 2012 19:35:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697138#M23110</guid>
      <dc:creator>lesterking</dc:creator>
      <dc:date>2012-04-23T19:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: 'If...Then' Code for Field Calculator</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697139#M23111</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try declaring and using teh variable as:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Dim Dense95
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Without the []s.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:20:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697139#M23111</guid>
      <dc:creator>NobbirAhmed</dc:creator>
      <dc:date>2021-12-12T05:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: 'If...Then' Code for Field Calculator</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697140#M23112</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You cannot calculate the field directly in the code block.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You can reference a field, as in your If statement,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But you cannot change a field, even if it is the one you are calculating.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code block acts like a function (even though it doesn't use Function tags), and has to return the value it is calculating to the calculation expression. That is why the code block is called Pre-Logic Script Code in the Field calculator: it is executed BEFORE the field is calculated. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As in rfairhur24's example, make the codeblok output a variable:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Output = [Soil_Acreage] * 460&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then put the variable (in thios case, Output) in the expression (in the Field Calculator, it is the box below the code block box. The one headed: Soil_Acreage =&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the python code, the output variable goes in the expression parameter&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;CalculateField_management (in_table, field, expression, {expression_type}, {code_block})&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;While the code block (that sets the variable to the value you need) is in the code_block variable.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Apr 2012 13:14:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/if-then-code-for-field-calculator/m-p/697140#M23112</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-04-24T13:14:55Z</dc:date>
    </item>
  </channel>
</rss>

