CodeItBetter Programming Another VB Programming Blog

How To Capitalize The First Character Of Each Word In A String/Phrase.

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'String Manipulation - How To Capitalize The First Character Of Each Word In A String/Phrase.
'Example Usage:
'Dim strTest As String
'strTest = "microsoft access 97"
'
'MakeInitialCaps strTest
'Debug.Print strTest  'Displays: "Microsoft Access 97"

Public Function MakeInitialCaps(ByRef strArg As String)
    Const strSpace As String = " "
    Dim ID As Long
    'Remove Leading and Trailing Spaces
    strArg = Trim$(strArg)
    'Capitalize first letter in String
    strArg = UCase(Left(strArg, 1)) & Mid(strArg, 2)
    'Loop through string looking for spaces and
    'Capitalizing the next Character
    ID = 1 'Set index into string
    Do
        ID = InStr(ID, strArg, strSpace, _
                   vbBinaryCompare)
        If ID > 0 Then
            strArg = Left$(strArg, ID) & UCase$(Left$(Right$(strArg, Len(strArg) - ID), 1)) & Mid$(strArg, ID + 2)
            ID = ID + 1
        Else
            Exit Do
        End If
    Loop
End Function
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.