Visual Basic Tips / Propercase routine for name and address fields
Suppose you have an application which has a user interface with text boxes used for entry fields for things like names and addresses. You suspect the user might use sloppy capitalization and your sorting routines depend on properly cased entries. What do you do? Include a readme asking the user to brush up on their data entry skills? No, add the following module to your project, and use it to do the propercasing when the user tabs out of the field. 8-)
Listing of ProperCase.bas
Public Function ProperCaseString(AString As String)
'will take any string and
uppercase the first letter of each word
in the string
'capitalize the
first word in the string
before checking to see if there are any more words
If AString = "" Then
Exit Function 'there
was no entry in the field
'store the
first letter in the string
LeftString = Left(AString, 1)
'store the
rest of the string
RightString = LCase$(Right(AString, Len(AString)
- 1))
'uppercase the
first letter of the string
LeftString = UCase(LeftString)
'put the newly
capitalized first word and the
rest of the string back into Astring variable
AString = LeftString & RightString
'find the
first space in the string
SpaceLocation = InStr(1, AString, "
", 1)
While SpaceLocation <> 0 'if
there was a
space fall into the inner loop to process the rest of the words
'store
the already
processed words in the string
LeftString = Left(AString,
SpaceLocation)
'store
the unprocessed
portion of the string
RightString = Right(AString,
Len(AString) - SpaceLocation)
'get
the first letter
of the unprocessed portion of the string
CapString =
Left(RightString,
1)
'uppercase
that letter
CapString = UCase(CapString)
'store
the rest of the
unprocessed portion of the string after the newly capitalized
letter
RightString =
LCase$(Right(RightString,
Len(RightString) - 1))
'put
the string back
in Astring variable with the newly capitalized word
AString = LeftString
& CapString & RightString
'check
to see if there
is another space in the string
SpaceLocation =
InStr(SpaceLocation
+ 1, AString, " ", 1)
Wend
ProperCaseString = AString
End Function
End of ProperCase.bas listing
To call the function above, use a call similar to the following:
TextOne.Text = ProperCaseString(TextOne.Text)
Here is the same code converted to propercase all of the entries in the Name field of a UDT array.
Listing of ProperCaseArray function
Public Sub ProperCaseArray(TheArray() As
Person)
'will take any string and
uppercase the first letter of each word
in the string
Dim UpperBound As Long 'number of elements in
passed in array
UpperBound = UBound(TheArray)
Dim LoopCount As Long
LoopCount = -1 'initialize
loop counter
for do loop
Do
LoopCount = LoopCount
+ 1 'increment loop counter
'capitalize the first
word in the string before checking to see if there are any more
words
'store the first
letter in the string
LeftString =
Left(TheArray(LoopCount).Name,
1)
'store
the rest of the
string
RightString =
Right(TheArray(LoopCount).Name,
Len(TheArray(LoopCount).Name) - 1)
'uppercase
the first
letter of the string
LeftString =
UCase(LeftString)
'put
the newly
capitalized first word and the rest of the string back into the
array
TheArray(LoopCount).Name
= LeftString & RightString
'find
the first space
in the string
SpaceLocation = InStr(1,
TheArray(LoopCount).Name, " ", 1)
While SpaceLocation
<> 0 'if there was a space
fall into the inner loop to
process the rest of the words
'store the already processed words in the string
LeftString = Left(TheArray(LoopCount).Name, SpaceLocation)
'store the unprocessed portion of the string
RightString = Right(TheArray(LoopCount).Name,
Len(TheArray(LoopCount).Name)
- SpaceLocation)
'get the first letter of the
unprocessed portion of the string
CapString = Left(RightString, 1)
'uppercase that letter
CapString = UCase(CapString)
'store the rest of the unprocessed
portion of the string after
the newly capitalized letter
RightString = Right(RightString, Len(RightString) - 1)
'put the string back in the array with the newly capitalized word
TheArray(LoopCount).Name = LeftString & CapString &
RightString
'check to see if there is another
space in the string
SpaceLocation = InStr(SpaceLocation + 1, TheArray(LoopCount).Name,
" ", 1)
Wend
'loop through
all elements in the array
Loop Until LoopCount = UpperBound
End Sub
End of ProperCaseArray listing
And here's how you call the routine and pass it the array
ProperCaseArray People()
Note that People() must be a publicly declared UDT array with at least the following structure.
Public Type Person
Name As
String
'10 bytes + length
End Type
Public People() As Person