Code:
Function ProperAll(ByVal strIn As String, Optional ByVal SkipExceptions As Boolean = True) As String
'Returns the strIn with all words in proper case
'exept those that are in table tblCaseExceptions
'if SkipExceptions is TRUE.
Dim rsExp As dao.Recordset
Dim i As Integer
Dim intPos As Integer
Dim strExp As String
Dim strTemp As String
Dim strW As String
Dim varW As Variant
On Error GoTo ErrHandler
strTemp = strIn
If SkipExceptions Then
'Get the exceptions.
Set rsExp = CurrentDb.OpenRecordset("SELECT CaseException FROM tblCaseExceptions", dbOpenForwardOnly)
While Not rsExp.EOF
strExp = rsExp(0)
'Search for exception in temporary text.
If InStr(1, strTemp, strExp, vbBinaryCompare) > 0 Then
'Remove the exception from temp text.
strTemp = Replace(strTemp, strExp, " ")
End If
rsExp.MoveNext
Wend
End If
'Remove extra spaces.
While InStr(1, strTemp, " ") > 0
strTemp = Replace(strTemp, " ", " ")
Wend
'Get the remaining words.
varW = Split(Trim(strTemp))
'Convert the input text using only the remaining words.
For i = LBound(varW) To UBound(varW)
strW = varW(i)
'Find the current word in text.
intPos = InStr(intPos + 1, strIn, strW)
'Convert it to proper case.
Mid(strIn, intPos, Len(strW)) = StrConv(strW, vbProperCase)
'Set the position in text to the end of this word.
intPos = intPos + Len(strW)
Next i
ExitHere:
'Return.
ProperAll = strIn
On Error Resume Next
rsExp.Close
Set rsExp = Nothing
On Error GoTo 0
Exit Function
ErrHandler:
Resume ExitHere
End Function