sentence case with exceptions

bbwolff

Registered User.
Local time
Today, 19:07
Joined
Oct 1, 2013
Messages
116
I'm trying to convert text to sentence case, ie capitalized first letter of the text box. The input will always be just one sentence, so I just converted everything to lower case and capitalized the first letter.

The problem begins here, I want some special combinations be left as upper case, abbrevations and such things. I found a code on the net, but it's not really working. Anyone sees anything wrong here or has some other solution?

there's table tblExceptions with coloumn Word where the exceptions are held.

Code:
Public Function LeaveCaps(strInputText) As String
On Error GoTo ErrorHandler
 
   Dim strException As String
   Dim strExceptionPlus As String
   Dim strModifiedText As String
   Dim strTestString As String
   Dim strAppendChar As String
   Dim i As Integer
   Dim strTestChars As String
  Dim a, b, c As String
  
  
   Set dbs = CurrentDb
   Set rst = dbs.OpenRecordset("tblExceptions")
  
  
   'Do the initial conversion to mixed case (initial letter capitalized)
   strModifiedText = UCase(Left(strInputText, 1)) & LCase(Right(strInputText, Len(strInputText) - 1))
   
    

   'Set up string of punctuation marks to append to words being checked
    strTestChars = " ,;.!?:-_()'" & Chr$(34)
  
   'Restore the original capitalization of all words in table
   Do While Not rst.EOF
      strException = rst![Word]
        
      'Search for word followed by each of the specified characters
      For i = 1 To Len(strTestChars)
         strAppendChar = Mid(strTestChars, i, 1)
         strExceptionPlus = strException & strAppendChar
         Debug.Print "Exception word: " & strExceptionPlus
         strModifiedText = Replace(expression:=strModifiedText, _
            Find:=strExceptionPlus, Replace:=strExceptionPlus)
      Next i
      rst.MoveNext
   Loop
  
   LeaveCaps = strModifiedText
 
ErrorHandlerExit:
   Exit Function
 
ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit
 
End Function

tx

sample db
 
, I want some special combinations be left as upper case, abbrevations and such things. I found a code on the net, but it's not really working.

This is not an adequate description of the problem. What exactly is "not working"?

Post the link to where you got the code.
 
http://office-watch.com/t/pda.aspx?a=1176

it should leave L1 as L1 but it changes it to l1
basically it just does this
Code:
 UCase(Left(strInputText, 1)) & LCase(Right(strInputText, Len(strInputText) - 1))
 
i think i can do it easier with replace in a loop
is replace, and tables, case sensitive?
 
yeah, works just fine like this

Code:
Public Function LeaveCaps(strInputText) As String
On Error GoTo ErrorHandler
 

   Dim strModifiedText  
  
   Set dbs = CurrentDb
   Set rst = dbs.OpenRecordset("tblExceptions")
  
  
   'Do the initial conversion to mixed case (initial letter capitalized)
   strModifiedText = UCase(Left(strInputText, 1)) & LCase(Right(strInputText, Len(strInputText) - 1))
   
    MsgBox "" & strModifiedText
  
   'Restore the original capitalization of all words in table
   Do While Not rst.EOF
      strModifiedText = Replace(strModifiedText, rst![Find], rst![Replace])
      rst.MoveNext
   Loop
  
   LeaveCaps = strModifiedText
 
ErrorHandlerExit:
   Exit Function
 
ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit
 
End Function
 

Users who are viewing this thread

Back
Top Bottom