Question

Has anyone ever run into breakpoints in the VBA console actually introducing errors? How could this possibly happen?

The short version of the story is that I have some code that works as expected when executed with no breakpoints toggled on. However, when breakpoints were toggled on in a few locations (in a subroutine and in a class method), the code would throw inexplicable errors when stepping through the code. I would ordinarily find this an extremely unlikely explanation, but after spending 2.5 days trying to debug the code only to discover that it executes properly with breakpoints turned off, I'm having trouble understanding what could have been the problem.

Details:

Two different errors occur when breakpoints are on.

Run-time error '457': This key is already associated with an element of this collection

and

Run-time error '424': Object required

The 457 error would occur after stepping through the code from the breakpoint to the line

 dictE.Add M.SubMatches.Item(0), tmpQ

where one of the conditions for reaching this line of code was that dictE.Exists(M.SubMatches.Item(0)) was False.

The 424 error would occur after choosing Debug when the 457 error occurred. In a similarly zany manner, the error would be thrown by the (different) line

 pathE = dictE.Item(Matches.Item(i).SubMatches.Item(0)).InFile

where one of the conditions for reaching this line of code was that dictE.Exists(Matches.Item(i).SubMatches.Item(0)) was True (and the .InFile property of the object is always defined).

Both of these errors occur in the same method of a custom class. I'd like to post all of the relevant code, but it's work-related and proprietary. Any ideas why this would happen? Is this a known issue? I tried googling for reports of similar problems, but I found nothing.

Was it helpful?

Solution

Not sure if it relates to your issue, but here's an illustration of how unexpected things can happen while using a dictionary and halted on a breakpoint...

Place a breakpoint on Debug.Print "Break" and then run this code:

Sub Tester()

    Dim d As Object

    Set d = CreateObject("scripting.dictionary")

    d.Add "K1", "first"
    d.Add "K2", "second"
    d.Add "K3", "third"

    Debug.Print d.Count '>>3 - as expected
    Debug.Print "Break" ' Enter '? d("blah")' in Immediate pane
                        '                      while halted here...
    Debug.Print d.Count '>> 4 ! key "blah" was added...

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top