Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2022
    Posts
    17

    Change existing VBA code to adjust results

    Good afternoon,
    Currently I am using the following VBA:
    Private Sub LAST_NAME_AfterUpdate()
    Me.[LAST NAME] = StrConv(Me.[LAST NAME], vbProperCase)
    End Sub

    to change any typing of the last name to proper case, and it works great.
    Today, more and more people are hyphenating their last name, i.e. Smith-Brown
    How can I change my above code to make this work? Otherwise, the result is Smith-brown.
    Thank you,


    Frank

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Substitute the - for a space, ProperCase it and replace the - back?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Problem with WGM idea is that if the last name string naturally contains a space (like St Pierre), it will be replaced with a dash (St-Pierre).

    Here's a VBA solution that uses a couple of UDFs. It will properly handle:
    Smith-jones
    smith- jones
    smith -jones
    smith - jones
    smith--jones
    smith --jones
    smith-- jones
    smith -- jones


    If the argument is too convoluted, the code will return the original name along with a messagebox recommending manual change required.

    Code:
    Public Function fcnProper(sLName As String) As String
        Dim sLeft As String, sRight As String, sWorker As String
        Dim nDashPos As Long
        sWorker = Trim(sLName)
        sWorker = Replace(sWorker, "--", "-")   'replace double dash
        If fcnCountOcc(sWorker, "-") > 1 Then
            MsgBox "Unexpected format of name " & sLName & ". Manual correction required."
            Exit Function
        End If
        If InStr(sWorker, "-") = 0 Then
            fcnProper = StrConv(sWorker, vbProperCase)
            Exit Function
        End If
        nDashPos = InStr(sWorker, "-")
        sRight = Trim(Mid(sWorker, nDashPos + 1))
        sLeft = Trim(Mid(sWorker, 1, Len(sRight)))
    'Debug.Print sLeft, sRight
        sRight = StrConv(sRight, vbProperCase)
        sLeft = StrConv(sLeft, vbProperCase)
        fcnProper = sLeft & "-" & sRight
    End Function
    
    
    Function fcnCountOcc(sText As String, sTarget As String) As Long
        ' Counts occurrences of a particular character
        Dim nCount As Long, i As Long
        sText = Trim(sText)
        For i = 1 To Len(sText)
            If Mid(sText, i, 1) = sTarget Then
                nCount = nCount + 1
            End If
        Next i
        fcnCountOcc = nCount
    End Function

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I would use a different substitution

    ?replace(strconv(replace("smith-brown","-"," - "),vbProperCase)," - ","-")
    Smith-Brown
    ?replace(strconv(replace("st pierre","-"," - "),vbProperCase)," - ","-")
    St Pierre

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Adjust this VBA code to include an image header
    By mchadwick in forum Modules
    Replies: 1
    Last Post: 02-11-2023, 03:28 PM
  2. MS Access - Graphing, Adjust scale via code
    By ironfelix717 in forum Forms
    Replies: 12
    Last Post: 03-20-2020, 12:02 AM
  3. Replies: 5
    Last Post: 07-14-2018, 07:22 AM
  4. Replies: 1
    Last Post: 09-22-2016, 02:37 AM
  5. Replies: 5
    Last Post: 08-29-2016, 04:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums