Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capitalize First Letter-That's it, Nothing Else. 3

Status
Not open for further replies.

SMHSleepy

Technical User
Sep 8, 2009
174
CA
Hello, I can't believe something as seemingly simple as this should need lines of code to create so please tell me if I'm missing something easier.

I want to add an input mask to my form for names so that only the first letter get's capitalized. I don't want the rest of the letters necessarily in small case and I want to be able to add other characters (e.g. hyphen). For example, a surname such as Smith-McDonald: The "S" would automatically get capitalized but the user has to manually capitalize the "M" and "D" in McDonald.

Any suggestions?
 
I don't think you can do this with a plain Input Mask. Something like [TT]>C<CCCCC[/TT] will force the first character to uppercase and force all the rest to lower case but I don't know of a switch which would turn off the effect of the [TT]>[/TT] so that the first character is capitalised but the rest remain as entered by the user.

All that I can suggest is to ignore the Input Mask and to put some code in the LostFocus to force the correct format:
Code:
Private Sub Text1_LostFocus()

Text1.Value = UCase(Left(Text1.Value, 1)) & Mid(Text1.Value, 2)

End Sub

Geoff Franklin
 



Hi,

This is NOT a very good idea, IMHO. Capitalization ought to totally controlled at the time of entry.

What about names like de Boer Food.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thank you Alvechurchdata for the code.

SkipVought, great point. Of course, that makes me ask if it's possible to have the first character act in an opposite manner to the rest of the characters. That is, default is capital, but if hold shift first, it becomes small.
 
I'm not a programmer, but I've used this for several fields.

In any module, add this:

Public Function CapitalizeFirst(Str)

' Make first letter in field uppercase; leave other
' letters as typed.
Dim strTemp As String
strTemp = Trim(Str)
CapitalizeFirst = UCase(Left(strTemp, 1)) & Mid(strTemp, 2)

End Function


Then add this to a field's afterupdate:

Private Sub LastName__AfterUpdate()

' Capitalize first letter in LastName name.
If Not (IsNull(Me!LastName)) Then
Me!LastName = CapitalizeFirst((Me!LastName))
End If

End Sub
 

Actually, far from being 'simple' this, by the very nature and variety of names, is a complex problem! Here's a hack by a couple of gentlemen named Jay Holovacs, Dev Ashish and Arvin Meyer.

Copy the functions and place them in an standard Module. When asked, name the Module NameCaps.

Code:
'************** Code Start *************
'This code was originally written by Jay Holovacs.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Jay Holovacs
'
'© 1998-2004, Dev Ashish & Arvin Meyer, All rights reserved. Optimized

Public Function mixed_case(str As Variant) As String
'returns modified string, first character of each word us uppercase
'all others lower case
Dim ts As String, ps As Integer, char2 As String
    If IsNull(str) Then
        mixed_case = ""
        Exit Function
    End If
    str = Trim(str) 'added 11/22/98
    If Len(str) = 0 Then
        mixed_case = ""
        Exit Function
    End If
    ts = LCase$(str)
    ps = 1
    ps = first_letter(ts, ps)
    special_name ts, 1 'try to fix the beginning
    Mid$(ts, 1) = UCase$(Left$(ts, 1))
    If ps = 0 Then
        mixed_case = ts
        Exit Function
    End If
    While ps <> 0
        If is_roman(ts, ps) = 0 Then 'not roman, apply the other rules
            special_name ts, ps
            Mid$(ts, ps) = UCase$(Mid$(ts, ps, 1)) 'capitalize the first letter
        End If
        ps = first_letter(ts, ps)
    Wend
    mixed_case = ts
End Function
Private Sub special_name(str As String, ps As Integer)
'expects str to be a lower case string, ps to be the
'start of name to check, returns str modified in place
'modifies the internal character (not the initial)

Dim char2 As String
char2 = Mid$(str, ps, 2) 'check for Scots Mc
If (char2 = "mc") And Len(str) > ps + 1 Then '3rd char is CAP
    Mid$(str, ps + 2) = UCase$(Mid$(str, ps + 2, 1))
End If

char2 = Mid$(str, ps, 2) 'check for ff
If (char2 = "ff") And Len(str) > ps + 1 Then 'ff form
    Mid$(str, ps, 2) = LCase$(Mid$(str, ps, 2))
End If

char2 = Mid$(str, ps + 1, 1) 'check for apostrophe as 2nd char
If (char2 = "'") Then '3rd char is CAP
    Mid$(str, ps + 2) = UCase$(Mid$(str, ps + 2, 1))
End If

Dim char3 As String
char3 = Mid$(str, ps, 3) 'check for scots Mac
If (char3 = "mac") And Len(str) > ps + 1 Then 'Mac form
    Mid$(str, ps + 3) = UCase$(Mid$(str, ps + 3, 1))
End If

Dim char4 As String
char4 = Mid$(str, ps, 4) 'check for Fitz
If (char4 = "fitz") And Len(str) > ps + 1 Then 'Fitz form
    Mid$(str, ps + 4) = UCase$(Mid$(str, ps + 4, 1))
End If

End Sub
Private Function first_letter(str As String, ps As Integer) As Integer
'ps=starting point to search (starts with character AFTER ps)
'returns next first letter, 0 if no more left
'modified 6/18/99 to handle hyphenated names
Dim p2 As Integer, p3 As Integer, s2 As String
    s2 = str
    p2 = InStr(ps, str, " ") 'points to next blank, 0 if no more
    p3 = InStr(ps, str, "-") 'points to next hyphen, 0 if no more
    If p3 <> 0 Then
        If p2 = 0 Then
            p2 = p3
        ElseIf p3 < p2 Then
            p2 = p3
        End If
    End If
    If p2 = 0 Then
        first_letter = 0
        Exit Function
    End If
    'first move to first non blank, non punctuation after blank
    While is_alpha(Mid$(str, p2)) = False
        p2 = p2 + 1
        If p2 > Len(str) Then 'we ran off the end
            first_letter = 0
            Exit Function
        End If
    Wend
    first_letter = p2
End Function
Public Function is_alpha(ch As String)
'returns true if this is alphabetic character
'false if not
    Dim c As Integer
    c = Asc(ch)
    Select Case c
        Case 65 To 90
            is_alpha = True
        Case 97 To 122
            is_alpha = True
        Case Else
            is_alpha = False
    End Select
    
End Function
Private Function is_roman(str As String, ps As Integer) As Integer
'starts at position ps, until end of word. If it appears to be
'a roman numeral, than the entire word is capped in passed back
'string, else no changes made in string
'returns 1 if changes were made, 0 if no change
Dim mx As Integer, p2 As Integer, flag As Integer, i As Integer
    mx = Len(str) 'just so we don't go off the edge
    p2 = InStr(ps, str, " ") 'see if there is another space after this word
    If p2 = 0 Then
        p2 = mx + 1
    End If
    'scan to see if any inappropriate characters in this word
    flag = 0
    For i = ps To p2 - 1
        If InStr("ivxIVX", Mid$(str, i, 1)) = 0 Then
            flag = 1
        End If
    Next i
    If flag Then
        is_roman = 0
        Exit Function 'this is not roman numeral
    End If
    Mid$(str, ps) = UCase$(Mid$(str, ps, p2 - ps))
    is_roman = 1
End Function
'************** Code End *************

Then call the function like this:
Code:
Private Sub TextBoxName_AfterUpdate()
  Me.TextBoxName = mixed_case(Me.TextBoxName)
End Sub
These functions cover most of the standard name variations, including hyphenated names, names with apostrophes, two 'word' names and so forth.

Please read and abide by the copyright statement at the beginning of the first function.


The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
How about the StrConv function?

Code:
?StrConv("aaa bbb ccc",vbProperCase)
Aaa Bbb Ccc
 
That, of course, works, as long as the names are straightforward. But names that are hyphenated, names than have apostrophes and names such as McDonald won't appear correctly. It really is a complex problem, and the solution from Holovacs, Ashish and Meyer is the most comprehensive I've come across.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Thank you. I tried this but got a compile error: Invalid outside procedure. (str) is highlighted when the error shows.
 
Hey Knownote, I tried your code and it works exactly the way I want it. Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top