Module: Search in Category General VBA/VB6 : Searching and Sorting from Total Visual SourceBook

Binary and Linear Search routines and Soundex function in VBA and VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modSearch module.
BinarySearchVariant Procedure Use a binary search to find a value in an array. The array must be zero based and sorted for this procedure to function correctly.
Note: This function works on arrays of variants, but not a variant array.
LinearSearchVariant Procedure Use a linear search to find a value in an array. The array must be zero based and sorted for this procedure to function correctly.
Note: This function works on arrays of variants, but not a variant array.
LinearSearchVariantUnsorted Procedure Use a linear search to find a value in an array. This function is not optimized for sorted data. See LinearSearchVariant for a function that is optimized for Sorted Data. The array must be zero based and sorted for this procedure to function correctly.
Note: This function works on arrays of variants, but not a variant array.
Soundex Procedure Create a Soundex key for a string which is helpful for finding close but not exact data, such as de-duping addresses and spelling differences/typos between names. Soundex is based on the principle that English words can be distinguished by their consonants. The Soundex algorithm generates a string based on the first character of the string, followed by the consonants contained in the string.
' Example of modSearch
'
' To try this example, do the following:
' 1. Create a new form
' 2. Create a command button named 'cmdTest'
' 3. Paste all the code from this example to the new form's module.
' 4. Run the form

Private Sub cmdTest_Click()
  Dim avarIn() As Variant
  Dim lngIndex As Long
  Dim i As Integer
  Dim dblFindItem As Double

  Randomize Timer

  ' Create array
  ReDim avarIn(1000)
  For i = 0 To 1000
    avarIn(i) = i
  Next i

  ' Example of LinearSearchVariantUnsorted
  dblFindItem = Int((1000 - 1 + 1) * Rnd + 1)
  lngIndex = LinearSearchVariantUnsorted(avarIn, dblFindItem)
  Debug.Print " LinearSearchVariantUnsorted Item " & dblFindItem & " is at " & lngIndex

  ' Example of LinearSearchVariant
  dblFindItem = Int((1000 - 1 + 1) * Rnd + 1)
  lngIndex = LinearSearchVariant(avarIn, dblFindItem)
  Debug.Print "LinearSearchVariant Item " & dblFindItem & " is at " & lngIndex

  ' Example of BinarySearchVariant
  dblFindItem = Int((1000 - 1 + 1) * Rnd + 1)
  lngIndex = BinarySearchVariant(avarIn, dblFindItem)
  Debug.Print "BinarySearchVariant Item " & dblFindItem & " is at " & lngIndex

  ' Example of Soundex (defaults to 4 characters)
  Debug.Print Soundex("Microsoft Corporation")
  ' Generate a 7 character soundex
  Debug.Print Soundex("Microsoft Corporation", 7)

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