2

What is wrong with this VBscript in the field calculator?

ColumnC=

if [ColumnB] > 0 then
    [ColumnC] = [ColumnA] / [ColumnB]
endif

Each time I receive this syntax error message:

Error 000539: SyntaxError: invalid syntax (expression, line 1) Failed to execute

All 3 columns are the double field type, although they vary in precision and scale. I have tried converting the columns to float type and doing the same expression. I received the same error messages.

For the first line (starting with 'if'), the script is left-justified. For the second line (starting with '[ColumnC]) I included four spaces before the script. In the codeblock on the field calculator the equation does extend into the third line (and is left justified) simply because the actual name I use for !ColumnB! is longer.

0

1 Answer 1

4

You need to pass the returned value in a variable, not reference the column itself.

Expression:

result

Codeblock:

if [ColumnB] > 0 then
    result = [ColumnA] / [ColumnB]
else
    result = 0
end if

Here's how the two parts fit together in the field calculator - my data doesn't have the same field names, but you get the idea.

enter image description here

2
  • 1
    Are you using the codeblock? The expression should be just "result" and the codeblock is the rest. VBScript doesn't care about indentation.
    – phloem
    Commented Feb 25, 2015 at 23:14
  • 2
    bubbalouie Please add a screen shot of your field calculator to your question, there are a few things that would be explained by that. @phloem, if you do the same that should clear it up. Commented Feb 25, 2015 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.