Field Calculater

1242
9
Jump to solution
01-24-2014 12:03 AM
AbhishekMisra
New Contributor
Dear tech Support Team,
I am working on table i want some correction in script for calculating field
I have Data in Grid_code field in the form of 1 to 6
In grid_code field there is value 1 to 6 i want 1 is convert as Very low, 2 Is Low, 3 is Moderate, 4 is High and 5 is Very high in Class field i am using following script

Dim Output as String
If [Class] = 1  Then
Output = "Very Low"
Elseif [Class] = 2 Then
Output = "Low"
Elseif [Class] = 3  Then
Output = "Moderate"
Elseif [Class] = 4  Then
Output = "High"
Elseif [Class] = 5  Then
Output = "Very High"
Elseif [Class] = 6  Then
Output = "Extreme"
Else
Output = "xxx"
end if
Please help me to correct this script or please tell me where i get solution


Thanks & Regards
Abhishek Misra
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

Ok. I missed that your code had at least 3 syntax errors. I overlooked them, because the syntax is so basic. “AND” is a Logical Operators used to link expressions that evaluate to True or False (See this link for logical operator help<http://msdn.microsoft.com/en-us/library/4ah2h0k3(v=vs.84).aspx>). You have to use “>=” not “= >” (See this link for comparison operators<http://msdn.microsoft.com/en-us/library/9hck4s70(v=vs.84).aspx>). You have to use “ElseIf” not “Else If” (See this link for If Else Then syntax<http://msdn.microsoft.com/en-us/library/5h27x7e9(v=vs.84).aspx>). You should study this MSDN website and learn more about VB Script syntax.

If <= 1000 Then

Output = "I"

Elseif > 1000 AND <= 3000 Then

Output = "II"

Elseif > 3000 AND <= 5000 Then

Output = "III"

Elseif > 5000 AND <= 10000 Then

Output = "IV"

Elseif > 10000 Then

Output = "V"

Else

Output = "xxx"

End If

View solution in original post

0 Kudos
9 Replies
RobertBorchert
Frequent Contributor III
If you only need to do this once or twice your over thinking it.

Simple perform a selection for all feature 1 then field calculate "Very Low"

Or create a coded domain for that attribute

1   Very Low
2   Low

Etc.

That way down the road whenever someone enters the 1, 2, 3 etc... the field will populate with the desired text.  And it will appear as a drop down selection when editing.

Dear tech Support Team,
I am working on table i want some correction in script for calculating field
I have Data in Grid_code field in the form of 1 to 6
In grid_code field there is value 1 to 6 i want 1 is convert as Very low, 2 Is Low, 3 is Moderate, 4 is High and 5 is Very high in Class field i am using following script

Dim Output as String
If [Class] = 1  Then
Output = "Very Low"
Elseif [Class] = 2 Then
Output = "Low"
Elseif [Class] = 3  Then
Output = "Moderate"
Elseif [Class] = 4  Then
Output = "High"
Elseif [Class] = 5  Then
Output = "Very High"
Elseif [Class] = 6  Then
Output = "Extreme"
Else
Output = "xxx"
end if
Please help me to correct this script or please tell me where i get solution


Thanks & Regards
Abhishek Misra
0 Kudos
RichardFairhurst
MVP Honored Contributor
The code you have written should work if you get rid of the line that says Dim Output as String.  VB Script does not use DIM anymore and it is not even necessary if you are using VBA with ArcMap 9.3 (but then the ElseIf needs to be changed to Else If for VBA).  Make sure you do the calculate to a text field with enough characters to accept the values you want it output and the make sure the field expression is the word Output.  If the Class field is numeric you can't use the field calculator to overwrite that field to a text value and would have to create a new field.

If [Class] = 1  Then 
  Output = "Very Low"
Elseif [Class] = 2 Then 
  Output = "Low"
Elseif [Class] = 3 Then 
  Output = "Moderate"
Elseif [Class] = 4 Then 
  Output = "High"
Elseif [Class] = 5 Then 
  Output = "Very High"
Elseif [Class] = 6 Then 
  Output = "Extreme"
Else
  Output = "xxx"
End If


Expression: Output
0 Kudos
AbhishekMisra
New Contributor

Dear Richard

once again i am stuck in a similar kind of script please help me my script is

If [Area_ha]= < 1000  Then

Output = "I"

Else if [Area_ha] > 1000 & [Area_ha]= < 3000 Then

Output = "II"

Else if [Area_ha] > 3000 & [Area_ha]= < 5000 Then

Output = "III"

Else if [Area_ha] > 5000 & [Area_ha]= < 10000 Then

Output = "IV"

Else if [Area_ha] > 10000 Then

Output = "V"

Else

Output = "xxx"

end if

Thanks & Regards

Abhishek

0 Kudos
RichardFairhurst
MVP Honored Contributor

Abhishek:

Please see the revision below. You cannot use the & for AND in a logical expression. That only works for concatenating strings.

0 Kudos
AbhishekMisra
New Contributor

Thanks For your quick reply

but i am unable to understand your point i want to calculate field in between 1000 to 3000 as II

so what kind of logical expression will be use for this

Regards

Abhishek

0 Kudos
AnthonyGiles
Frequent Contributor

Abhishek,

You have to use the word AND in your script not the ampersand '&' symbol:

Else if [Area_ha] > 1000 AND [Area_ha]= < 3000 Then

Regards

Anthony

0 Kudos
RichardFairhurst
MVP Honored Contributor

Ok. I missed that your code had at least 3 syntax errors. I overlooked them, because the syntax is so basic. “AND” is a Logical Operators used to link expressions that evaluate to True or False (See this link for logical operator help<http://msdn.microsoft.com/en-us/library/4ah2h0k3(v=vs.84).aspx>). You have to use “>=” not “= >” (See this link for comparison operators<http://msdn.microsoft.com/en-us/library/9hck4s70(v=vs.84).aspx>). You have to use “ElseIf” not “Else If” (See this link for If Else Then syntax<http://msdn.microsoft.com/en-us/library/5h27x7e9(v=vs.84).aspx>). You should study this MSDN website and learn more about VB Script syntax.

If <= 1000 Then

Output = "I"

Elseif > 1000 AND <= 3000 Then

Output = "II"

Elseif > 3000 AND <= 5000 Then

Output = "III"

Elseif > 5000 AND <= 10000 Then

Output = "IV"

Elseif > 10000 Then

Output = "V"

Else

Output = "xxx"

End If

0 Kudos
AbhishekMisra
New Contributor

Thanks Richerd Script is working fine

Regards

Abhishek

0 Kudos
AbhishekMisra
New Contributor
Thanks now Script is working fine
Abhishek Misra

The code you have written should work if you get rid of the line that says Dim Output as String.  VB Script does not use DIM anymore and it is not even necessary if you are using VBA with ArcMap 9.3 (but then the ElseIf needs to be changed to Else If for VBA).  Make sure you do the calculate to a text field with enough characters to accept the values you want it output and the make sure the field expression is the word Output.  If the Class field is numeric you can't use the field calculator to overwrite that field to a text value and would have to create a new field.

If [Class] = 1  Then 
  Output = "Very Low"
Elseif [Class] = 2 Then 
  Output = "Low"
Elseif [Class] = 3 Then 
  Output = "Moderate"
Elseif [Class] = 4 Then 
  Output = "High"
Elseif [Class] = 5 Then 
  Output = "Very High"
Elseif [Class] = 6 Then 
  Output = "Extreme"
Else
  Output = "xxx"
End If


Expression: Output
0 Kudos