Select to view content in your preferred language

VBA - field names from GxFile/GxObject

1958
3
Jump to solution
10-21-2010 07:16 AM
LornaMurison
Regular Contributor
Hi, I am trying to populate a combobox with the field names of a polygon feature class selected by the user from a GxDialog.

Here is the code so far, I can't figure out how to get from GxObject to the field names.

Thanks

Private Sub btnCatch_Click()

'Allow the user to select the catchments feature class
Dim pGxDialog As IGxDialog
Set pGxDialog = New GxDialog

pGxDialog.Title = "Select Catchments Feature Class"
pGxDialog.ButtonCaption = "Select"
pGxDialog.AllowMultiSelect = False
pGxDialog.StartingLocation = "Catalog"

Dim pGxFilter As IGxObjectFilter
Set pGxFilter = New GxFilterPolygonFeatureClasses
Set pGxDialog.ObjectFilter = pGxFilter

Dim pLayerFiles As IEnumGxObject

pGxDialog.DoModalOpen 0, pLayerFiles

Dim pLayerFile As IGxObject
Set pLayerFile = pLayerFiles.Next


'Display the name of the selected feature class in the text box
txtboxSelectedFC.Text = pLayerFile.Name

'Populate combobox with the field names from that feature class
Dim pGxFile As IGxFile
Set pGxFile = New GxFile
pGxFile.Path = pLayerFile.FullName

Dim pGxLayer As IGxLayer




End Sub
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
This should work

    Dim pGxDialog As IGxDialog
    Set pGxDialog = New GxDialog
    
    pGxDialog.Title = "Select Catchments Feature Class"
    pGxDialog.ButtonCaption = "Select"
    pGxDialog.AllowMultiSelect = False
    pGxDialog.StartingLocation = "Catalog"
    
    Dim pGxFilter As IGxObjectFilter
    Set pGxFilter = New GxFilterPolygonFeatureClasses
    Set pGxDialog.ObjectFilter = pGxFilter
    
    Dim pLayerFiles As IEnumGxObject
    
    pGxDialog.DoModalOpen 0, pLayerFiles

    Dim pLayerFile As IGxObject
    Set pLayerFile = pLayerFiles.Next

    Dim pFLayer As IFeatureLayer
    Set pFLayer = New FeatureLayer
    Set pFLayer.FeatureClass = pLayerFile.InternalObjectName.Open
    
    Dim pFields As IFields
    Set pFields = pFLayer.FeatureClass.Fields
    
    Dim i As Integer
    For i = 0 To pFields.FieldCount - 1
        ComboBox1.AddItem pFields.Field(i).Name
    Next

View solution in original post

0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor
This should work

    Dim pGxDialog As IGxDialog
    Set pGxDialog = New GxDialog
    
    pGxDialog.Title = "Select Catchments Feature Class"
    pGxDialog.ButtonCaption = "Select"
    pGxDialog.AllowMultiSelect = False
    pGxDialog.StartingLocation = "Catalog"
    
    Dim pGxFilter As IGxObjectFilter
    Set pGxFilter = New GxFilterPolygonFeatureClasses
    Set pGxDialog.ObjectFilter = pGxFilter
    
    Dim pLayerFiles As IEnumGxObject
    
    pGxDialog.DoModalOpen 0, pLayerFiles

    Dim pLayerFile As IGxObject
    Set pLayerFile = pLayerFiles.Next

    Dim pFLayer As IFeatureLayer
    Set pFLayer = New FeatureLayer
    Set pFLayer.FeatureClass = pLayerFile.InternalObjectName.Open
    
    Dim pFields As IFields
    Set pFields = pFLayer.FeatureClass.Fields
    
    Dim i As Integer
    For i = 0 To pFields.FieldCount - 1
        ComboBox1.AddItem pFields.Field(i).Name
    Next

0 Kudos
DuncanHornby
MVP Notable Contributor
Here is an alternate but very similar approach to Ken's code:

Public Sub Test()
    ' Create dialog
    Dim pGxDialog As IGxDialog
    Set pGxDialog = New GxDialog
    
    ' Define filter
    Dim pGxObjectFilter As IGxObjectFilter
    Set pGxObjectFilter = New GxFilterPolygonFeatureClasses
    
    ' Set dialog properties and open
    Dim pEnumGxObject As IEnumGxObject
    With pGxDialog
        .Title = "Select Catchments Feature Class"
        .ButtonCaption = "Select"
        .AllowMultiSelect = False
        .StartingLocation = "Catalog"
        Set .ObjectFilter = pGxObjectFilter
        .DoModalOpen 0, pEnumGxObject
    End With
    
    Dim pGXObject As IGxObject
    Set pGXObject = pEnumGxObject.Next
    
    Dim pGXdataset As IGxDataset
    Set pGXdataset = pGXObject
    
    Dim pDataset As IDataset
    Set pDataset = pGXdataset.Dataset
    
    Dim pFeatureClass As IFeatureClass
    Set pFeatureClass = pDataset
    
    ' Get fields a cycle through them
    Dim pFields  As IFields
    Set pFields = pFeatureClass.Fields
    Dim i As Byte
    Dim pField As IField
    For i = 0 To pFields.FieldCount - 1
        Set pField = pFields.Field(i)
        MsgBox pField.Name
    Next i
End Sub


Duncan
0 Kudos
LornaMurison
Regular Contributor
Thank-you both very much,
I used your code, Ken and it worked perfectly.  I'd mark the question as answered if I was able to 🙂

Duncan,
I thought I had tried to go through the dataset interface with the lines below, but I had problems with it.

Dim pGXdataset As IGxDataset
Set pGXdataset = pGXObject
Dim pDataset As IDataset
Set pDataset = pGXdataset.Dataset
0 Kudos