When Scripting.Dictionary keys wont die!!!

I’ve just spent 2 hours debugging an amazing bug. At least I think its a bug.

I am using a Scripting.Dictionary object to act like an in memmopry database. I load key pair into it via a text file. It works fine. Then I added some functionaility to remove a key. It works like this.

  1. Test for key using DictObj.exists
  2. Remove said key using DictObj.remove
  3. Kill text file using Kill (path) (ekk!)
  4. Make new text file
  5. Save data from DictObj to the new text file.

This part worked like a dream. But! wait, these keys don’t  want to die.

Someplace esle i have a button that saves/updates the key in the dictObject to the text file.

When I called this (and it uses the same sub to write to the text file!!!) the key that i had removed would be writted  to the text time – at the bottom of the list. Amazing.

I used all my debugging foo, and tracked the DictObj through all code paths. What was amazing was that the DictObj was upated (confirmed via watch window), when I saved it to a text file from the delete code it would behave as expected. But when that same sub was called from the save button, as it looped through the DictObj to write the text file, it would magically add the keys that had been removed!!!

What was even more amazing was that I could close down VBA IDE, AND (in this case) PowerPoint, reopen, then click the save button and it would still have the old, removed keys in it!

I looked all over the web but could not find anything pointing out what the issue was. Maybe it just the particularly way I’m using the DictObj, in the end I used .removeall and them relaoded it from the text file in the delete sub. A work around of sorts, but, bloody hell, what an odd bug!!!

Happy to say as of now, all seems to be working as expected, but I’m not counting my chickens!

Bloodly VBA!

Advertisements