[GIS] How to get the “queried” value from attribute table and put it in a textbox

arcobjectsvba

I am new in VBA, I have a problem about querying an attribute table….:

I have a table of a layer (wish.shp) with the fields: owner_ID, Value, Area.

I made 3 textboxes in a userform, I input the owner_ID, in one textbox, I want to get the corresponding value and area and display in other textboxes.

Best Answer

You can use a FeatureCursor to grab the field value. The code below is looking at a selected record. Then, just assign your strValue to your text box.

            Dim hook As Object
            Dim App as IApplication = CType(hook, IApplication)
            MxDoc = CType(App.Document, IMxDocument)

            Dim pMap As IMap
            pMap = MxDoc.FocusMap
            Dim pEnumLayer As IEnumLayer
            pEnumLayer = pMap.Layers
            pEnumLayer.Reset()
            Dim pLayer As ILayer
            pLayer = pEnumLayer.Next
            Dim pFLayer As IFeatureLayer
            Do While Not pLayer Is Nothing
                If pLayer.Name = strLayerName And TypeOf pLayer Is IFeatureLayer Then ' Substitute your real layer name
                    pFLayer = TryCast(pLayer, IFeatureLayer)
                    Exit Do
                End If
                pLayer = pEnumLayer.Next
            Loop

            If pFLayer Is Nothing Then
                MsgBox("No GetFeatuerValue Layer Found")
                Exit Function
            End If


                Dim pFSel As IFeatureSelection
                pFSel = pFLayer
                Dim pSelSet As ESRI.ArcGIS.Geodatabase.ISelectionSet2
                pSelSet = pFSel.SelectionSet
                If pSelSet.Count < 1 Then
                    MsgBox("No Features Selected")
                    Return Nothing
                End If

                Dim pFCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor = Nothing
                pSelSet.Search(Nothing, False, pFCursor)
                Dim pFeature As IFeature
                pFeature = pFCursor.NextFeature ' Get First Feature
                If pFeature Is Nothing Then
                    MsgBox("No Features Selected!") ' Just in case, but probably already taken care of earlier
                    Return Nothing
                End If

                Dim lFIndex As Long
                Dim strValue As String

                lFIndex = pFCursor.FindField("MYField") ' Replace with your field name
                If lFIndex > -1 Then
                    Dim fValue As String ' Substute the correct data type to match the field type
                    Do While Not pFeature Is Nothing
                        fValue = pFeature.Value(lFIndex)
                        ' Do something with the value stored in fValue
                        pFeature = pFCursor.NextFeature
                        strValue = fValue
                    Loop
                End If

                Me.txtValue.Text = strValue

                Marshal.ReleaseComObject(pFCursor)