Private Function FirstLetterCaps(str As String) As String
Dim ExtractWord, NewStr As String
Dim position As Integer
position = InStr(str, " ")
While position
ExtractWord = LCase$(Left$(str, position))
NewStr = NewStr & UCase$(Left$(ExtractWord, 1)) & Mid$(ExtractWord, 2)
str = Right$(str, Len(str) - position)
position = InStr(str, " ")
Wend
ExtractWord = LCase$(str)
NewStr = NewStr & UCase$(Left$(ExtractWord, 1)) & Mid$(ExtractWord, 2)
FirstLetterCaps = NewStr
End Function
Above function is called wth str argument passed to it. First word of the str is extracted by using Left$ function and changed to LCase for sure. Then First char of the extracted word is converted to UCase while remaining are retained as Lcase, and this word is added to Newstr. The str is then deducted of the first word using Right$ function. Position is moved to next space location. As the loop so performed, the Newstr continuously accumulates words while str looses them until str is left with last word whose first char is finally converted to UCase and added to Newstr. The Newstr thus is formed with First Chars UCase which is then returned to calling procedure.
Note: With VB6, StrConv(string, vbProperCase) function performs the same task in a single line of code with effortless ease.
Enter your message below
Sign in or Join us (it's free).