VBA: Convert text to proper case (alternative to StrConv())

        10 votes: ****      16,912 views      0 comments
by Allen Browne, 20 April 2005    (Access 1, 2)

Proper()


Use this function to correct case for proper nouns. For example, if company names have been entered in all upper case, create an Update Query that calls this function to correct it. You will probably need to manually correct those that don't follow the rule: the function does not handle names like McDonald or van Leen

Function Proper (var As Variant) As Variant
' Purpose: Convert the case of var so that the first letter of each word capitalized.
   Dim strV As String, intChar As Integer, i As Integer
   Dim fWasSpace As Integer    'Flag: was previous char a space?

   If IsNull(var) Then Exit Function
   strV = var
   fWasSpace = True              'Initialize to capitalize first letter.
   For i = 1 To Len(strV)
      intChar = Asc(Mid$(strV, i, 1))
      Select Case intChar
      Case 65 To 90              ' A to Z
         If Not fWasSpace Then Mid$(strV, i, 1) = Chr$(intChar Or &H20)
      Case 97 To 122             ' a to z
         If fWasSpace Then Mid$(strV, i, 1) = Chr$(intChar And &HDF)
      End Select
      fWasSpace = (intChar = 32)
   Next
   Proper = strV
End Function

Rate this article:   Your rating: Poor Your rating: Not so good Your rating: Average Your rating: Good Your rating: Excellent



Have your say - comment on this article.

What did you think of 'VBA: Convert text to proper case (alternative to StrConv())'?

No comments yet.

Why not be the first to comment on this article?!

Have your say...

Name
E-mail (e-mail address will be kept private)
Comments
Verify Code
Verification Code


It may take 30 seconds after clicking submit - please be patient to avoid duplicate submissions.  

All comments must be approved before being displayed on this web page.  This process may take up to 24 hours.