Link to home
Start Free TrialLog in
Avatar of Aquarus
Aquarus

asked on

Run-time error 457

My application began giving me this error.  please advise how to fix it safely.

Run-Time error '457' . This key is already associated with an element of this collection.
Avatar of northfields
northfields

Hi Aquarus

Is this a VB6 application with a tree view?
Avatar of Meir Rivkin
its because Key in Collection Object Is Not Case-Sensitive.
u probably added 2 identical case-insensitive keys
something like:

Dim s As Collection
s.Add("TEST", 1)
s.Add("test", 1)
the second line will raise Run-Time error '457'
put break point in every line in your code which uses key-value data structure like Hashtable, Dictionary and so on.

before each line u need to verify that the key does not exists already in your collection.
Avatar of Aquarus

ASKER

Thank you, sedgwick.  I will try to debug on the key.  
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aquarus

ASKER

Thank you.  I debugged as you suggested and found the cause for the error.
'This is the test given in interview, while running the code i getting the run time error 457... this key is already associated with the collection.
' the line were i get error is at 'DicoDeal.Add Key, Item.Please solve as soon as possible. Thank you



Sub RunTest2()

    Dim i As Integer
    Dim WsTest2 As Worksheet
    Dim Key As String
    Dim Item As String
    Dim DicoDeal As New Dictionary
    Set WsTest2 = ThisWorkbook.Sheets("TEST 2")
   
    i = 1
    While WsTest2.Range("A1").Offset(i).Value <> ""
        Key = WsTest2.Range("A1").Offset(i).Value
        Item = WsTest2.Range("C1").Offset(i).Value
   
        DicoDeal.Add Key, Item
 
   
        i = i + 1
    Wend
   

End Sub