Microsoft KB Archive/189251

= PRB: Key in VBA Collection Object Is Not Case-Sensitive =

Article ID: 189251

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic for Applications 5.0
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q189251



SYMPTOMS
When trying to add or retrieve an element of a Collection using the Key property, the Key value is not case-sensitive. This is inconsistent with Collections elsewhere in Visual Basic.



RESOLUTION
If you need a Collection with a case-sensitive key, use the Dictionary Object. The Dictionary is quite similar to a Collection, but offers expanded functionality. For additional information, please see the following article in the Microsoft Knowledge Base:

187234 Use the Dictionary Object with Visual Basic



STATUS
This behavior is by design.



Steps to Reproduce Behavior:
 Start a new Standard EXE project in Visual Basic. Form1 is created by default.  Add the following code to the Form_Load event of Form1: Dim col As New Collection col.Add Key:="TEST", Item:="some data" col.Add Key:="Test", Item:="more data"  Run the project. You should see:

"Runtime Error '457': This Key is already associated with an element of this Collection."



Additional query words: kbDSupport kbDSD kbvba kbVBp400 kbVBp500 kbNoKeyWord

Keywords: kbprb KB189251

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.