Select to view content in your preferred language

VBA red text - 9.2 to 9.3.1

814
8
01-31-2011 10:43 AM
LornaMurison
Regular Contributor
Hi everyone,
I used a piece of code when I was using ArcGIS 9.2 to split lines into segments of no less than a given length.

I am now using 9.3.1 and there is one line in the code that is giving me trouble. This line:
For lInFld = 0 To pInRow.Fields.FieldCount â?? 1
from the code below shows up in red, and when I run the code it causes a syntax error.

Any help would be appreciated
Private Sub Proportion_Click()
'Divides a line feature class into segments no less than a specified amount
'Source: â??http://forums.esri.com/Thread.asp?c=93&f=986&t=264945&mc=1#msgid815131

' Make sure that the layer is an editable, projected, polyline, with features selected
Dim pEditor As IEditor
Set pEditor = Application.FindExtensionByName("ESRI Object Editor")
If pEditor.EditState <> esriStateEditing Then
    MsgBox "start editing first"
Exit Sub
End If

Dim pEL As IEditLayers
Set pEL = pEditor
If pEL.CurrentLayer.FeatureClass.ShapeType <> esriGeometryPolyline Then
    MsgBox "target layer must be polylines"
Exit Sub
End If

Dim pFSel As IFeatureSelection
Set pFSel = pEL.CurrentLayer
If pFSel.SelectionSet.Count = 0 Then
    MsgBox "select some " & pEL.CurrentLayer.Name & " features first"
Exit Sub
End If

If Not TypeOf pEditor.Map.SpatialReference Is IProjectedCoordinateSystem Then
    MsgBox ("set a projection first")
Exit Sub
End If

'get the user-defined minimum segment length
Dim dInterval As Double
dInterval = InputBox("Enter minimum segment length in meters", "Segment Length", 250)

' Set up a query to select features greater than the minimum segment length
Dim pQueryF As IQueryFilter
Set pQueryF = New QueryFilter
pQueryF.SubFields = "Length"
pQueryF.WhereClause = "Length > " & dInterval

'pEditor.StartOperation
Dim pFCur As IFeatureCursor
pFSel.SelectionSet.Search pQueryF, False, pFCur
Dim pFeat As IFeature
Set pFeat = pFCur.NextFeature

Do Until pFeat Is Nothing
    Dim pPolyline As IPolyline
    Set pPolyline = pFeat.ShapeCopy
    pPolyline.Project pEditor.Map.SpatialReference
    Dim l As Long
    Dim Max As Integer
    Max = Int(pPolyline.Length / dInterval)

    For l = 0 To Max
        Dim pOutCurve As ICurve
        pPolyline.GetSubcurve (l - 1) / Max, l / Max, True, pOutCurve
        Debug.Print pOutCurve.Length
        pOutCurve.Project GetSR(pEL.CurrentLayer.FeatureClass)
        If l = 0 Then
            Set pFeat.Shape = pOutCurve
            pFeat.Store
        Else
            Dim pNewFeat As IFeature
            Set pNewFeat = pEL.CurrentLayer.FeatureClass.CreateFeature
            CopyFields pFeat, pNewFeat
            Set pNewFeat.Shape = pOutCurve
            pNewFeat.Store
            pFSel.Add pNewFeat
        End If
    Next l
    Set pFeat = pFCur.NextFeature

    'Show something in status bar
    Dim pStatus As IStatusBar
    Set pStatus = Application.StatusBar
    pStatus.Message(0) = "Processing " & pOutCurve.Length
Loop

pEditor.StopOperation "split at " & dInterval
MsgBox ("Finished!  Don't forget to save your edits!")
End Sub


Sub CopyFields(pInRow As IRow, pOutRow As IRow)

Dim lInFld As Long

For lInFld = 0 To pInRow.Fields.FieldCount â?? 1  <--- This line causes trouble!
    Dim lOutFld As Long
    lOutFld = pOutRow.Fields.FindField(pInRow.Fields.Field(lInFld).Name)
    If lOutFld > -1 Then
        If pInRow.Fields.Field(lInFld).Editable Then
            If pInRow.Fields.Field(lInFld).Type <> esriFieldTypeGeometry Then
                pOutRow.Value(lOutFld) = pInRow.Value(lInFld)
            End If
        End If
    End If
Next lInFld

End Sub

Function GetSR(pGDS As IGeoDataset) As ISpatialReference
Set GetSR = pGDS.SpatialReference
End Function
0 Kudos
8 Replies
RuchiraWelikala
Regular Contributor
Does this happen during the first time around the loop?
and also, is the fieldcount = 0 off the bat?
0 Kudos
LornaMurison
Regular Contributor
Thanks for the reply,
It does happen during the first loop, and I don't know how to check what the field count is right off the bat.
0 Kudos
RuchiraWelikala
Regular Contributor
Sub CopyFields(pInRow As IRow, pOutRow As IRow)

Dim lInFld As Long
Msgbox (pInRow.Fields.FieldCount) <--- INSERT THIS LINE
For lInFld = 0 To pInRow.Fields.FieldCount �?? 1  <--- This line causes trouble!
    Dim lOutFld As Long
    lOutFld = pOutRow.Fields.FindField(pInRow.Fields.Field(lInFld).Name)
    If lOutFld > -1 Then
        If pInRow.Fields.Field(lInFld).Editable Then
            If pInRow.Fields.Field(lInFld).Type <> esriFieldTypeGeometry Then
                pOutRow.Value(lOutFld) = pInRow.Value(lInFld)
            End If
        End If
    End If
Next lInFld

End Sub
0 Kudos
LornaMurison
Regular Contributor
Sorry,
I did try that and the code seems to crash at "Sub CopyFields(pInRow As IRow, pOutRow As IRow)" which gets highlighted in yellow.
But "For lInFld = 0 To pInRow.Fields.FieldCount �?? 1" gets highlighted in blue.
Even before I run the code that line shows up in red, but doesn't appear to have an error message associated with it.
... hope that makes sense.

Could this be caused by using a really large feature class?

Thanks
0 Kudos
LornaMurison
Regular Contributor
It is giving me an "expected end of statement" error.
0 Kudos
RuchiraWelikala
Regular Contributor
It is giving me an "expected end of statement" error.


ah yes.. delete that line and retype it as such..don't copy paste..retype it:
For lInFld = 0 To (pInRow.Fields.FieldCount - 1)


Make sure you get rid of  "<---" comments.
0 Kudos
LornaMurison
Regular Contributor
Thank-you so much, it works now.
Is this something that changed from 9.2 to 9.3?
0 Kudos
RuchiraWelikala
Regular Contributor
Nah, it's just buggy.  You can take out the brackets and it would still probably work.
Cheers,
0 Kudos