Select to view content in your preferred language

Edit Attributes in User Form

1126
8
01-05-2012 04:53 AM
PhyllisDavis
Deactivated User
I am trying to convert my old ArcGIS 9.3 (VBA) code to ArcGIS 10 using Visual Basic 2008 Express Edition. So far I have sucessfully created an pop-up window that displays the attributes of a selected feature in text boxes on the form.

However, I want to create a button that allows users to save any changes they make to the attributes in the user form. Below is the code I have thus far for the On_Click event to update the attributes.

The error I'm getting is on "IEditor.EditState <> esriStateEditing Then" IT says reference to non-shared member requires an object reference


Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
    Dim pMxDoc As IMxDocument
    pMxDoc = My.ArcMap.Document
    Dim pMap As IMap
    pMap = pMxDoc.FocusMap
    Dim pFLayer As IFeatureSelection

    Dim Count As Integer
    For Count = 0 To pMap.LayerCount - 1


        'Get the Transportation Improvement Line Layer from the Map
        If pMap.Layer(Count).Name = "TransportationImprovementProjects_Lines" Then
            pFLayer = pMap.Layer(Count)
            Dim pFCursor As IFeatureCursor = Nothing
            pFLayer.SelectionSet.Search(Nothing, False, pFCursor)
            Dim pF As IFeature
            pF = pFCursor.NextFeature
        End If

        'Get Dataset from FeatureLayer
        Dim pEditDataset As IDataset
        pEditDataset = pFLayer

        'If not editing, start editing dataset's Workspace
        If IEditor.EditState <> esriStateEditing Then
            Editor.StartEditing(pEditDataset.Workspace)
        End If

        'Initialize first (only) feature
        Dim pFeat As IFeature
        pFeat = pFLayer.NextFeature

        'Update attributes with user input
        pFeat.Value(pFeat.Fields.FindField("YEAR")) = txtPrjYear.Text
        pFeat.Value(pFeat.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
        pFeat.Value(pFeat.Fields.FindField("LOCATION")) = txtLocation.Text
        pFeat.Value(pFeat.Fields.FindField("AGENCY")) = txtAgency.Text
        pFeat.Value(pFeat.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
        pFeat.Value(pFeat.Fields.FindField("COST")) = txtCost.Text
        pFeat.Value(pFeat.Fields.FindField("DATEEDITED")) = txtDate.Text
        pFeat.Value(pFeat.Fields.FindField("USEREDITED")) = txtUser.Text

        'Store changes to feature
        pFeat.Store()

        'Stop editing and save edits
        Editor.StopEditing(True)

        'Refresh map
        pMxDoc.ActiveView.Refresh()

        'Close form
        Me.Close()
    Next Count
End Sub
0 Kudos
8 Replies
NeilClemmons
Honored Contributor
The variable you are using to reference the editor is named Editor, not IEditor.  Or at least I think it is.  I don't see where you are declaring it or setting its value.  Change IEditor to Editor and it should work.
0 Kudos
PhyllisDavis
Deactivated User
Ahh...thanks, Neil..

Ahh...thanks, Neil!!

I added "Public Editor as Editor", Instantiated the EditorDim Editor = My.ArcMap.Document.FindExtensionByName("ESRI Object Editor"), and changed all my IEditors to Editor.

However, it crashes everytime i push the button....any suggestions?!?

    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
        Dim pMxDoc As IMxDocument
        pMxDoc = My.ArcMap.Document
        Dim pMap As IMap
        pMap = pMxDoc.FocusMap
        Dim pFLayer As IFeatureSelection
        Dim Count As Integer

        'Instantiate the editor 
        Dim Editor = My.ArcMap.Document.FindExtensionByName("ESRI Object Editor")

        'Get the Transportation Improvement Line Layer from the Map
        If pMap.Layer(Count).Name = "TransportationImprovementProjects_Lines" Then
            pFLayer = pMap.Layer(Count)
            Dim pFCursor As IFeatureCursor = Nothing
            pFLayer.SelectionSet.Search(Nothing, False, pFCursor)
            Dim pF As IFeature
            pF = pFCursor.NextFeature

            'Get Dataset from FeatureLayer
            Dim pEditDataset As IDataset
            pEditDataset = pFLayer

            'If not editing, start editing dataset's Workspace
            If Editor.EditState <> Editor Then
                Editor.StartEditing(pEditDataset.Workspace)
            End If

            'Initialize first (only) feature
            Dim pFeat As IFeature
            pFeat = pFLayer.NextFeature

            'Update attributes with user input
            pFeat.Value(pFeat.Fields.FindField("YEAR")) = txtPrjYear.Text
            pFeat.Value(pFeat.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
            pFeat.Value(pFeat.Fields.FindField("LOCATION")) = txtLocation.Text
            pFeat.Value(pFeat.Fields.FindField("AGENCY")) = txtAgency.Text
            pFeat.Value(pFeat.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
            pFeat.Value(pFeat.Fields.FindField("COST")) = txtCost.Text
            pFeat.Value(pFeat.Fields.FindField("DATEEDITED")) = txtDate.Text
            pFeat.Value(pFeat.Fields.FindField("USEREDITED")) = txtUser.Text

            'Store changes to feature
            pFeat.Store()

            'Stop editing and save edits
            Editor.StopEditing(True)

            'Close form
            Me.Close()
        End If

    End Sub
0 Kudos
NeilClemmons
Honored Contributor
You're declaring Editor twice.  You should only be declaring it once.  If you want the variable to be module level then keep the Public declaration and remove the Dim.  The line should be:

Editor = My.ArcMap.Document.FindExtensionByName("ESRI Object Editor")

If you want the variable to have local scope then remove the Public declaration and edit local declaration to include the data type:

Dim Editor As IEditor = My.ArcMap.Document.FindExtensionByName("ESRI Object Editor")

Also, this line is incorrect:
If Editor.EditState <> Editor Then

You should replace Editor with an EditState enumeration value.
You should also put your edits inside of an edit operation, which I don't see in your code.
0 Kudos
PhyllisDavis
Deactivated User
Neil, Thanks again for your help...as you can tell I'm very much a novice with programming.  I spent a lot of time yesterday trying to figure out how to get this code to work, but each time it crashes when the button is clicked. I added "editor.startediting(peditdataset.workspace)" within the code and removed the public editor declaration.

Am I selecting my feature in correctly or am I still mis-coding my editor?

    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
        pMxDoc = My.ArcMap.Document
        Dim pMap As IMap
        pMap = pMxDoc.FocusMap
        Dim pFLayer As IFeatureSelection
        Dim Count As Integer


        'Get a reference to the editor
        Dim uid As UID
        uid = New UIDClass()
        uid.Value = "esriEditor.Editor"
        Dim editor As IEditor
        editor = My.ArcMap.Document.FindExtensionByName("ESRI Object Editor")

        'If not editing, start editing dataset's Workspace
        If editor.EditState <> esriEditState.esriStateEditing Then Exit Sub

        'Get the Transportation Improvement Line Layer from the Map
        If pMap.Layer(Count).Name = "TransportationImprovementProjects_Lines" Then
            pFLayer = pMap.Layer(Count)
            Dim pFCursor As IFeatureCursor = Nothing
            pFLayer.SelectionSet.Search(Nothing, False, pFCursor)
            Dim pF As IFeature
            pF = pFCursor.NextFeature

            'Get Dataset from FeatureLayer
            Dim pEditDataset As IDataset
            pEditDataset = pFLayer

            'Start Editing
            editor.StartEditing(pEditDataset.Workspace)

            'Initialize first (only) feature
            Dim pFeat As IFeature
            pFeat = pFLayer.NextFeature

            'Update attributes with user input
            pFeat.Value(pFeat.Fields.FindField("YEAR")) = txtPrjYear.Text
            pFeat.Value(pFeat.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
            pFeat.Value(pFeat.Fields.FindField("LOCATION")) = txtLocation.Text
            pFeat.Value(pFeat.Fields.FindField("AGENCY")) = txtAgency.Text
            pFeat.Value(pFeat.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
            pFeat.Value(pFeat.Fields.FindField("COST")) = txtCost.Text
            pFeat.Value(pFeat.Fields.FindField("DATEEDITED")) = txtDate.Text
            pFeat.Value(pFeat.Fields.FindField("USEREDITED")) = txtUser.Text

            'Store changes to feature
            pFeat.Store()

            'Stop editing and save edits
            Editor.StopEditing(True)

            'Close form
            Me.Close()
        End If



    End Sub
0 Kudos
NeilClemmons
Honored Contributor
There are several things I would change in your code.
1)  You declare and set the variable uid but then never use it.  I would get rid of it or change the call to FindExtensionByName to FindExtensionByCLSID and use it.
2)  You're checking the editor's Edit State and exiting if an edit session is not already started.  Therefore, you shouldn't attempt to start an edit session later in the code.  Remove that call to start the edit session.
3)  You're declaring pFCursor, filling it from the selection set and getting the first feature from it.  You then do not use it for anything.
4)  pFeat = pFLayer.NextFeature  I'm not sure how you don't get a compile error here.  IFeatureSelection doesn't have a NextFeature method.  I would get rid of this and use the feature you got from pFCursor earlier.
5)  Make sure all of those field names are spelled correctly.  Is PROJECTYPE really spelled with only one T?
6)  You should enclose your edits within an edit operation.
0 Kudos
PhyllisDavis
Deactivated User
Neil,
Thanks for catching my Editor declarations..I fixed that error.  I changed my code to loop through and get the selected feature and update attributes...but the form just closes, it doesn't update the data.  Is this because I don't have it in an edit operation like you said? How do I create and edit operation?

Thanks again for help!

       Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
        Dim pMxDocument As IMxDocument
        Dim pMap As IMap
        Dim pEnumLayer As IEnumLayer
        Dim pFeatureSelection As IFeatureSelection
        Dim player As ILayer

        'Get a reference to the editor.
        Dim editor As IEditor
        editor = My.ArcMap.Application.FindExtensionByName("esriEditor.Editor")


        pMxDocument = My.ArcMap.Application.Document
        pMap = pMxDocument.FocusMap
        pEnumLayer = pMap.Layers
        pEnumLayer.Reset()
        player = pEnumLayer.Next
        Do While Not player Is Nothing
            If TypeOf player Is IFeatureLayer Then
                Dim pFLayer As IFeatureLayer
                pFLayer = player
                pFeatureSelection = pFLayer
                If pFeatureSelection.SelectionSet.Count <> 0 Then

                    Dim pFCursor As IFeatureCursor
                    pFeatureSelection.SelectionSet.Search(Nothing, False, pFCursor)
                    Dim pFeature As IFeature
                    pFeature = pFCursor.NextFeature
                    If Not pFeature Is Nothing Then

                        'Get Dataset from FeatureLayer
                        Dim pEditDataset As IDataset
                        pEditDataset = pFLayer

                        'If not editing, start editing dataset's Workspace
                        If editor.EditState <> esriEditState.esriStateEditing Then
                            editor.StartEditing(pEditDataset.Workspace)
                        End If

                        'Get SelectionSet
                        Dim pSelSet As ISelectionSet
                        pSelSet = GetSelection(pFLayer)

                        'Get FeatureCursor from SelectionSet
                        pSelSet.Search(Nothing, True, pFCursor)

                        'Initialize first (only) feature
                        pFeature = pFCursor.NextFeature

                        'Update attributes with user input
                        pFeature.Value(pFeature.Fields.FindField("YEAR")) = txtPrjYear.Text
                        pFeature.Value(pFeature.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
                        pFeature.Value(pFeature.Fields.FindField("LOCATION")) = txtLocation.Text
                        pFeature.Value(pFeature.Fields.FindField("AGENCY")) = txtAgency.Text
                        pFeature.Value(pFeature.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
                        pFeature.Value(pFeature.Fields.FindField("COST")) = txtCost.Text
                        pFeature.Value(pFeature.Fields.FindField("DATEEDITED")) = txtDate.Text
                        pFeature.Value(pFeature.Fields.FindField("USEREDITED")) = txtUser.Text
                    End If
                End If
            End If
            player = pEnumLayer.Next
        Loop

        'Store changes to feature
        'pFeature.

        'Stop editing and save edits
        editor.StopEditing(True)

        'Close form
        Me.Close()

    End Sub

    Public Function GetSelection(ByVal pFeatLyr As IFeatureLayer) As ISelectionSet

        'Gets selection of feature layer and returns selection set

        'Initialize the required variables
        pMxDoc = My.ArcMap.Application.Document
        Dim pFeatSel As IFeatureSelection
        pFeatSel = pFeatLyr

        GetSelection = pFeatSel.SelectionSet

    End Function
End Class
0 Kudos
PhyllisDavis
Deactivated User
I changed my code some...and all of the errors are out of my program, except I get an error in my immediate window that says:" Am I not referencing the Editor or another toolbar correctly?


    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
            Dim pMxDocument As IMxDocument
            Dim pMap As IMap
            Dim pEnumLayer As IEnumLayer
            Dim player As ILayer
            Dim editor As IEditor
            editor = My.ArcMap.Application.FindExtensionByName("esriEditor.Editor")


            pMxDocument = My.ArcMap.Application.Document
            pMap = pMxDocument.FocusMap
            pEnumLayer = pMap.Layers
            pEnumLayer.Reset()
            player = pEnumLayer.Next


            Dim pMxDoc As IMxDocument
            pMxDoc = My.ArcMap.Document
            pMap = pMxDoc.FocusMap
            Dim pFLayer As IFeatureSelection

            'Verify there are layers located in Map
            Dim Count As Integer
            For Count = 0 To pMap.LayerCount - 1

                'Get the Transportation Improvement Line Layer from the Map
                If pMap.Layer(Count).Name = "TransportationImprovementProjects_Lines" Then
                    pFLayer = pMap.Layer(Count)
                    Dim pFCursor As IFeatureCursor = Nothing
                    pFLayer.SelectionSet.Search(Nothing, False, pFCursor)

                    Dim pF As IFeature
                    pF = pFCursor.NextFeature

                    'Get Dataset from FeatureLayer
                    Dim pEditDataset As IDataset
                    pEditDataset = pFLayer

                    If editor.EditState <> esriEditState.esriStateNotEditing Then
                        editor.StartEditing(pEditDataset.Workspace)
                    End If

                    'Update attributes with user input
                    If pFLayer.SelectionSet.Count <> 0 Then
                        pF.Value(pF.Fields.FindField("YEAR")) = txtPrjYear.Text
                        pF.Value(pF.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
                        pF.Value(pF.Fields.FindField("LOCATION")) = txtLocation.Text
                        pF.Value(pF.Fields.FindField("AGENCY")) = txtAgency.Text
                        pF.Value(pF.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
                        pF.Value(pF.Fields.FindField("COST")) = txtCost.Text
                        pF.Value(pF.Fields.FindField("DATEEDITED")) = txtDate.Text
                        pF.Value(pF.Fields.FindField("USEREDITED")) = txtUser.Text
                    End If

                    pF.Store()

                End If

            Next Count

            'Stop editing and save edits
            editor.StopEditing(True)

            'Kill form
            Me.Close()


    End Sub 
0 Kudos
NeilClemmons
Honored Contributor
What is the exact message?  You should be able to copy/paste from the Visual Studio window.
0 Kudos