Calculate Field Code Block VB Help

3963
9
Jump to solution
03-10-2013 06:02 PM
DarrochKaye
Occasional Contributor
Hi folks,

I have an empty field that I want to populate with text dependent on the value of another field. In Excel this would be a breeze but I'm having trouble with the syntax and the code block structure of the Calculate Field tool. I'm using the tool in VB mode and below is what I have currently got, resulting in the very helpful 99999 Error Code.

In Expression dialogue box:
temp

In Code Block:
DIM temp as String IF OBJECTID='1' THEN temp = 'Very Destructive' ELSEIF OBJECTID='2' THEN temp = 'Destructive' ELSEIF OBJECTID='3' THEN temp = 'Damaging' ENDIF 


This is resulting in errors as described above; 'Expecting end of statement'. I'm fairly certain this is a syntax issue but if it is more complicated then please advise. any assistance would be greatly appreciated.

Thanks,
DK
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChrisFox3
Occasional Contributor III
A couple things, VBScript does not allow you to explicitly declare any data types; all variables are implicitly Variant. Statements like Dim x as String should be removed or simplified to Dim x. If you want to execute only one statement when a condition is true, you can write the code on one line, otherwise each statement needs to be on a new line ie

If....Then
statement
elseif....Then
statement
else statement
end if

Dim temp If [OBJECTID] =1 Then  temp = "Very Destructive" elseif [OBJECTID] = 2 Then  temp = "Destructive" elseif [OBJECTID] =3 Then  temp = "Damaging" else temp ="Nothing" end if

View solution in original post

0 Kudos
9 Replies
AnthonyGiles
Frequent Contributor
Darroch,

Field names need to be placed in brackets:

DIM temp as String
IF [OBJECTID]='1' THEN temp = 'Very Destructive'
ELSEIF [OBJECTID]='2' THEN temp = 'Destructive'
ELSEIF [OBJECTID]='3' THEN temp = 'Damaging'
ENDIF

Regards

Anthony
0 Kudos
DarrochKaye
Occasional Contributor
Darroch, 

Field names need to be placed in brackets: 

DIM temp as String 
IF [OBJECTID]='1' THEN temp = 'Very Destructive' 
ELSEIF [OBJECTID]='2' THEN temp = 'Destructive' 
ELSEIF [OBJECTID]='3' THEN temp = 'Damaging' 
ENDIF 

Regards 

Anthony


Thanks for the info but I'm still getting the same error:

General error executing calculator.
ERROR 999999: Error executing function.
Expected end of statement
Failed to execute (CalculateField).


Thanks,
DK
0 Kudos
AnthonyGiles
Frequent Contributor
Try using double speech marks around your text, and a space between your end and if on your last line, also the last else if only needs to be an else statement, plus the objectid field is numeric so you don't need the values in speech marks.

DIM temp as String
IF [OBJECTID] = 1 THEN temp = "Very Destructive"
ELSEIF [OBJECTID] = 2 THEN temp = "Destructive"
ELSE [OBJECTID] = 3 THEN temp = "Damaging"
END IF
0 Kudos
DarrochKaye
Occasional Contributor
Try using double speech marks around your text, and a space between your end and if on your last line, also the last else if only needs to be an else statement, plus the objectid field is numeric so you don't need the values in speech marks.

DIM temp as String
IF [OBJECTID] = 1 THEN temp = "Very Destructive"
ELSEIF [OBJECTID] = 2 THEN temp = "Destructive"
ELSE [OBJECTID] = 3 THEN temp = "Damaging"
END IF


I had tried various double quote and single quote methods but still receiving the same error.

N.B. This doesn't have to be written in VB; if anyone knows how to write this in Python then please comment.

Thanks,
DK
0 Kudos
AnthonyGiles
Frequent Contributor
Darrock,

Did you also make the other changes I suggested not just the quotes?

Regards

Anthony

P.S. not sure what version of arcmap your are using but I have just found this article:

http://support.esri.com/en/knowledgebase/techarticles/detail/25207
0 Kudos
DarrochKaye
Occasional Contributor
Darrock,

Did you also make the other changes I suggested not just the quotes?

Regards

Anthony

P.S. not sure what version of arcmap your are using but I have just found this article:

http://support.esri.com/en/knowledgebase/techarticles/detail/25207


Yeah I've tried various combinations suggested. I'm using v10.1 SP1; I'll attempt something in Python.
0 Kudos
ChrisFox3
Occasional Contributor III
A couple things, VBScript does not allow you to explicitly declare any data types; all variables are implicitly Variant. Statements like Dim x as String should be removed or simplified to Dim x. If you want to execute only one statement when a condition is true, you can write the code on one line, otherwise each statement needs to be on a new line ie

If....Then
statement
elseif....Then
statement
else statement
end if

Dim temp If [OBJECTID] =1 Then  temp = "Very Destructive" elseif [OBJECTID] = 2 Then  temp = "Destructive" elseif [OBJECTID] =3 Then  temp = "Damaging" else temp ="Nothing" end if
0 Kudos
DarrochKaye
Occasional Contributor
A couple things, VBScript does not allow you to explicitly declare any data types; all variables are implicitly Variant. Statements like Dim x as String should be removed or simplified to Dim x. If you want to execute only one statement when a condition is true, you can write the code on one line, otherwise each statement needs to be on a new line ie

If....Then
statement
elseif....Then
statement
else statement
end if

Dim temp
If [OBJECTID] =1 Then 
temp = "Very Destructive"
elseif [OBJECTID] = 2 Then 
temp = "Destructive"
elseif [OBJECTID] =3 Then 
temp = "Damaging"
else temp ="Nothing"
end if


Excellent Chris, this works perfectly. Thanks very much.

DK

P.S. Any idea why elseif is one work but end if is two?
0 Kudos
ChrisFox3
Occasional Contributor III
Excellent Chris, this works perfectly. Thanks very much.

DK

P.S. Any idea why elseif is one work but end if is two?


I am not sure, but this is consistent with all VB languages.

http://msdn.microsoft.com/en-us/library/752y8abs(v=vs.80).aspx
0 Kudos