make the first letter uppercase

Well again, that is not what you are asking.
You appear to be asking for the first word of each sentence?, but then have diogo as Diogo.?

How is one meant to identify 'special' words?
If every sentence ends in a full stop (as it should?) then you could split using that character, set first character to uppercase and concatenate the split strings back together.?
For that I would write a function.

However I do not have any idea at the moment on how to identify diogo as a word to proper.?

I believe Gemma´s answer will suffice my needs since it will be too much trouble to get the special cases and other stuff.

Thanks anyway!
 
Unless you have an issue with multiple spaces, why not just use
?strconv("yOuR stRinG",vbProperCase)

Result: Your String
 
Well this will do your paragraphs?

Code:
Function ProperSentence(pstrParagraph As String) As String
Dim strSentences() As String, strParagraph As String
Dim iIndex As Integer

strSentences = Split(pstrParagraph, ".")
For iIndex = 0 To UBound(strSentences)
    strSentences(iIndex) = Trim(strSentences(iIndex))
    strSentences(iIndex) = UCase(Left(strSentences(iIndex), 1)) & Mid(strSentences(iIndex), 2)
    strParagraph = strParagraph & strSentences(iIndex) & ". "
    'Debug.Print strSentences(iIndex)
    'Debug.Print strParagraph
Next
ProperSentence = Left(strParagraph, Len(strParagraph) - 1)
End Function

Code:
? propersentence("welcome to the pleasuredome.    this is my test.     hope you like it")
Welcome to the pleasuredome. This is my test. Hope you like it.
 
Here's the simple code I use for proper case:

Code:
Private Sub ClientCityName_AfterUpdate()

    On Error GoTo Err_ClientCityName_AfterUpdate
    'To change the City Name to Proper Case
    If Me.NewRecord Then
        Me.ClientCityName = StrConv(Me.ClientCityName, vbProperCase)
    End If
    
Exit_ClientCityName_AfterUpdate:
    Exit Sub

Err_ClientCityName_AfterUpdate:
    MsgBox Err.Description, vbInformation
    Resume Exit_ClientCityName_AfterUpdate
    
End Sub/CODE]

Has work flawlessly for years. Only hiccup I encountered is if the City name (from my db) has two or more words. It will only capitalize the first letter of the first word. Since I don't encounter that often, I can live with it.

Wayne
 
@Wayne
I've also used this for years and mentioned it in post #22.
However, it capitalises every word e.g.

?strconv("weSton supeR mAre",vbProperCase)
Weston Super Mare
 
Last edited:
Colin,

You're right. That works for all the words in that string.

Wayne
 
Hi, old post I know but like your code as below. I have added it to my DB as public function and called it on a text field as follows:

Code Tags Added by UG
Please use Code Tags when posting VBA Code
https://www.access-programmers.co.u...e-use-code-tags-when-posting-vba-code.240420/
Code:
Private Sub QuoteName_KeyPress(ByVal s As Integer)
    KeyAscii = fnCapitalizeFirstLetter(KeyAscii, ActiveControl.Text)
End Sub

However I must have something wrong as it errors, can you advise?

Thanks


put in a module, doenst only capitalized the first letter, but make the successing letters in small caps, making them proper.
Code:
Public Function fnCapitalizeFirstLetter(ByVal s As String) As String
    Dim lngLen As Long
    Dim l As Long
    Dim bolSkip As Boolean
    Dim sChar As String
    lngLen = Len(Trim(s & ""))
    If lngLen = 0 Then Exit Function
    l = 1
    While l <= lngLen
        sChar = Mid(s, l, 1)
        If sChar = " " Then
            bolSkip = False
        Else
            If isChar(sChar) Then
                If Not bolSkip Then
                    If l = 1 Then
                        s = UCase(sChar) & Mid(s, l + 1)
                    Else
                        s = left(s, l - 1) & UCase(sChar) & Mid(s, l + 1)
                    End If
                    bolSkip = True
                Else
                    If l = 1 Then
                        s = LCase(sChar) & Mid(s, l + 1)
                    Else
                        s = left(s, l - 1) & LCase(sChar) & Mid(s, l + 1)
                    End If
                End If
            End If
        End If
        l = l + 1
    Wend
    fnCapitalizeFirstLetter = s
End Function

Public Function isChar(s As Variant) As Boolean
    Dim intAscii As Integer
    s = left(s & "", 1)
    If Len(s) = 0 Then Exit Function
    intAscii = Asc(s)
    isChar = (intAscii >= 65 And intAscii <= 90)
    If Not (isChar) Then isChar = (intAscii >= 97 And intAscii <= 122)
End Function
 
Last edited by a moderator:
However I must have something wrong as it errors, can you advise?

Well, the first problem is that the function only calls for one parameter and you're passing through two parameters. I don't know what KeyAscii is? Is it a variable? And can you tell me what you are trying to do when you pass through this parameter:- ActiveControl.Text, what is its purpose?
 
And you could not possibly tell us what the error is? :(

You have to help us to help you.?
 
Sorry for the late reply to your responses above. I found the following function code on the site and was hoping to use it on a text field to ensure correct text field formatting. I tried calling the function on a text field on the Key press action using the following which I also found o the same thread but it doesn't seem to work! Any ideas?

TEXT FIELD CODE:

Code:
Private Sub QuoteName_KeyPress(ByVal s As Integer)
    KeyAscii = fnCapitalizeFirstLetter(KeyAscii, ActiveControl.Text)
End Sub

FUNCTION CODE:

Code:
Public Function fnCapitalizeFirstLetter(ByVal s As String) As String
    Dim lngLen As Long
    Dim l As Long
    Dim bolSkip As Boolean
    Dim sChar As String
    lngLen = Len(Trim(s & ""))
    If lngLen = 0 Then Exit Function
    l = 1
    While l <= lngLen
        sChar = Mid(s, l, 1)
        If sChar = " " Then
            bolSkip = False
        Else
            If isChar(sChar) Then
                If Not bolSkip Then
                    If l = 1 Then
                        s = UCase(sChar) & Mid(s, l + 1)
                    Else
                        s = left(s, l - 1) & UCase(sChar) & Mid(s, l + 1)
                    End If
                    bolSkip = True
                Else
                    If l = 1 Then
                        s = LCase(sChar) & Mid(s, l + 1)
                    Else
                        s = left(s, l - 1) & LCase(sChar) & Mid(s, l + 1)
                    End If
                End If
            End If
        End If
        l = l + 1
    Wend
    fnCapitalizeFirstLetter = s
End Function

Public Function isChar(s As Variant) As Boolean
    Dim intAscii As Integer
    s = left(s & "", 1)
    If Len(s) = 0 Then Exit Function
    intAscii = Asc(s)
    isChar = (intAscii >= 65 And intAscii <= 90)
    If Not (isChar) Then isChar = (intAscii >= 97 And intAscii <= 122)
End Function
 
Why are calling it on the keypress event? Surely after update event would make more sense?

And What "Doesn't work"?

An error message or code where it goes wrong would be really helpful, as already asked.
 
Yes you well be right, although I'm still not clear on the field code to call it and get it to work?
 
Let's get back to basics. Some assumptions: You have a control on a form we're going to call it txtMyTextBox. And we want to capitalise the first word in every sentence?
If the above is roughly true then in the After update event of the control you want something like this;

Code:
Private Sub txtMyTextBox_After_Update()

     Me.txtMyTextBox =  fnCapitalizeFirstLetter(Me.txtMyTextBox)

End Sub

I've assumed I have got the purpose of the function correctly, I haven't examined the code for it
 
Let's get back to basics. Some assumptions: You have a control on a form we're going to call it txtMyTextBox. And we want to capitalise the first word in every sentence?
If the above is roughly true then in the After update event of the control you want something like this;

Code:
Private Sub txtMyTextBox_After_Update()

     Me.txtMyTextBox =  fnCapitalizeFirstLetter(Me.txtMyTextBox)

End Sub

I've assumed I have got the purpose of the function correctly, I haven't examined the code for it

Thanks Minty, I'll have a go with this later, appreciate your help.
 
Good luck.
If you get an error - screen-shot the error and the highlighted line of code, and paste the whole of the code that has the error.
 
Good to hear - than you for letting us know.
 
@arnelgp how do you use it ? Sorry you know I am learning.. I have tried fnCapitalizeFirstLetter (PLastNameTxt) and it doesn't work..
Hm. What does "... it doesn't work" mean? Wrong results, errors, nothing happens?

Often, we get a lot of useful information from details about a) what, specifically you tried, and b) specifically, what the result was.
 
Hm. What does "... it doesn't work" mean? Wrong results, errors, nothing happens?

Often, we get a lot of useful information from details about a) what, specifically you tried, and b) specifically, what the result was.
Sorry, I made a mistake, should have tested fully before making questions,
I was simply doing the conversion to uppercase but never apply the conversion back

I did " fnCapitalizeFirstLetter (PLastNameTxt) "
instead of " PlastName.txt = fnCapitalizeFirstLetter (PLastNameTxt) "

makes a world of difference, but I am in a learning curve, sometimes I am able to catch my own mistakes and correct them.

Thanks George.
 

Users who are viewing this thread

Back
Top Bottom