make the first letter uppercase (1 Viewer)

DBApprentice

On Error GoTo AWF *****:
Local time
Today, 04:52
Joined
Dec 24, 2018
Messages
150
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!
 

isladogs

MVP / VIP
Local time
Today, 08:52
Joined
Jan 14, 2017
Messages
18,209
Unless you have an issue with multiple spaces, why not just use
?strconv("yOuR stRinG",vbProperCase)

Result: Your String
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:52
Joined
Sep 21, 2011
Messages
14,232
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.
 

Wayne

Crazy Canuck
Local time
Today, 03:52
Joined
Nov 4, 2012
Messages
176
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
 

isladogs

MVP / VIP
Local time
Today, 08:52
Joined
Jan 14, 2017
Messages
18,209
@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:

Wayne

Crazy Canuck
Local time
Today, 03:52
Joined
Nov 4, 2012
Messages
176
Colin,

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

Wayne
 

Oreynolds

Member
Local time
Today, 08:52
Joined
Apr 11, 2020
Messages
157
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:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:52
Joined
Jul 9, 2003
Messages
16,271
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?
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:52
Joined
Sep 21, 2011
Messages
14,232
And you could not possibly tell us what the error is? :(

You have to help us to help you.?
 

Oreynolds

Member
Local time
Today, 08:52
Joined
Apr 11, 2020
Messages
157
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
 

Minty

AWF VIP
Local time
Today, 08:52
Joined
Jul 26, 2013
Messages
10,368
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.
 

Oreynolds

Member
Local time
Today, 08:52
Joined
Apr 11, 2020
Messages
157
Yes you well be right, although I'm still not clear on the field code to call it and get it to work?
 

Minty

AWF VIP
Local time
Today, 08:52
Joined
Jul 26, 2013
Messages
10,368
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
 

Oreynolds

Member
Local time
Today, 08:52
Joined
Apr 11, 2020
Messages
157
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.
 

Minty

AWF VIP
Local time
Today, 08:52
Joined
Jul 26, 2013
Messages
10,368
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.
 

Minty

AWF VIP
Local time
Today, 08:52
Joined
Jul 26, 2013
Messages
10,368
Good to hear - than you for letting us know.
 

Users who are viewing this thread

Top Bottom