Select to view content in your preferred language

Field calculator code snippet doesn't work now that ArcMap 10 doesn't support VBA

992
4
01-11-2011 01:41 PM
blairborries
Emerging Contributor
Hello

I have a very large automation model created in modelbuilder that I've been using for a couple of years now and with the upgrade to 10, it no longer works because it contains code snippets in which a static variable is declared at the beginning of the code.  The part that gets an error contains a part of the cumulate values code that is included in Easy Calculate.  This part of my model sums up the values in each row of a column according to the values in an adjacent column. ie.:

[Sourcefield] [Targetfield]
1                 1
2                 3
3                 6
4                 10
5                 15

Obvious, I'm sure to anyone familiar with VBA and Vbscript is that Vbscript does not accept static variables declared at the onset of a set of code.

The code is simple, I've copied it below:

'=========================
'field_CumulateValues.cal
'Author: Ianko Tchoukanski
'http://www.ian-ko.com
'=========================
Static rec As Long
Static dSum As Double
Dim sourceField
'=================
'adjust the source field name below
sourceField =
'=================
If (rec = 0) Then
  dSum = sourceField
Else
  dSum = sourceField + dSum
End If
rec = rec + 1

__esri_field_calculator_splitter__
dSum

I don't know a whole lot about VBA or Vbscript, I've just picked up a little through editing code snippets to meet my needs.  So, I'm wondering if anyone knows a quick fix for this to make it work with Vbscript, someway to remove the static declaration, or if not if you can give some advice... I need to make this code work so I'll learn what I need to figure this out.  Will it be easiest/best to make modelbuilder model into .net code (there's not much more complicated to it than this cumulate values code) and compile into a button or try to rewrite the cumulate values code as a vbscript snippet without using the static declarative?

Thanks

Blair
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus
Did you check Ianko's website http://www.ian-ko.com/
to see if he has provided updates for version 10?  I did and there is stuff there.  You should bookmark the site and visit it regularly to keep current with the new tools provided for ArcMap
0 Kudos
blairborries
Emerging Contributor
Did you check Ianko's website http://www.ian-ko.com/
to see if he has provided updates for version 10? I did and there is stuff there. You should bookmark the site and visit it regularly to keep current with the new tools provided for ArcMap


He did not update all the easy calculate functions-in particular he did not update cumulate values- only the edit, geometry and a few other functions.  Where he did update, it was in a way that cannot be used with modelbuilder; he made a button using .Net.  In his latest update, he said he was not going to update for 10 and recommended using .net to do everything because VBA was on its way out.  What does everyone think?  Is there a quick fix to make this code work with Vbscript so the field calculator can be used or should I use .Net.  And if I use .Net will it be an easy transition?

Blair
0 Kudos
blairborries
Emerging Contributor
Well, I think I've found my own answer.  For anyone else trying to solve the same problem, it looks like ESRI has put up some instructions on how to do "accumulative functions" with python inside the code block.  This may be the quick fix i was looking for, but I still haven't tested it.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005s0000002m000000.htm

Parser:
Python

Expression:
accumulate(!FieldA!)

Code Block:
total = 0
def accumulate(increment):
global total
if total:
  total += increment
else:
  total = increment
return total
0 Kudos
DanPatterson_Retired
MVP Emeritus
stick with Python solutions, VBA is gone, perhaps VBScript will be next, Python is stable for the next three years (aka no major changes, just bug fixes) and it seems to be the preferred scripting/programming environment within Arcmap and open source GIS packages.  Pretty anything you see in VBA can be converted to Python easily (start by removing the Dim this, Dim that stuff), the only thing that isn't great is the code block section within the field calculator which eventually will allow one to load Python scripts like the old VB scripts....translate away
0 Kudos