Solved

An interesting observation re: VB6 & Dictionaries

Posted on 2006-07-10
5
255 Views
Last Modified: 2013-12-25
I use dictionaries extensively to keep track of data within the program.

In this case, I was using the dictionary to determine which records should be deleted.

Unfortunetley, all records were being deleted (test only, thank goodness!).

This is what I learned:

If you are stupid, like I was apparently, and get the sequence mixed up just a wee little  bit...

vKey = gRecord.Field("MasterKey")
sDictData = gDict.Item(vKey)
If Not gDict.Exists(vKey) Then SkipStuff
... process deletes

----------------

That code would ADD the value to the data dictionary, regardless of whether it is there or not!

vKey = gRecord.Field("MasterKey")
If Not gDict.Exists(vKey) Then SkipStuff
   sDictData = gDict.Item(vKey)
   ... process deletes

---------------

This works fine.

Thought I would share this... and perhaps,  if someone has an observation to share back, that would be equally interesting.
0
Comment
Question by:merydion
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
This is a typical Programming Logic Error, which is quite difficult to debug if code is not commented.
please comment the code so you and others will understand it later also.
0
 

Author Comment

by:merydion
Comment Utility
The code is commented, I just didn't do so here for brevity.
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 20 total points
Comment Utility
See also:

http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_ybdz.mspx?mfr=true

Inadvertently Adding a Key to a Dictionary

One potential problem in using the Dictionary object is that any attempt to reference an element that is not contained in the Dictionary does not result in an error. Instead, the nonexistent element is added to the Dictionary. Consider the following script sample, which creates a Dictionary, adds three key-item pairs to the Dictionary, and then attempts to echo the value of a nonexistent item, Printer 4:

When the script tries to echo the value of the nonexistent item, no run-time error occurs. Instead, the new key, Printer 4, is added to the Dictionary, along with the item value Null. As a result, the message box shown in Figure 4.9 appears.

Figure 4.9 Message Resulting from Inadvertently Adding a Key to a Dictionary

To avoid this problem, check for the existence of a key before trying to access the value of the item.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Vb.net Listview 9 76
Access 2016 VB code 9 85
VB error "Type mismatch" 2 42
Excel VBA, find a string in a column, update a cell 7 50
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now