Web Scripting
Active Server Pages
Online Tools
Click Here


What's New


Code Downloads

Code Snippets

Message boards

Tool Box


Mailing List
Receive free code snippets and notices when this site is updated.



Tell a friend about this article

Converting Abbreviations to Words

Do users of your applications have common terms they use frequently? I recently developed an application for physiotherapists, they often abbreviate long medical terms.  This article shows you how to automatically convert these abbreviations to full words as the user types.

To demostrate what I mean look at the screenshots below.  The user types "ASP", as soon as they press the space bar "ASP" is converted to "Active Server Pages".

How is this done.

Through an easily three step program.

Step 1. - Load the Abbreviations List

The list of abbreviations is held in a collection and each abbreviation/word pair is held in an instance of a class called clsAbbreviation.

You could implement this list of abbreviations another way if you like.  As a array of user defined types for example.

To follow this example, create a new VB Exe project and add a class called clsAbbreviation.

To this class add the code:

Public Abbreviation As String
Public Word As String

In the general declaractions section of the form add:

Private Abbreviations As Collection

This variable holds the clsAbbreviation objects.

Now we're are ready to load the list of a abbreviations.  In this example we just add the abbreviations thru code.  But a better solution may be to load them from a file or database.

Place this code into the Form_Load() event.

Private Sub Form_Load()

Dim myAbbreviation As clsAbbreviation
Set Abbreviations = New Collection

Set myAbbreviation = New clsAbbreviation
myAbbreviation.Abbreviation = "ASP"
myAbbreviation.Word = "Active Server Pages"
Abbreviations.Add myAbbreviation

Set myAbbreviation = New clsAbbreviation
myAbbreviation.Abbreviation = "VB"
myAbbreviation.Word = "Visual Basic"
Abbreviations.Add myAbbreviation

Set myAbbreviation = New clsAbbreviation
myAbbreviation.Abbreviation = "HTML"
myAbbreviation.Word = "Hyper Text Markup Language"
Abbreviations.Add myAbbreviation

End Sub

Step 2. - Check to see if a new word has been typed

Add a text box to the form and add this code to the textbox's KeyPress event.

Private Sub Text1_KeyPress(KeyAscii As Integer)
    ' If the space bar or enter key where pressed
    If KeyAscii = 13 Or KeyAscii = 32 Then
      ConvertAbbreviations Text1
    End If
End Sub

Step 3. - Check if the typed word is an abbreviation.

Below is the sub routine that does most of the work.  It's pretty simple it just does the following:

  1. Finds what the previous typed word was.
  2. Checks to see if the word is an abbreviation.
  3. If the word is an abbreviation, it replaces it with the full word.
' Looks at the last word typed to see if it is an a
' abbreviation.
Sub ConvertAbbreviations(ByVal txtBox As TextBox)
    Dim sLastWord As String
    Dim lLastWordStart As Long
    Dim lLastLineBreak As Long
    Dim lSelStart As Long
    Dim lWordLength As Long
    Dim myAbbreviation As clsAbbreviation
    lSelStart = txtBox.SelStart - 1
    If lSelStart <= 0 Then Exit Sub
    ' Find the position of the last Space and the last new line
    lLastWordStart = InStrRev(txtBox.Text, " ", lSelStart)
    lLastLineBreak = InStrRev(txtBox.Text, vbLf, lSelStart)
    ' If a Line Break occured after the space then
    ' the start of the most recently typed word
    ' is at the line break.
    If lLastLineBreak > lLastWordStart Then
        lLastWordStart = lLastLineBreak
    End If
    ' The length of the word
    lWordLength = (lSelStart + 1) - lLastWordStart
    ' sLastWord will now contain the last typed word.
    sLastWord = Mid$(txtBox.Text, lLastWordStart + 1, lWordLength)
    'Compare each abbreviation with the word
    'we have just found.
    For Each myAbbreviation In Abbreviations
        If StrComp(sLastWord, myAbbreviation.Abbreviation) = 0 Then
            ' Select the abbreviation that was typed.
            txtBox.SelStart = lLastWordStart
            txtBox.SelLength = lWordLength
            ' Replace the abbreviation with the full word.
            txtBox.SelText = myAbbreviation.Word
            Exit For
        End If
End Sub

To save your wrists you can download the example here.

Author: Michael Jones