Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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
            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

            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
                End If

                Me.txtValue.Text = strValue

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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