Calculate Field Error VBScripting

2204
3
04-24-2012 09:48 AM
SteveRichards2
New Contributor
Hello all! The company I am currently working for has decided to migrate from ArcGIS 9.3 to 10.0 recently and we are having difficulty using a few of our Arc 9.3 models that utilize the Calculate Field tool. They were written in VBA and I need to get them to work using VBScript. The following bit of code is intended to calculate the x coordinate of the centre of a harvesting block and put it in a field.


Field Name: xcoord
Expression: dblX
Expression Type: VB

Code Block:
Dim dblX as Double
Dim pArea as IArea
Set pArea = [Shape]
dblX = pArea.Centroid.X

I am aware that you cannot have "as Double" in Dim statements with VBSand I have tried to remove those without any luck. I apologize in advance for my lack of scripting/programming language. I receive the following error once the model finished running:

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


I also get the same error when the following bit of code runs in another calculate field tool within the same model.

Field Name: LONGITUDE
Expression: sDMS
Expression Type: VB

Code Block:
Dim pArea As IArea
Dim dblX As Double
Dim dVal As Double
Dim dD As Double, dM As Double, dS As Double
Dim dM1 As Double, dS1 As Double
Dim sD As String, sM As String, sS As String
Dim sSuf As String
Dim sType As String
Dim sDMS As String
Dim sDeg As String, sMin As String, sSec As String
Dim inumdec As Integer

Dim pSpatRefFact As ISpatialReferenceFactory2
Set pSpatRefFact = New SpatialReferenceEnvironment

Dim pCoordSys As IGeographicCoordinateSystem
Set pCoordSys = pSpatRefFact.CreateGeographicCoordinateSystem(esriSRGeoCS_NAD1983)

Dim pNewSpRef As ISpatialReference
Set pNewSpRef = pCoordSys


Set pArea = [Shape]

pArea.Centroid.Project pNewSpRef
dblX = pArea.Centroid.X
dVal = dblX

sDeg = "d"
sMin = Chr(39)
sSec = Chr(34)
inumdec = 2
sType = "dms"

dVal = Abs(dVal)
dD = Int(dVal)
sD = CStr(dD)
dM = (dVal - dD) * 60
dM1 = Int(dM)

If (sType = "dms") Then
If (Len(CStr(dM1)) = 1) Then
sM = "0" & CStr(dM1)
Else
sM = CStr(dM1)
End If
dS = FormatNumber(((dM - dM1) * 60), inumdec)
dS1 = Int(dS)
If (Len(CStr(dS1)) = 1) Then
sS = "0" & CStr(dS)
Else
sS = CStr(dS)
End If
sDMS = sD & sDeg & " " & sM & sMin & " " & sS & sSec
Else
sM = CStr(FormatNumber(dM, inumdec))
sDMS = sD & sDeg & " " & sM & sMin
End If

Any assistance with getting this to run using VBScripting would be most appreciated. Thanks in advance!

Steve
0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor
Hello all! The company I am currently working for has decided to migrate from ArcGIS 9.3 to 10.0 recently and we are having difficulty using a few of our Arc 9.3 models that utilize the Calculate Field tool. They were written in VBA and I need to get them to work using VBScript. The following bit of code is intended to calculate the x coordinate of the centre of a harvesting block and put it in a field.


Field Name: xcoord
Expression: dblX
Expression Type: VB

Code Block:
Dim dblX as Double
Dim pArea as IArea
Set pArea = [Shape]
dblX = pArea.Centroid.X




From the online help:

Use Python if you want access to geoprocessing functionality, including feature geometry....If you have VBA code from past releases that use ArcObjects, you will need to modify your calculations to work in 10.0.


It sounds painful, but rewriting it in Python won't be as hard as you may think. The arcpy methods handling feature geometry are less complex to work with than ArcObjects.

Arc 10 Help:
Calculate Field examples
Working" rel="nofollow" target="_blank">http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html... with geometry in Python

Also, there are tools available that could be combined to set up an easier-to-maintain workflow than your VBA scripts; a ModelBuilder model may even accomplish what you need using tools like:

Feature To Point
Add XY
Convert Coordinate Notation
0 Kudos
SteveRichards2
New Contributor
Thank you for the reply. Learning Python is on the agenda for the near future and I will eventually rewrite the VBA code to Python. For now I just simplified the models in order to get them to produce what I need. Thanks for your help.
0 Kudos
DarrenWiens2
MVP Honored Contributor
Just FYI, you can calculate the x-centroid using the "PYTHON" parser (v9.2) in the Calculate Field tool (expression stolen from this help page😞
float(!SHAPE.CENTROID!.split()[0])
0 Kudos