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

ID: Q189251


The information in this article applies to:
  • Microsoft Visual Basic for Applications version 5.0
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0


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:

Q187234 Use the Dictionary Object with Visual Basic


STATUS

This behavior is by design.


MORE INFORMATION

Steps to Reproduce Behavior:

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.


  2. 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" 


  3. 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 :
Version : WINDOWS:4.0,5.0
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: November 1, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.