Class: Dictionary in Category General VBA/VB6 : Abstract Data Types from Total Visual SourceBook

This class uses the Dictionary object in VBA and VB6.

Visual Basic 6 includes the Windows Scripting Host runtime component. This dynamic link library adds the ability to create and use Dictionary objects, which are similar to Collections, but are more powerful and can also be used in VBA. Unfortunately, several things are lacking in the Dictionary object, and the documentation provided with VB6 for this new object is thin at best.

This class accomplishes three things:

  1. Works around the bugs in the version of the Windows Scripting Runtime that ships with VB6.
  2. Provides detailed examples of how to use the Dictionary object.
  3. Provides additional functionality such as sorting the Dictionary.
Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CDictionary class.
CompareMode Property Get the setting of the CompareMode property. This property is used by the Exists method to find keys in the Dictionary. Use Binary mode to do exact comparison. Use Text mode to case-insensitive comparisons.
Count Property Get the count of objects in the Dictionary.
Dictionary Property Get the handle to the current Dictionary object. Use this property for direct access to the Dictionary object.
Item Property Get the item in the Dictionary with the specified key.
Items Property Get the Dictionary items as an array.
Key Property Get the key in the Dictionary with the specified key.
Keys Property Get the Dictionary keys as an array.
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
Add Method Adds the specified item to the Dictionary.
Exists Method Determine if the specified item exists in the Dictionary. Comparison is done according to the setting of the CompareMode property.
Remove Method Removes the specified item from the Dictionary. A runtime error occurs if the specified item does not exist.
RemoveAll Method Removes all items from the Dictionary.
SortDictionary Method Sorts the Dictionary items. This method copies all keys and items to arrays, sorts the array, removes all items from the Dictionary, and then re-populates the Dictionary with the contents of the sorted array.
DoSort Private Sorts the passed variant array.
' Example of CDictionary
'
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_CDictionary()
  ' Comments: Example of the CDictionary class using the Windows Scripting Runtime component SCRRUN.DLL using VBA and VB6.
  '           See the results in the Immediate Window.

  Dim clsDictionary As CDictionary
  Dim varItems As Variant
  Dim varItem As Variant

  Set clsDictionary = New CDictionary

  clsDictionary.Add "K1", "John"
  clsDictionary.Add "K2", "Mary"
  clsDictionary.Add "K3", "Louise"

  Debug.Print "Item count: " & clsDictionary.Count

  If clsDictionary.Exists("K2") Then
    Debug.Print "Mary exists"
  Else
    Debug.Print "Mary doesn't exist"
  End If

  clsDictionary.SortDictionary True

  varItems = clsDictionary.Items
  For Each varItem In varItems
    Debug.Print "Item: " & varItem
  Next varItem

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS