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



The information in this article applies to:

  • Microsoft Visual Basic for Applications 5.0
  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • 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 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."

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbprb KB189251