Knowledge Base

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

Article ID: 189251

Article Last Modified on 7/15/2004


APPLIES TO


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.

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: kbprb KB189251