PRB: 'For Each' with 'Delete' Doesn't Work for DAO Collections (129928)
The information in this article applies to:
- Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
- Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
- Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
This article was previously published under Q129928 SYMPTOMS
Using the For Each method to iterate through and delete objects from DAO
collections fails to delete all of the objects in the collection.
CAUSE
The Jet Data Access Object (DAO) automatically compacts collections. This
causes the indices of the objects within the collections to change
dynamically. The For Each statement identifies all of the items in a
collection that it will iterate through only at the initial entry into the
loop.
RESOLUTION
Use the standard For...Next statement to iterate through DAO collections.
Looping should start at the upper bound of the collection and step
backwards through the collection. This allows the compaction to happen
normally without affecting the loop. For example:
For I = DB.TableDefs.Count - 1 To 0 Step - 1
STATUS
This behavior is by design.
Modification Type: | Major | Last Reviewed: | 12/9/2003 |
---|
Keywords: | kbprb KB129928 |
---|
|