PROBLEM

Select Features command is disabled after defining a definition query

Last Published: April 25, 2020

Description

After building a definition query using ArcObjects the Select Features command is disabled. It is not possible to select features from this layer.

Cause

Selections are not supported for joins generated using QueryDef. An Object ID column is required for a table to support selections. Joins generated using a QueryDef are not guaranteed to have a unique identifier. For example, a join generated using a QueryDef may define a 1 to many relationship. The table on the left side of the join can return multiple rows for each OID.

Solution or Workaround

The TableQueryName CoClass can be used to ensure that a table generated using QueryDef has a unique identifier. Instructions provided demonstrate how to use IQueryDef and IQueryName2 to join tables in a personal geodatabase. IQueryName2 is used to specify an OID field so that the output featureclass is selectable.


Code:
Private Sub AddQueryLayer2()

Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pWorkspace As IWorkspace
Dim pFWorkspace As IFeatureWorkspace
Dim pWSFactory As IWorkspaceFactory
Dim pFClass As IFeatureClass
Dim pQueryDef As IQueryDef
Dim pFLayer As IFeatureLayer
Dim pDataset As IDataset
Dim pWorkspaceName As IWorkspaceName
Dim pDataSetName As IDatasetName
Dim pName As IName
Dim pQueryName2 As IQueryName2

' Get the map
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap

' Get the workspace
Set pWSFactory = New AccessWorkspaceFactory
Set pWorkspace = pWSFactory.OpenFromFile("C:\TestDB.mdb", 0)
Set pFWorkspace = pWorkspace

' Define the query
Set pQueryDef = pFWorkspace.CreateQueryDef
With pQueryDef
.Tables = "Yield, YieldEvent"
.WhereClause = "Yield.EventID=YieldEvent.EventID"
End With

'Setup query so OBJECTID Field is true OID field. This will enable selections
Set pQueryName2 = New FeatureQueryName
pQueryName2.PrimaryKey = "Yield.YieldID"
pQueryName2.QueryDef = pQueryDef

Set pDataset = pFWorkspace
Set pWorkspaceName = pDataset.FullName
Set pDataSetName = pQueryName2
Set pDataSetName.WorkspaceName = pWorkspaceName
Set pName = pQueryName2
Set pFClass = pName.Open

' Add feature class as layer to the map
Set pFLayer = New FeatureLayer
Set pFLayer.FeatureClass = pFClass

pFLayer.Name = "Yields"
pFLayer.ScaleSymbols = True
pFLayer.Selectable = True

pMap.AddLayer pFLayer

End Sub

Article ID:000007072

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic