+ Reply to Thread
Results 1 to 4 of 4

Thread: Calculate Field Error VBScripting

  1. #1
    Steve Richards
    Join Date
    Apr 2012
    Posts
    10
    Points
    0
    Answers Provided
    0


    0

    Default Calculate Field Error VBScripting

    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

  2. #2
    Curtis Price

    Join Date
    Oct 2009
    Posts
    1,798
    Points
    874
    Answers Provided
    127


    0

    Default Re: Calculate Field Error VBScripting

    Quote Originally Posted by 66stever View Post
    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:
    Code:
    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 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
    Last edited by curtvprice; 04-24-2012 at 11:35 AM.

  3. #3
    Steve Richards
    Join Date
    Apr 2012
    Posts
    10
    Points
    0
    Answers Provided
    0


    0

    Default Re: Calculate Field Error VBScripting

    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.

  4. #4
    Darren Wiens

    Join Date
    Apr 2011
    Posts
    821
    Points
    271
    Answers Provided
    37


    1

    Default Re: Calculate Field Error VBScripting

    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):
    Code:
    float(!SHAPE.CENTROID!.split()[0])

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts