This sample shows two possible methods for doing an attribute seleciton. The first script uses the 'Select By Attributes' dialog enabling the client to enter a specific attribute query. This macro sets a default expression that can be cleared or modified in the dialog. The second script creates an attribute query using a QueryFilter and performs the selection manually, without the aid of the 'Select By Attributes' dialog. The Java example only shows the second usage.
How to use
- Paste the code into VBA.
- Modify the default expression if desired.
- Load one or more feature layers.
- From the Macros dialog, run the AttributeQuery macro.
- Create a new query in the query builder and hit apply.
Public Sub AttributeQuery()
Dim pQueryAttributes As IQueryAttributes
'Create a new Query Attribute Dialog and set
'some necessary properties before launching it
Set pQueryAttributes = New QueryAttributes
Set pQueryAttributes.Application = Application
pQueryAttributes.SelectFeaturesInLayerOnOK = True
'Provide a default expression if desired
pQueryAttributes.Expression = """NAME"" = 'Halifax'"
'Lauch the dialog
pQueryAttributes.DoModal Application.hWnd
End Sub
Here Is a Second approach the doesn't use the 'Select By Attributes' dialog.
Public Sub SelectMapFeatures()
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pQueryFilter As IQueryFilter
Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
Set pActiveView = pMap
'For simplicity sake let's use the first layer in the map
If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
Set pFeatureLayer = pMap.Layer(0)
Set pFeatureSelection = pFeatureLayer 'QI
'Create the query filter
Set pQueryFilter = New QueryFilter
pQueryFilter.WhereClause = "NAME = 'Nova Scotia'"
'Invalidate only the selection cache
'Flag the original selection
pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
'Perform the selection
pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
'Flag the new selection
pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
End Sub