English

How To: Use an IFeatureCursor with IQueryDef

Summary

To use an IQueryDef with an IFeatureCursor, you assign a name to the QueryDef using IFeatureContainer and use this name like a feature class name.

Procedure

The following code connects to a geodatabase, sets up an IQueryDef, and does a table join between the counties feature class and the farms table. Note only three fields are included in the subfields: the shape, and one field from each table. These columns are the only fields that can be accessed through the IQueryDef object. A IFeatureClassContainer is used to assign the name MyJoin to the IQueryDef. This name is used to open an IFeatureCursor.


Code:
'-- Connect to Ggodatabase (SDE)
Dim pPropset As IPropertySet
Set pPropset = New PropertySet
With pPropset
.SetProperty "Server", "testserver"
.SetProperty "Instance", "5250"
.SetProperty "Database", ""
.SetProperty "user", "test"
.SetProperty "password", "test"
.SetProperty "version", "SDE.DEFAULT"
End With

Dim pFact As IWorkspaceFactory
Set pFact = New SdeWorkspaceFactory

'-- Open the feature dataset
Dim pWorkspace As IWorkspace
Set pWorkspace = pFact.Open(pPropset, Me.hWnd)

'-- Create feature workspace
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pWorkspace

'-- Create query definition
Dim pQueryDef As IQueryDef
Set pQueryDef = pFeatureWorkspace.CreateQueryDef

'-- Provide list of tables to join
pQueryDef.Tables = "counties,farms"

'-- Retrieve the fields from all tables
pQueryDef.SubFields = "shape,counties.name,farms.NO_FARMS87"

'-- Set up join
pQueryDef.WhereClause = "counties.fips = farms.fips"

'-- Create FeatureDataset. Note the use of .OpenFeatureQuery. The
'-- MyJoin name was the result of the query def and is used in
'-- place of a feature class name.
Dim pFeatureDataset As IFeatureDataset
Set pFeatureDataset = pFeatureWorkspace.OpenFeatureQuery("MyJoin", pQueryDef)

'-- Open Layer to test against
Dim pFeatureClassContainer As IFeatureClassContainer
Set pFeatureClassContainer = pFeatureDataset

Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureClassContainer.ClassByName("MyJoin")

Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureClass.Search(Nothing, True)