ACC: Sample Function to Capitalize Words and PhrasesID: Q110391
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article describes a sample user-defined function called Proper() that
you can use to capitalize the first character of a word or set of words.
This article contains two versions of the Proper() function:
To create the Proper() function, enter the following code in a module:
'**********************************************************
' Declarations section of the module
'**********************************************************
Option Explicit
'==========================================================
' The following function is designed for use in the AfterUpdate
' property of form controls.
' Features:
' - Leading spaces do not affect the function's performance.
' - "O'Brian" and "Wilson-Smythe" will be properly capitalized.
' Limitations:
' - It will change "MacDonald" to "Macdonald."
' - It will change "van Buren" to "Van Buren."
' - It will change "John Jones III" to "John Jones Iii."
'==========================================================
Function Proper (AnyValue As Variant) As Variant
Dim ptr As Integer
Dim TheString As String
Dim currChar As String, prevChar As String
If IsNull(AnyValue) Then
Exit Function
End If
TheString = CStr(AnyValue)
For ptr = 1 To Len(TheString) ' Go through each char. in
' string.
currChar = Mid$(TheString, ptr, 1) ' Get the current character.
Select Case prevChar ' If previous char. is a
' letter,'this char. should be
' lowercase.
Case "A" To "Z", "a" To "z"
Mid(TheString, ptr, 1) = LCase(currChar)
Case Else
Mid(TheString, ptr, 1) = UCase(currChar)
End Select
prevChar = currChar
Next ptr
AnyValue = theString
End Function
AnyValue= theString
End Function
Proper=CVar(theString)
End Function
Field: Full Name:Proper([Last Name] & " " & [First Name])
This will concatenate the first and last names and capitalize the
first letter of each.
TextBox
---------------------------------
ControlName: AddressP
ControlSource: =Proper([Address])
NOTE: In calculated fields, the ControlName must be unique.
Action: SetValue
Item: Screen.ActiveControl
Expression: Proper(Screen.ActiveControl)
NOTE: You can call this macro from the AfterUpdate property of a control
on a form. This has the same affect as the first version of the Proper()
function.
For more information about methods of calling a function, please see the
following articles in the Microsoft Knowledge Base:
Q97514 ACC: Writing Functions Called from Events or Expressions
For more information about manipulating text strings, please see the
following articles in the Microsoft Knowledge Base:
Q115915 ACC: Sample Expressions to Extract Portion of Text String
Additional query words: proper
Keywords : kbprg ExrStrg
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 2, 1999