capitalize first letter of sentence.

foxtet

Registered User.
Local time
Today, 20:38
Joined
May 21, 2011
Messages
129
Dear Guys
I am using a form to enter data in which I want to capitalize the first letter of the sentence.
I use the following code after update event of the text box

Me!DemReason = StrConv(Me!DemReason, vbProperCase)

This gives first letter of each word in upper case.

Any suggestions...?

foxtet
 
Code:
Public Function fnCapitalizeInSentence(ByVal strToConvert As String) As String
    
    Dim varString() As String
    Dim varElement As Variant
    Dim strChar As String
    Dim intLength As Integer
    Dim i As Integer
    Dim k As Integer
    Dim strNew As String
    
    varString = Split(strToConvert, ".")
    
    ' upper case first letter of sentence
    For Each varElement In varString
        intLength = Len(varElement)
        i = 1
        Do While i <= intLength
        
            strChar = Mid(varElement, i, 1)
            If strChar <> " " Then
                If i = 1 Then
                    varElement = UCase(strChar) & Mid(varElement, i + 1)
                Else
                    varElement = Left(varElement, i - 1) & UCase(strChar) & Mid(varElement, i + 1)
                End If
                varString(k) = varElement
                Exit Do
                
            End If
            i = i + 1
        Loop
        k = k + 1
    Next
    
    ' assemble the string again
    For Each varElement In varString
        strNew = strNew & varElement & "."
    Next
    If InStr(strNew, "..") = Len(strNew) - 1 Then strNew = Left(strNew, Len(strNew) - 1)
    
    fnCapitalizeInSentence = strNew
End Function
 
Last edited:
Code:
Public Function fnCapitalizeInSentence(ByVal strToConvert As String) As String
    
    Dim varString() As String
    Dim varElement As Variant
    Dim strChar As String
    Dim intLength As Integer
    Dim i As Integer
    Dim k As Integer
    Dim strNew As String
    
    varString = Split(strToConvert, ".")
    
    ' upper case first letter of sentence
    For Each varElement In varString
        intLength = Len(varElement)
        i = 1
        Do While i <= intLength
        
            strChar = Mid(varElement, i, 1)
            If strChar <> " " Then
                If i = 1 Then
                    varElement = UCase(strChar) & Mid(varElement, i + 1)
                Else
                    varElement = Left(varElement, i - 1) & UCase(strChar) & Mid(varElement, i + 1)
                End If
                varString(k) = varElement
                Exit Do
                
            End If
            i = i + 1
        Loop
        k = k + 1
    Next
    
    ' assemble the string again
    For Each varElement In varString
        strNew = strNew & varElement & "."
    Next
    If InStr(strNew, "..") = Len(strNew) - 1 Then strNew = Left(strNew, Len(strNew) - 1)
    
    fnCapitalizeInSentence = strNew
End Function

How to apply this function in a text box demReason.
 
use the code on your fist post:

Me!DemReason = fnCapitalizeInSentence(Me!DemReason & "")
 
Hi. Taking advantage of other questions asked here, but I don't seem to have this working properly :-(

I've copied and pasted the code and followed the instructions, but is seems the corrections are automatically carried out by Access? Having said that, the very first letter of the sentence doesn't capitalize, either. Only the first letter after each '.'

Has anyone had the same issue, please?

As an aside, is the Me! bit necessary?

Thanks
 
Last edited:
can you show how do you call the function.
 
Oops. Typo in calling the function!

Works perfectly now. Sorry

Pete
 
Arnelgp, you never cease to amaze me. This bit of code is going into my ever-growing library...
 
thanks mr.nauticalGent!
 
Hi, just playing with this code. I want the correctly formatted text to be saved in my table. How can I do this and will that then enable the same text format in other forms and reports.

Thanks.
 

Users who are viewing this thread

Back
Top Bottom