Replace 3rd character in string with uppercase (1 Viewer)

Too$hort

New member
Local time
Yesterday, 22:26
Joined
Jan 25, 2012
Messages
2
Hi Everyone,

I'm a first time poster but long time lurker. This is a great site!

I am an novice access user and I am having trouble with trying to set up a query where if a last name starts with "Mc".... it will capitalize the next character. For example: Mcdonald would change to McDonald. Mccleary would change to McCleary.. etc.

So far I have:

IIf(Left([Clean5],2)="Mc",Replace([Clean5],Mid([Clean5],3,1),StrConv(Mid([Clean5],3,1),3)),[Clean5])

Clean5 is the field where I have my names stored.
This sort of works but it replaces all the same letters as the 3rd character to uppercase too. For example: Mcdonald changes to McDonalD, and Mccleary changes to MCCleary.

Can anyone help with a better solution?

Thanks in advance!
 

vbaInet

AWF VIP
Local time
Today, 06:26
Joined
Jan 22, 2010
Messages
26,374
Welcome to the forum (as a poster) :)

This question has been asked a few times and the problem lies in getting all the possible variations right. Think about names like O'Reilly, Mac Donald, Mc-Fisher ... etc. I'm sure there are many others but I can't list them all. I guess you could improve the list as you go along.

Here's a function that might help:

http://www.tek-tips.com/faqs.cfm?fid=6324
 

Too$hort

New member
Local time
Yesterday, 22:26
Joined
Jan 25, 2012
Messages
2
Thanks so much! I'm going to try this out, but it looks like it should solve my problem!
 

vbaInet

AWF VIP
Local time
Today, 06:26
Joined
Jan 22, 2010
Messages
26,374
It looks pretty comprehensive so it should catch most situations.
 

ericwarren98

New member
Local time
Yesterday, 23:26
Joined
Jul 27, 2017
Messages
1
The Function is helpful. I'm pasting below an improvement on the original question that solves the problem with unintentional additional portions of the name being capitalized, in this, just targeting last names that start with "Mc" or "O' " as that is all we have in the population:

Expr1: IIf(Left([LAST_NAME_agr],2) In ("Mc","O'"),Left([LAST_NAME_agr],2) & UCase(Mid([LAST_NAME_agr],3,1)) & Mid([LAST_NAME_agr],4),[LAST_NAME_agr])
 

MarkK

bit cruncher
Local time
Yesterday, 22:26
Joined
Mar 17, 2004
Messages
8,180
Another little known attribute of the Mid() function is that it can serve as the target of an assignment, so consider the following code...
Code:
Private Sub Test1923864791437104()
    Dim tmp As String
    
    tmp = "Mcdonald"
    [COLOR="Blue"]Mid(tmp, 3)[/COLOR] = UCase(Mid(tmp, 3, 1)) [COLOR="Green"]'UCase() the 3rd character in the string[/COLOR]
    Debug.Print tmp
End Sub
See how using Mid() we can expose a substring to reassignment? This, of course, won't work in an immediate if, but it is worthy of note in the context of this thread.
Mark
 

ypma

Registered User.
Local time
Today, 06:26
Joined
Apr 13, 2012
Messages
643
Too$hort: I found the following code written by John Viescas which work on access 2003 . It requires a function and an after update event code to call the Function. I will attach a demo. If you cannot access the demo I will post the script and call code. Hope this is of use to you.

Regards Ypma
 

Attachments

  • ClientsDetails.accdb
    560 KB · Views: 101

Gasman

Enthusiastic Amateur
Local time
Today, 06:26
Joined
Sep 21, 2011
Messages
14,238
I was given this code a good while back from this site for one of my databases.
Sorry I have forgotten who it was, but I do remember he helped me with the NotInList event for my data entry.

It certainly worked for my purposes.

HTH

Code:
'************** Code Start *************
'This code was originally written by Jay Holovacs.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Jay Holovacs
'
Public Function mixed_case(str As Variant) As String
'returns modified string, first character of each word us uppercase
'all others lower case
Dim ts As String, ps As Integer, char2 As String
    If IsNull(str) Then
        mixed_case = ""
        Exit Function
    End If
    str = Trim(str) 'added 11/22/98
    If Len(str) = 0 Then
        mixed_case = ""
        Exit Function
    End If
    ts = LCase$(str)
    ps = 1
    ps = first_letter(ts, ps)
    Special_Name ts, 1 'try to fix the beginning
    Mid$(ts, 1) = UCase$(Left$(ts, 1))
    If ps = 0 Then
        mixed_case = ts
        Exit Function
    End If
    While ps <> 0
        If is_roman(ts, ps) = 0 Then 'not roman, apply the other rules
            Special_Name ts, ps
            Mid$(ts, ps) = UCase$(Mid$(ts, ps, 1)) 'capitalize the first letter
        End If
        ps = first_letter(ts, ps)
    Wend
    mixed_case = ts
End Function
Private Sub Special_Name(str As String, ps As Integer)
'expects str to be a lower case string, ps to be the
'start of name to check, returns str modified in place
'modifies the internal character (not the initial)

Dim char2 As String
char2 = Mid$(str, ps, 2) 'check for Scots Mc
If (char2 = "mc") And Len(str) > ps + 1 Then '3rd char is CAP
    Mid$(str, ps + 2) = UCase$(Mid$(str, ps + 2, 1))
End If

char2 = Mid$(str, ps, 2) 'check for ff
If (char2 = "ff") And Len(str) > ps + 1 Then 'ff form
    Mid$(str, ps, 2) = LCase$(Mid$(str, ps, 2))
End If

char2 = Mid$(str, ps + 1, 1) 'check for apostrophe as 2nd char
If (char2 = "'") Then '3rd char is CAP
    Mid$(str, ps + 2) = UCase$(Mid$(str, ps + 2, 1))
End If

Dim char3 As String
char3 = Mid$(str, ps, 3) 'check for scots Mac
If (char3 = "mac") And Len(str) > ps + 1 Then 'Mac form
    Mid$(str, ps + 3) = UCase$(Mid$(str, ps + 3, 1))
End If

Dim char4 As String
char4 = Mid$(str, ps, 4) 'check for Fitz
If (char4 = "fitz") And Len(str) > ps + 1 Then 'Fitz form
    Mid$(str, ps + 4) = UCase$(Mid$(str, ps + 4, 1))
End If

End Sub
Private Function first_letter(str As String, ps As Integer) As Integer
'ps=starting point to search (starts with character AFTER ps)
'returns next first letter, 0 if no more left
'modified 6/18/99 to handle hyphenated names
Dim p2 As Integer, p3 As Integer, s2 As String
    s2 = str
    p2 = InStr(ps, str, " ") 'points to next blank, 0 if no more
    p3 = InStr(ps, str, "-") 'points to next hyphen, 0 if no more
    If p3 <> 0 Then
        If p2 = 0 Then
            p2 = p3
        ElseIf p3 < p2 Then
            p2 = p3
        End If
    End If
    If p2 = 0 Then
        first_letter = 0
        Exit Function
    End If
    'first move to first non blank, non punctuation after blank
    While is_alpha(Mid$(str, p2)) = False
        p2 = p2 + 1
        If p2 > Len(str) Then 'we ran off the end
            first_letter = 0
            Exit Function
        End If
    Wend
    first_letter = p2
End Function
Public Function is_alpha(ch As String)
'returns true if this is alphabetic character
'false if not
    Dim c As Integer
    c = Asc(ch)
    Select Case c
        Case 65 To 90
            is_alpha = True
        Case 97 To 122
            is_alpha = True
        Case Else
            is_alpha = False
    End Select
    
End Function
Private Function is_roman(str As String, ps As Integer) As Integer
'starts at position ps, until end of word. If it appears to be
'a roman numeral, than the entire word is capped in passed back
'string, else no changes made in string
'returns 1 if changes were made, 0 if no change
Dim mx As Integer, p2 As Integer, flag As Integer, i As Integer
    mx = Len(str) 'just so we don't go off the edge
    p2 = InStr(ps, str, " ") 'see if there is another space after this word
    If p2 = 0 Then
        p2 = mx + 1
    End If
    'scan to see if any inappropriate characters in this word
    flag = 0
    For i = ps To p2 - 1
        If InStr("ivxIVX", Mid$(str, i, 1)) = 0 Then
            flag = 1
        End If
    Next i
    If flag Then
        is_roman = 0
        Exit Function 'this is not roman numeral
    End If
    Mid$(str, ps) = UCase$(Mid$(str, ps, p2 - ps))
    is_roman = 1
End Function
'************** Code End  *************
 

isladogs

MVP / VIP
Local time
Today, 06:26
Joined
Jan 14, 2017
Messages
18,209
Hi Gasman

Thanks for these functions.

There is a simpler way of converting a string to proper case - use the StrConv function

e.g. StrConv("access world forums", vbProperCase) gives Access World Forums

The full list of arguments for StrConv is:

Code:
Parameter	   Value    Description
vbUpperCase	     1	     Converts the string to all uppercase.
vbLowerCase	     2	     Converts the string to all lowercase.
vbProperCase	     3	     Converts the first letter to every word to uppercase. All other characters are left as lowercase. 
vbUnicode	     64	     Converts the string to Unicode.
vbFromUnicode	     128     Converts the string from Unicode to the default code page of the system.

The first two are of course equivalent to UCase & LCase
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 06:26
Joined
Sep 21, 2011
Messages
14,238
Hi Colin,

It does a little more than proper case a sentence.
I was using it for the likes of McDonald, O'Reilly etc when I was keying in all my data for my Bibby database,
 

isladogs

MVP / VIP
Local time
Today, 06:26
Joined
Jan 14, 2017
Messages
18,209
Hi Paul

Yes I realised there was additional functionality in the other functions that you posted.
In fact these would have been useful to me in the past rather than plough through loads of records manually as I did.

I was only giving a simpler alternative to the first function listed
Apologies if I gave the wrong impression.
 
Last edited:

JaniceKL

New member
Local time
Today, 15:26
Joined
Dec 1, 2021
Messages
9
I was given this code a good while back from this site for one of my databases.
Sorry I have forgotten who it was, but I do remember he helped me with the NotInList event for my data entry.

It certainly worked for my purposes.

HTH

Code:
'************** Code Start *************
'This code was originally written by Jay Holovacs.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Jay Holovacs
'
Public Function mixed_case(str As Variant) As String
'returns modified string, first character of each word us uppercase
'all others lower case
Dim ts As String, ps As Integer, char2 As String
    If IsNull(str) Then
        mixed_case = ""
        Exit Function
    End If
    str = Trim(str) 'added 11/22/98
    If Len(str) = 0 Then
        mixed_case = ""
        Exit Function
    End If
    ts = LCase$(str)
    ps = 1
    ps = first_letter(ts, ps)
    Special_Name ts, 1 'try to fix the beginning
    Mid$(ts, 1) = UCase$(Left$(ts, 1))
    If ps = 0 Then
        mixed_case = ts
        Exit Function
    End If
    While ps <> 0
        If is_roman(ts, ps) = 0 Then 'not roman, apply the other rules
            Special_Name ts, ps
            Mid$(ts, ps) = UCase$(Mid$(ts, ps, 1)) 'capitalize the first letter
        End If
        ps = first_letter(ts, ps)
    Wend
    mixed_case = ts
End Function
Private Sub Special_Name(str As String, ps As Integer)
'expects str to be a lower case string, ps to be the
'start of name to check, returns str modified in place
'modifies the internal character (not the initial)

Dim char2 As String
char2 = Mid$(str, ps, 2) 'check for Scots Mc
If (char2 = "mc") And Len(str) > ps + 1 Then '3rd char is CAP
    Mid$(str, ps + 2) = UCase$(Mid$(str, ps + 2, 1))
End If

char2 = Mid$(str, ps, 2) 'check for ff
If (char2 = "ff") And Len(str) > ps + 1 Then 'ff form
    Mid$(str, ps, 2) = LCase$(Mid$(str, ps, 2))
End If

char2 = Mid$(str, ps + 1, 1) 'check for apostrophe as 2nd char
If (char2 = "'") Then '3rd char is CAP
    Mid$(str, ps + 2) = UCase$(Mid$(str, ps + 2, 1))
End If

Dim char3 As String
char3 = Mid$(str, ps, 3) 'check for scots Mac
If (char3 = "mac") And Len(str) > ps + 1 Then 'Mac form
    Mid$(str, ps + 3) = UCase$(Mid$(str, ps + 3, 1))
End If

Dim char4 As String
char4 = Mid$(str, ps, 4) 'check for Fitz
If (char4 = "fitz") And Len(str) > ps + 1 Then 'Fitz form
    Mid$(str, ps + 4) = UCase$(Mid$(str, ps + 4, 1))
End If

End Sub
Private Function first_letter(str As String, ps As Integer) As Integer
'ps=starting point to search (starts with character AFTER ps)
'returns next first letter, 0 if no more left
'modified 6/18/99 to handle hyphenated names
Dim p2 As Integer, p3 As Integer, s2 As String
    s2 = str
    p2 = InStr(ps, str, " ") 'points to next blank, 0 if no more
    p3 = InStr(ps, str, "-") 'points to next hyphen, 0 if no more
    If p3 <> 0 Then
        If p2 = 0 Then
            p2 = p3
        ElseIf p3 < p2 Then
            p2 = p3
        End If
    End If
    If p2 = 0 Then
        first_letter = 0
        Exit Function
    End If
    'first move to first non blank, non punctuation after blank
    While is_alpha(Mid$(str, p2)) = False
        p2 = p2 + 1
        If p2 > Len(str) Then 'we ran off the end
            first_letter = 0
            Exit Function
        End If
    Wend
    first_letter = p2
End Function
Public Function is_alpha(ch As String)
'returns true if this is alphabetic character
'false if not
    Dim c As Integer
    c = Asc(ch)
    Select Case c
        Case 65 To 90
            is_alpha = True
        Case 97 To 122
            is_alpha = True
        Case Else
            is_alpha = False
    End Select
   
End Function
Private Function is_roman(str As String, ps As Integer) As Integer
'starts at position ps, until end of word. If it appears to be
'a roman numeral, than the entire word is capped in passed back
'string, else no changes made in string
'returns 1 if changes were made, 0 if no change
Dim mx As Integer, p2 As Integer, flag As Integer, i As Integer
    mx = Len(str) 'just so we don't go off the edge
    p2 = InStr(ps, str, " ") 'see if there is another space after this word
    If p2 = 0 Then
        p2 = mx + 1
    End If
    'scan to see if any inappropriate characters in this word
    flag = 0
    For i = ps To p2 - 1
        If InStr("ivxIVX", Mid$(str, i, 1)) = 0 Then
            flag = 1
        End If
    Next i
    If flag Then
        is_roman = 0
        Exit Function 'this is not roman numeral
    End If
    Mid$(str, ps) = UCase$(Mid$(str, ps, p2 - ps))
    is_roman = 1
End Function
'************** Code End  *************
Hello - I am also a novice with coding. I have copied and pasted all of the above, and other similar codes, but I receive the following message. Any help would be great please.

1638488743038.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:26
Joined
May 7, 2009
Messages
19,229
can you post the SQL string of your query?
 

Users who are viewing this thread

Top Bottom