ERROR 999999 using Calculate Field in VB Code Block

3996
3
Jump to solution
07-12-2013 07:05 AM
KaelDowdy
New Contributor III
Perhaps I should be using Python, but old habits die hard as I have more of a VB background...

At any rate, I'm trying to update a field in a table using the Calculate Field tool.  After fighting numerous errors, I decided to simplify my function to a very simple routine to isolate the problem.  In the screenshot, I'm just returning the value I'm passing in as an input value to save in the field.  However, I get the following error:[INDENT]
General error executing calculator.[/INDENT]
[INDENT]ERROR 999999: Error executing function.[/INDENT]
[INDENT]Syntax error[/INDENT]
[INDENT]Failed to execute (Calculate Field).
[/INDENT]


Screenshot of Calculate Field setup:

[ATTACH=CONFIG]25886[/ATTACH]

I know that VB (VBScript) has certain limitations, such as not being able to explicitly declare datatypes for variables, so I don't think that's the problem here.  It appears that the "shell" or setup of either the function itself (in the Code Block box) or the function call (in the Expression box) is wrong.  Once I get past this error, I can put the real logic in the function.

Does anyone know what I might be doing wrong?

Thanks!

EDIT:  Using ArcMap 10.1
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
Perhaps I should be using Python, but old habits die hard as I have more of a VB background...

At any rate, I'm trying to update a field in a table using the Calculate Field tool.  After fighting numerous errors, I decided to simplify my function to a very simple routine to isolate the problem.  In the screenshot, I'm just returning the value I'm passing in as an input value to save in the field.  However, I get the following error:[INDENT]
General error executing calculator.[/INDENT]
[INDENT]ERROR 999999: Error executing function.[/INDENT]
[INDENT]Syntax error[/INDENT]
[INDENT]Failed to execute (Calculate Field).
[/INDENT]


Screenshot of Calculate Field setup:

[ATTACH=CONFIG]25886[/ATTACH]

I know that VB (VBScript) has certain limitations, such as not being able to explicitly declare datatypes for variables, so I don't think that's the problem here.  It appears that the "shell" or setup of either the function itself (in the Code Block box) or the function call (in the Expression box) is wrong.  Once I get past this error, I can put the real logic in the function.

Does anyone know what I might be doing wrong?

Thanks!

EDIT:  Using ArcMap 10.1


The Field Calculator has disabled the Function, Sub, Execute, and probably some other similar keywords in its parser for VB Script (and I believe for VBA as well prior to 10.0), possibly for security reasons to protect against code insertion.  As a result, it is impossible to build a recursive function within the Field Calculator using VB.  So if that is where you are going, you can't do that.  And in fact when you use VB with a codeblock, the Expression will only be used for an Output variable name, and is not used to pass any input to the codeblock (unlike Python).

However, the Codeblock itself can accept external inputs such as fields and model builder variables directly.

So it is possible to obtain and operate on inputs from external data sources, such as fields or Model Builder variables, within the CodeBlock as shown below:

Codeblock:
Test = 28 ' Hard Coded Value Test = Test / [MyField] ' Field Value evaluated at runtime per each feature or row Test = Test + %Value% ' Model Builder Variable evaluated at runtime per each iteration or based on user input to the model For i = 0 to 3 ' For loop processes up to 4 times, breaking out of the loop as soon as Test has grown larger than 1000.   Test = Test * 10   If Test > 1000 Then Exit For Next


Expression:
Test

(Note:  At the users conference I was told by the person who writes the SQL parser that he has intentionally disabled standard SQL functions that can be used by hackers for code insertion, and causes the parser to return errors if those function are attempted, since the parser treats them as unrecognized requests.  Where he has allowed SQL functionality that could lead to Code Insertion if unprotected, he has only permitted it with well structured SQL commands and must to do additional parsing and evaluation within the ESRI parser to prevent any code insertion attempt before passing the SQL to the underlying database.  Field Calculator has to offer the same kinds of protection and Function, Sub, and Execute are probably deemed too open ended to allow.

Presumably Python is also being handled in much the same way to block code insertion attempts; however, Python is more like VBA at 9.3 than VB Script, since only Python can define cursors or operate on geometry data within the Field Calculator like VBA could, while VB Script can't.  Nothing has access to the same set of ArcObject methods through the Field Calculator that VBA had at 9.3, but Python comes the closest in terms of functionality).

View solution in original post

0 Kudos
3 Replies
KaelDowdy
New Contributor III
Perhaps I should be using Python, but old habits die hard as I have more of a VB background..

So, I converted my function to a Python script and it worked without errors.  While I'm able to get by for the time being using Python instead of VB, it would still be nice to determine what the error might be with my approach using VB -- especially for more complex VB scripts that I may know how to write in VBScript but not necessarily in Python.

Does anyone see what might be wrong with my VB code block or function call?
0 Kudos
RichardFairhurst
MVP Honored Contributor
Perhaps I should be using Python, but old habits die hard as I have more of a VB background...

At any rate, I'm trying to update a field in a table using the Calculate Field tool.  After fighting numerous errors, I decided to simplify my function to a very simple routine to isolate the problem.  In the screenshot, I'm just returning the value I'm passing in as an input value to save in the field.  However, I get the following error:[INDENT]
General error executing calculator.[/INDENT]
[INDENT]ERROR 999999: Error executing function.[/INDENT]
[INDENT]Syntax error[/INDENT]
[INDENT]Failed to execute (Calculate Field).
[/INDENT]


Screenshot of Calculate Field setup:

[ATTACH=CONFIG]25886[/ATTACH]

I know that VB (VBScript) has certain limitations, such as not being able to explicitly declare datatypes for variables, so I don't think that's the problem here.  It appears that the "shell" or setup of either the function itself (in the Code Block box) or the function call (in the Expression box) is wrong.  Once I get past this error, I can put the real logic in the function.

Does anyone know what I might be doing wrong?

Thanks!

EDIT:  Using ArcMap 10.1


The Field Calculator has disabled the Function, Sub, Execute, and probably some other similar keywords in its parser for VB Script (and I believe for VBA as well prior to 10.0), possibly for security reasons to protect against code insertion.  As a result, it is impossible to build a recursive function within the Field Calculator using VB.  So if that is where you are going, you can't do that.  And in fact when you use VB with a codeblock, the Expression will only be used for an Output variable name, and is not used to pass any input to the codeblock (unlike Python).

However, the Codeblock itself can accept external inputs such as fields and model builder variables directly.

So it is possible to obtain and operate on inputs from external data sources, such as fields or Model Builder variables, within the CodeBlock as shown below:

Codeblock:
Test = 28 ' Hard Coded Value Test = Test / [MyField] ' Field Value evaluated at runtime per each feature or row Test = Test + %Value% ' Model Builder Variable evaluated at runtime per each iteration or based on user input to the model For i = 0 to 3 ' For loop processes up to 4 times, breaking out of the loop as soon as Test has grown larger than 1000.   Test = Test * 10   If Test > 1000 Then Exit For Next


Expression:
Test

(Note:  At the users conference I was told by the person who writes the SQL parser that he has intentionally disabled standard SQL functions that can be used by hackers for code insertion, and causes the parser to return errors if those function are attempted, since the parser treats them as unrecognized requests.  Where he has allowed SQL functionality that could lead to Code Insertion if unprotected, he has only permitted it with well structured SQL commands and must to do additional parsing and evaluation within the ESRI parser to prevent any code insertion attempt before passing the SQL to the underlying database.  Field Calculator has to offer the same kinds of protection and Function, Sub, and Execute are probably deemed too open ended to allow.

Presumably Python is also being handled in much the same way to block code insertion attempts; however, Python is more like VBA at 9.3 than VB Script, since only Python can define cursors or operate on geometry data within the Field Calculator like VBA could, while VB Script can't.  Nothing has access to the same set of ArcObject methods through the Field Calculator that VBA had at 9.3, but Python comes the closest in terms of functionality).
0 Kudos
KaelDowdy
New Contributor III
The Field Calculator has disabled the Function, Sub, Execute, and probably some other similar keywords in its parser for VB Script (and I believe for VBA as well prior to 10.0), possibly for security reasons to protect against code insertion.  As a result, it is impossible to build a recursive function within the Field Calculator using VB.  So if that is where you are going, you can't do that.  And in fact when you use VB with a codeblock, the Expression will only be used for an Output variable name, and is not used to pass any input to the codeblock (unlike Python).

...

(Note:  At the users conference I was told by the person who writes the SQL parser that he has intentionally disabled standard SQL functions that can be used by hackers for code insertion, and causes the parser to return errors if those function are attempted, since the parser treats them as unrecognized requests.  Where he has allowed SQL functionality that could lead to Code Insertion if unprotected, he has only permitted it with well structured SQL commands and must to do additional parsing and evaluation within the ESRI parser to prevent any code insertion attempt before passing the SQL to the underlying database.  Field Calculator has to offer the same kinds of protection and Function, Sub, and Execute are probably deemed too open ended to allow.

Thanks for the insight -- I'm new to the various Esri tools, so the particulars of what was supported in earlier versions and what no longer isn't is something new to me.

I can understand the implications of code injection and safeguarding against it; my only complaint would be for the error message to be a little more informative as to the nature of the error and/or the documentation be updated to state that this particular set of functionality within VB code blocks has been deprecated or disabled.

I guess that's the advantage of doing things purely in a Python script as opposed to just an expression as part of a tool inside a model (i.e., being able to perform recursive function calls).  In this particular case, the logic for my function was pretty basic, so I could get away with doing it in a non-procedural fashion, but if I want to do something more modular or break up a more complex process into smaller pieces, it appears I'll need to use Python.  Guess it's time for me to jump into the snake pool!

Thanks for the help!
Kael
0 Kudos