I'm trying to convert my ArcGIS 9.3 code to ArcGIS 10 via Visual Basic 2008 Express; however, I'm having a couple issues.

The goal is for a user to select a feature in ArcMap, click on this custom button, and a user form appears displaying attribute information of the selected feature. The code worked great in 9.3, but not in 10.

Currently, I can get the user form to open by itself...but if a user selects a feature and clicks the button it crashes ArcMap.

**User Form Code:**
    Open User Form
    Public Class TransLineEdit
        Inherits ESRI.ArcGIS.Desktop.AddIns.Button
        Dim pForm As New frmEdit

      Protected Overrides Sub OnClick()
            Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.ToString, "OnClick")
            End Try
        End Sub
    End Class

    **User Form Code:**
    Imports ESRI.ArcGIS.ArcMapUI
    Imports ESRI.ArcGIS.Carto
    Imports ESRI.ArcGIS.Geodatabase
    Imports ESRI.ArcGIS.Geometry
    Imports ESRI.ArcGIS.ArcMap
    Imports ESRI.ArcGIS.DataManagementTools
    Imports ESRI.ArcGIS.Desktop
    Imports ESRI.ArcGIS.Editor
    Imports ESRI.ArcGIS.Framework

    Public Class frmLineSelectAttribute
'Declare variables that are used all thru the application
Public pMxDoc As IMxDocument
Public pMap As ESRI.ArcGIS.Carto.IMap
Public pEditor As IEditor
Public Application As IAppROT

Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click

    'Kill form

    'Clear selected features
    'Dim pActiveView As ESRI.ArcGIS.Carto.IActiveView
    'pActiveView = pMap
End Sub
Shared Function AccessLayersData(ByVal pLayer As ESRI.ArcGIS.Carto.ILayer) As IFeatureClass
    Dim pFeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = pLayer
    If Not pFeatureLayer Is Nothing Then
        Return pFeatureLayer.FeatureClass
        Return Nothing
    End If
End Function

Private Sub UserForm_Initialize()

    'Get Improvement Projects FeatureLayer
    Dim pFeatLyr As ESRI.ArcGIS.Carto.IFeatureLayer
    pFeatLyr = GetLayerByTOC("TransportationImprovementProjects_Lines")

    'Get selected feature (only one)
    Dim pFeat As IFeature
    pFeat = GetSelFeat(pFeatLyr)

    'Populate form controls with feature's attribute values
    txtPrjYear.Text = pFeat.Value(pFeat.Fields.FindField("YEAR"))
    txtPrjType.Text = pFeat.Value(pFeat.Fields.FindField("PROJECTYPE"))
    txtAgency.Text = pFeat.Value(pFeat.Fields.FindField("AGENCY"))
    txtLocation.Text = pFeat.Value(pFeat.Fields.FindField("LOCATION"))
    txtDescrip.Text = pFeat.Value(pFeat.Fields.FindField("DESCRIPTION"))
    txtCost.Text = pFeat.Value(pFeat.Fields.FindField("COST"))
    txtDate.Text = pFeat.Value(pFeat.Fields.FindField("DATEEDITED"))
    txtUser.Text = pFeat.Value(pFeat.Fields.FindField("USEREDITED"))

    'Dim pDoc As IMxDocument
    'Dim pItem As ICommandItem
    'pItem = Project.ThisDocument.CommandBars.Find(arcid.Query_ZoomToSelected)

End Sub
Public Function GetSelFeat(ByVal pFeatLyr As ESRI.ArcGIS.Carto.IFeatureLayer) As IFeature

    'Gets selected feature from feature layer and returns feature
    'Call this function only when you are sure that only one feature is selected

    'Initialize the required variables
    Call Initialize()

    Dim pFeatSel As ESRI.ArcGIS.Carto.IFeatureSelection
    pFeatSel = pFeatLyr

    Dim pSelectionSet As ISelectionSet
    pSelectionSet = pFeatSel.SelectionSet

    pSelectionSet.Search(Nothing, True, pFeatSel)

    'Return the selected feature
    GetSelFeat = pFeatSel.NextFeature

End Function

Public Sub Initialize()

    'Initalize global variables
    pMxDoc = Application.Document
    pMap = pMxDoc.FocusMap

    'Instantiate the editor
    pEditor = Application.FindExtensionByName("ESRI Object Editor")

End Sub

Public Function GetLayerByTOC(ByVal lyrName As String) As ESRI.ArcGIS.Carto.IFeatureLayer

    'This function finds a feature layer based on its TOC Name
    Call Initialize()

    Dim lyrCntr As Integer
    Dim pFeatLyr As ESRI.ArcGIS.Carto.IFeatureLayer

    For lyrCntr = 0 To pMap.LayerCount - 1

        'Ensure that the layer is valid
        If pMap.Layer(lyrCntr).Valid = True Then

            'Ensure that the layer is a feature layer
            If TypeOf pMap.Layer(lyrCntr) Is ESRI.ArcGIS.Carto.IFeatureLayer Then
                pFeatLyr = pMap.Layer(lyrCntr)

                If UCase(pMap.Layer(lyrCntr).Name) = UCase(lyrName) Then
                    GetLayerByTOC = pFeatLyr
                    Exit Function
                End If
            End If
        End If
    Next lyrCntr

    'Layer not found. Show a message
    MsgBox("Layer " & lyrName & " not found !", vbCritical, "Error")

End Function

    End Class
  • Was the 9.3 version in VBA? What is the error, and what line does it occur on? You may want to include some Try Catch wrappers around your code.
    – artwork21
    Jan 4, 2012 at 1:54

1 Answer 1


Here are some suggestions:

  1. Include some additional try catch statements into your code so you can identifly what line is giving you the error.

        'Your code here
    Catch ex As Exception
                    MessageBox.Show("Caught an unspecified error in the calling code: " & vbCrLf & ex.ToString)

    End Try

  2. May want to check that Intialize is a form event, not sure in vs 08, but in vs 10 it is not.

  3. You will have to hook your vs project to the ArcMap application, eg.

            m_App = CType(hook, IApplication)
            m_MxDoc = CType(m_App.Document, IMxDocument)
            m_mxDocument = CType(m_App.Document, MxDocument)
            m_map = CType(m_MxDoc.FocusMap, Map)
  • Thanks, artwork21..I ended up scratching my code and starting again from the beginning and got it to work. However, I can not get one of my previous functions to work correctly. Before users could change the attribute data that appeared, make changes, and then click a button to save the changes. Any thoughts on starting an editing session and saving changes on a click event? Jan 4, 2012 at 22:44
  • @Phil Davies, To work with the editor take a look at this link, help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/…
    – artwork21
    Feb 3, 2012 at 13:11
  • Here is some further documentation on using an Update Cursor as opposed to an edit session if you are updating/modifying the attributes of features. help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/…
    – AHigh
    Jul 2, 2012 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.