Description
IFeatureClass:FeatureCount and ITable::RowCount return incorrect record counts after deleting a record using IFeature::Delete. This behavior only occurs with shapefiles.
Cause
This problem occurs due to a known limit in the software.
Workaround
Set IFeatureCursor equal to nothing before calling the FeatureCount or RowCount methods to return the correct row counts.
- Start ArcMap.
- Create a new UIButtonControl.
Note:
For more information on creating a UIControl, see the ArcGIS Desktop Help topic 'How to create custom commands with VBA.'
- Right-click the UIButtonControl and select View Source.
- Copy this code into the UIButtonControl's click event.
Dim StrPathToDataBase As String
StrPathToDataBase = "H:\esri_data\world_data\usa"
''This is the name of the ShapeFile...
Dim StrFeatureClassName As String
StrFeatureClassName = "States"
''This is the connection information that is used to access the WorkSpace...
Dim ppropset As IPropertySet2
Set ppropset = New PropertySet
ppropset.SetProperty "DATABASE", StrPathToDataBase
Dim pWorkSpaceName2 As IWorkspaceName2
Set pWorkSpaceName2 = New WorkspaceName
pWorkSpaceName2.WorkspaceFactoryProgID = "esriCore.ShapeFileWorkspaceFactory"
pWorkSpaceName2.ConnectionProperties = ppropset
Dim pname As IName
Set pname = pWorkSpaceName2
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pname.Open
Dim pfeatureclass As IFeatureClass
Set pfeatureclass = pFeatureWorkspace.OpenFeatureClass(StrFeatureClassName)
Debug.Print pfeatureclass.FeatureCount(Nothing) & " FeatureCount - AD - ..."
Dim pfeatcursor As IFeatureCursor
Set pfeatcursor = pfeatureclass.Search(Nothing, True)
Dim pfeat As IFeature
Set pfeat = pfeatcursor.NextFeature
If pfeat Is Nothing Then
MsgBox "Nothing"
End If
Debug.Print pfeat.Value(0)
pfeat.Delete
Dim ptable As ITable
Set ptable = pfeatureclass
Debug.Print pfeatureclass.FeatureCount(Nothing) & " FeatureCount - AD - ..."
Debug.Print ptable.RowCount(Nothing) & " From Table..."
''If this isn't set to NOTHING the count is off...
Set pfeatcursor = Nothing
''These are now correct if you set pfeatcursor = NOTHING....
Debug.Print pfeatureclass.FeatureCount(Nothing) & " FeatureCount - AD - ..."
Debug.Print ptable.RowCount(Nothing)
Note:
This code will delete a record in the test feature class
- Change the StrPathToDataBase variable to point to a directory that contains a shapefile and set the StrFeatureClassName to the name of a shapefile in that directory.