Programming code for Title Case

  • Thread starter Thread starter Darts via AccessMonster.com
  • Start date Start date
D

Darts via AccessMonster.com

What is the programming code for Title Case.

I want to be abel to type john smith without using shift key.

thanks much
 
You can do that, but what happens to...

John McDaniel

or

Luis de los Sotos

or

Rick Jones III (You want Rick Jones Iii ?)





It is generally a bad idea to try to code this.
 
What would the code be? My curiosity is killing me!

I guess there is no way around that. What you entered below is a very valid
point!!!
 
This is what I use: vText (which is the text box value) is passed to this
function from all the textbox BeforeUpdate events. As I had first names and
last name in different textboxes I only needed to check/change the first
character. Beware of null and empty strings.

Private Function CheckFirstCap(vText As Variant) As String
On Error GoTo HandleError
Dim iMax As Integer
Dim sFirstChar As String
If IsNull(vText) Then
CheckFirstCap = ""
Exit Function
End If
iMax = Len(vText)
If iMax = 0 Then
CheckFirstCap = vText
Exit Function
End If
sFirstChar = Left(vText, 1)
CheckFirstCap = Format(sFirstChar, ">") & Right(vText, iMax - 1)
Exit Function
HandleError:
MsgBox Err.Number & " - " & Err.Description, vbExclamation + vbOKOnly
CheckFirstCap = Nz(vText, "")
End Function
 
Ok this is verrry new to me so in that field I would type
Ucast("John Smith")

Be patient i am new at this

thanks
 
Hi,
I don't remember where I first saw this but it works well.

Public Function LowerToProperCase(ByVal edit$) As String
' The LowerToProperCase function looks through the string
' seeing which characters are letters and which are not.
' It capitalizes those that satisfy the rules shown in the
' comment at the beginning of the function.
' Subroutine to normalize 1 piece of data to Capitalize.
' Inputs: char label.
' Outputs: Normalized Label.
' Does more than I need here, will leave it as is.
' Rules Cap char after ['][>][-][.][/][#][0-9][&][Mc][Mac][,][(],[\],[|]
' Cap char after [!][@][$][%][^][*][+][=]["]['(not s)]
' abbreviations [po][nw][ne][sw][se][ii] are capitalized
' abbreviations [st][th][nd][rd][c/o] are not capitalized
' words [of][and][van][der][de] are not capitalized.
On Error Resume Next
Dim A$, b$, c$, i%

If Len(Trim(edit$)) = 0 Then
LowerToProperCase = edit$
Exit Function
End If

edit$ = edit$ + Space$(3) ' End of word match ???
Mid$(edit$, 1, 1) = UCase$(Mid$(edit$, 1, 1)) ' Cap first char.
If Mid$(edit$, 1, 3) = "Po " Then
Mid$(edit$, 1, 3) = "PO " ' PO Box cap.
End If
b$ = "NwNeSwSeIi" ' NW NE SW SE II.
If Mid$(edit$, 1, 2) = "Mc" Then
Mid$(edit$, 3, 1) = UCase$(Mid$(edit$, 3, 1))
End If
If Mid$(edit$, 1, 3) = "Mac" Then
Mid$(edit$, 4, 1) = UCase$(Mid$(edit$, 3, 1))
End If
If Mid$(edit$, 1, 1) > Chr$(47) And Mid$(edit$, 1, 1) < Chr$(58) Then
Mid$(edit$, 2, 1) = UCase$(Mid$(edit$, 2, 1)) ' Nos 0 - 9.
End If
c$ = " >-./#&,(!@$%^*+=\|" & Chr(34)
If InStr(c$, Mid$(edit$, 1, 1)) > 0 Then
Mid$(edit$, 2, 1) = UCase$(Mid$(edit$, 2, 1))
End If
For i% = 2 To Len(edit$)
If InStr(b$, Mid$(edit$, i%, 2)) > 0 Then
If Mid$(edit$, i% - 1, 1) = " " And Mid$(edit$, i% + 2, 1) = " "
Then
Mid$(edit$, i%, 2) = UCase$(Mid$(edit$, i%, 2))
End If
i% = i% + 2
End If
If Mid$(edit$, i%, 4) = " of " Then ' Don't cap word "of".
i% = i% + 2
GoTo skipcapchar
ElseIf Mid$(edit$, i%, 5) = " and " Then ' Don't cap word "and".
i% = i% + 3
GoTo skipcapchar

' Uncomment any of these lines if you want to keep parts of a name in lower
case eg da Vinci instead of Da Vinci.
' ElseIf Mid$(edit$, i%, 5) = " der " Then ' Don't cap word "der".
' i% = i% + 3
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 5) = " van " Then ' Don't cap word "van".
' i% = i% + 3
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " da " Then ' Don't cap word "de".
' i% = i% + 2
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " de " Then ' Don't cap word "de".
' i% = i% + 2
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " di " Then ' Don't cap word "da".
' i% = i% + 2
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " di " Then ' Don't cap word "di".
' i% = i% + 2
' GoTo skipcapchar
ElseIf Mid$(edit$, i%, 5) = " c/o " Then ' Don't cap abbrev c/o.
i% = i% + 3
GoTo skipcapchar
End If
If InStr(c$, Mid$(edit$, i%, 1)) > 0 And i% < Len(edit$) Then
Mid$(edit$, i% + 1, 1) = UCase(Mid$(edit$, i% + 1, 1))
End If
If Mid$(edit$, i%, 1) = Chr$(39) And Mid$(edit$, i% + 1, 1) <> "s"
Then
Mid$(edit$, i% + 1, 1) = UCase$(Mid$(edit$, i% + 1, 1)) ' Catch
"'s".
End If
If Mid$(edit$, i%, 2) = "Mc" Then
Mid$(edit$, i% + 2, 1) = UCase$(Mid$(edit$, i% + 2, 1))
End If
If Mid$(edit$, i%, 3) = "Mac" Then
Mid$(edit$, i% + 3, 1) = UCase$(Mid$(edit$, i% + 3, 1))
End If
If InStr("0123456789", Mid$(edit$, i%, 1)) > 0 Then ' Nos 0-9.
A$ = Mid$(edit$, i% + 1, 2)
If A$ <> "st" And A$ <> "th" And A$ <> "nd" And A$ <> "rd" Then
Mid$(edit$, i% + 1, 1) = UCase$(Mid$(edit$, i% + 1, 1)) '
No's 0-9.
End If
End If
skipcapchar:
Next i%
edit$ = RTrim$(edit$) ' Trim space added earlier.

LowerToProperCase = edit$
End Function

Use example:
strName = LowerToProperCase(strName)

Have fun.

Regards
Paul
 
Very comprehensive! Thanks. Clare

Paul said:
Hi,
I don't remember where I first saw this but it works well.

Public Function LowerToProperCase(ByVal edit$) As String
' The LowerToProperCase function looks through the string
' seeing which characters are letters and which are not.
' It capitalizes those that satisfy the rules shown in the
' comment at the beginning of the function.
' Subroutine to normalize 1 piece of data to Capitalize.
' Inputs: char label.
' Outputs: Normalized Label.
' Does more than I need here, will leave it as is.
' Rules Cap char after ['][>][-][.][/][#][0-9][&][Mc][Mac][,][(],[\],[|]
' Cap char after [!][@][$][%][^][*][+][=]["]['(not s)]
' abbreviations [po][nw][ne][sw][se][ii] are capitalized
' abbreviations [st][th][nd][rd][c/o] are not capitalized
' words [of][and][van][der][de] are not capitalized.
On Error Resume Next
Dim A$, b$, c$, i%

If Len(Trim(edit$)) = 0 Then
LowerToProperCase = edit$
Exit Function
End If

edit$ = edit$ + Space$(3) ' End of word match ???
Mid$(edit$, 1, 1) = UCase$(Mid$(edit$, 1, 1)) ' Cap first char.
If Mid$(edit$, 1, 3) = "Po " Then
Mid$(edit$, 1, 3) = "PO " ' PO Box cap.
End If
b$ = "NwNeSwSeIi" ' NW NE SW SE II.
If Mid$(edit$, 1, 2) = "Mc" Then
Mid$(edit$, 3, 1) = UCase$(Mid$(edit$, 3, 1))
End If
If Mid$(edit$, 1, 3) = "Mac" Then
Mid$(edit$, 4, 1) = UCase$(Mid$(edit$, 3, 1))
End If
If Mid$(edit$, 1, 1) > Chr$(47) And Mid$(edit$, 1, 1) < Chr$(58) Then
Mid$(edit$, 2, 1) = UCase$(Mid$(edit$, 2, 1)) ' Nos 0 - 9.
End If
c$ = " >-./#&,(!@$%^*+=\|" & Chr(34)
If InStr(c$, Mid$(edit$, 1, 1)) > 0 Then
Mid$(edit$, 2, 1) = UCase$(Mid$(edit$, 2, 1))
End If
For i% = 2 To Len(edit$)
If InStr(b$, Mid$(edit$, i%, 2)) > 0 Then
If Mid$(edit$, i% - 1, 1) = " " And Mid$(edit$, i% + 2, 1) = " "
Then
Mid$(edit$, i%, 2) = UCase$(Mid$(edit$, i%, 2))
End If
i% = i% + 2
End If
If Mid$(edit$, i%, 4) = " of " Then ' Don't cap word "of".
i% = i% + 2
GoTo skipcapchar
ElseIf Mid$(edit$, i%, 5) = " and " Then ' Don't cap word "and".
i% = i% + 3
GoTo skipcapchar

' Uncomment any of these lines if you want to keep parts of a name in lower
case eg da Vinci instead of Da Vinci.
' ElseIf Mid$(edit$, i%, 5) = " der " Then ' Don't cap word "der".
' i% = i% + 3
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 5) = " van " Then ' Don't cap word "van".
' i% = i% + 3
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " da " Then ' Don't cap word "de".
' i% = i% + 2
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " de " Then ' Don't cap word "de".
' i% = i% + 2
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " di " Then ' Don't cap word "da".
' i% = i% + 2
' GoTo skipcapchar
' ElseIf Mid$(edit$, i%, 4) = " di " Then ' Don't cap word "di".
' i% = i% + 2
' GoTo skipcapchar
ElseIf Mid$(edit$, i%, 5) = " c/o " Then ' Don't cap abbrev c/o.
i% = i% + 3
GoTo skipcapchar
End If
If InStr(c$, Mid$(edit$, i%, 1)) > 0 And i% < Len(edit$) Then
Mid$(edit$, i% + 1, 1) = UCase(Mid$(edit$, i% + 1, 1))
End If
If Mid$(edit$, i%, 1) = Chr$(39) And Mid$(edit$, i% + 1, 1) <> "s"
Then
Mid$(edit$, i% + 1, 1) = UCase$(Mid$(edit$, i% + 1, 1)) ' Catch
"'s".
End If
If Mid$(edit$, i%, 2) = "Mc" Then
Mid$(edit$, i% + 2, 1) = UCase$(Mid$(edit$, i% + 2, 1))
End If
If Mid$(edit$, i%, 3) = "Mac" Then
Mid$(edit$, i% + 3, 1) = UCase$(Mid$(edit$, i% + 3, 1))
End If
If InStr("0123456789", Mid$(edit$, i%, 1)) > 0 Then ' Nos 0-9.
A$ = Mid$(edit$, i% + 1, 2)
If A$ <> "st" And A$ <> "th" And A$ <> "nd" And A$ <> "rd" Then
Mid$(edit$, i% + 1, 1) = UCase$(Mid$(edit$, i% + 1, 1)) '
No's 0-9.
End If
End If
skipcapchar:
Next i%
edit$ = RTrim$(edit$) ' Trim space added earlier.

LowerToProperCase = edit$
End Function

Use example:
strName = LowerToProperCase(strName)

Have fun.

Regards
Paul


Darts via AccessMonster.com said:
What is the programming code for Title Case.

I want to be abel to type john smith without using shift key.

thanks much
 
Back
Top