Code:
'---------------------------------------------------------------------------------------
' Procedure : fProperCase
' Author : orange
' Date : 5/22/2008
' Purpose : To put names into a "proper case" format. This deals with
' the Mc Mac O'Reilly issues.
'
' --Note: Code obtained from http://www.access-programmers.co.uk/forums
' -- It was written by David McAFee
''If any improvements are made to this code, please
'email David McAfee at mcafee_audio@msn.com
'---------------------------------------------------------------------------------------
'
Function fProperCase(AnyText As String) As String
'Convert passed text to all lowercase. Use ProperCase()
'as you would a built-in Access function.
'If any improvement are made to this code, please
'email David McAfee at mcafee_audio@msn.com
On Error GoTo fProperCase_Error
If Nz(AnyText, "") = "" Then Exit Function 'If passed value is a null, ignore all the stuff below.
Dim intCounter As Integer
Dim OneChar As String
Dim StartingNumber As Integer
StartingNumber = 1
If Right(Left(AnyText, 4), 1) = " " Then 'Check for 3 letter words
' If MsgBox("In the example '" & AnyText & "', shall I capitalize the three letter word '" & Mid$(AnyText, intCounter + 1, 3) & "' to '" & UCase(Mid$(AnyText, intCounter + 1, 3)) & "' ?", vbYesNo, "Three letter word was found") = vbYes Then
'Yes was selected, so Capitalize the 3 char's
' AnyText = UCase(Left$(AnyText, 3)) & LCase$(Mid$(AnyText, 4, 255))
' StartingNumber = 4
' Else
' 'No was selected, so only capitalize the first of the 3 char's
AnyText = UCase$(Left$(AnyText, 1)) & LCase$(Mid$(AnyText, 2, 255))
StartingNumber = 2
' End If
ElseIf Right(Left(AnyText, 3), 1) = " " Then
' If MsgBox("In the example '" & AnyText & "', shall I capitalize the two letter word '" & Mid$(AnyText, intCounter + 1, 2) & "' to '" & UCase(Mid$(AnyText, intCounter + 1, 2)) & "' ?", vbYesNo, "Two letter word was found") = vbYes Then
'Yes was selected, so Capitalize the 2 char's
' AnyText = UCase(Left$(AnyText, 2)) & LCase$(Mid$(AnyText, 3, 255))
' StartingNumber = 3
' Else
' 'No was selected, so only capitalize the first of the 2 char's
AnyText = UCase(Left$(AnyText, 1)) & LCase$(Mid$(AnyText, 2, 255))
StartingNumber = 2
' End If
Else
'First convert to initial cap, followed by all lowercase.
AnyText = UCase$(Left$(AnyText, 1)) & LCase$(Mid$(AnyText, 2, 255))
StartingNumber = 2
End If
'Look at each character, starting at the second character.
For intCounter = StartingNumber To Len(AnyText)
OneChar = Mid$(AnyText, intCounter, 1)
Select Case OneChar
'...convert the character after dash/hyphen/slash/period/ampersand to uppercase.
' Such as A.B.C. Industries, B&B Mfg
Case "-", "/", ".", "&", "("
AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
Case "'" 'Check the character two places after the apostrophe.
If Mid$(AnyText, intCounter + 2, 1) <> " " Then 'If it is not a space, then capatilize (O'Conner, O'Niel)
AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
Else
'Do nothing (Don't , Tom's, haven't)
End If
Case "c" ' Take care of the McAfee's, McDonalds & McLaughlins and such
If (Mid$(AnyText, intCounter - 1, 1) = "M") Then 'Check if Prev Char is an M
If ((intCounter - 2) < 1) Then 'Check to see if the M was the first character
AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
ElseIf (Mid$(AnyText, intCounter - 2, 1) = " ") Then 'If M wasn't first character, then check to see if a space was before the M
AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
End If
End If
Case " "
Select Case Mid$(AnyText, intCounter + 1, 2)
Case "de" 'Add any other exceptions here Example: Oscar de La Hoya
AnyText = Left$(AnyText, intCounter) & LCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
Case Else ' Example: A B C Manufacturing
AnyText = Left$(AnyText, intCounter) & UCase$(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
End Select
'If Mid$(AnyText, intCounter + 4, 1) = " " Or ((intCounter + 3) = Len(AnyText)) Then 'Check for 3 letter words
' If MsgBox("In the example '" & AnyText & "', shall I capitalize the three letter word '" & Mid$(AnyText, intCounter + 1, 3) & "' to '" & UCase(Mid$(AnyText, intCounter + 1, 3)) & "' ?", vbYesNo, "Three letter word was found") = vbYes Then
'Yes was selected, so Capitalize the 3 char's
' AnyText = Left$(AnyText, intCounter) & UCase(Mid$(AnyText, intCounter + 1, 3)) & Mid$(AnyText, intCounter + 4, 255)
' intCounter = intCounter + 3
' Else
' 'No was selected, so only capitalize the first of the 3 char's
' AnyText = Left$(AnyText, intCounter) & UCase(Mid$(AnyText, intCounter + 1, 1)) & Mid$(AnyText, intCounter + 2, 255)
' End If
'End If
End Select
Next
'All done, return current contents of AnyText variable.
fProperCase = AnyText
On Error GoTo 0
Exit Function
fProperCase_Error:
MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure fProperCase of Module fProperCaseStuff"
End Function
Here is a test routine. You can add your own test data and adjust as needed.