Results 1 to 5 of 5
  1. #1
    PasJes is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2023
    Posts
    6

    Change to proper case...with exceptions

    I know there are ways of easily changing case in Access...to upper, lower or proper.

    Just wondeirng if there is any code anyone has that builds in exceptions when trying to convert to proper case.

    For example, words (or name) like O'BRIEN...should convert to O'Brien....not O'brien.

    And to ignore hyphens, so that MERCEDES-BENZ gets converted to Mercedes-Benz and not Mercedes-Benz.

    I'm fairly new to all this...so any assistance greatly appreciated.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Often the requirements are quite specific so need to be handled on a trial and error basis so take a look at the similar threads section at the bottom of this thread

    to handle ‘ and - you can use the split function on the appropriate character then rebuild the string - but then you might find even that is not enough. For example ‘de la Rue’ only wants the last element with a capital letter

  3. #3
    Join Date
    Apr 2017
    Posts
    1,680
    Unless the number you have to deal with such exceptions is very big, maybe you can simply create a table of exceptions - with a field for value to convert, and one for converted value (or several ones for different conversion rules). When converting table field, you join this exceptions table into your replace query. When join field is not null, you read converted value from conversion table, otherwise you apply Upper(), Lower(), or Proper().

    'Mercedes-Benz and not Mercedes-Benz' - what is the difference?

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Here is something I used in one of my Dbs.
    You will likely need to modify it for any other types?, but at least it is a start.
    You will need to remove the .txt extension, as the site does not accept .bas files.
    Attached Files Attached Files
    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

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    just put this together - Edit - missed a bit

    Code:
    Function properCase(str As String) As String
    'could put these in a table
    Const exception = "de,la"
    Const delim = "-,'"
    
    
    Dim e() As String
    Dim d() As String
    Dim i As Integer
        
        properCase = StrConv(str, vbProperCase)
    
        d = Split(delim, ",")
        For i = 0 To UBound(d)
            If InStr(str, d(i)) Then
                properCase = Replace(StrConv(Replace(str, d(i), " " & d(i) & " "), vbProperCase), " " & d(i) & " ", d(i))
            End If
        Next i
        
        e = Split(exception, ",")
        For i = 0 To UBound(e)
            If InStr(properCase, " " & e(i) & " ") Then
                properCase = Replace(properCase, " " & e(i) & " ", " " & e(i) & " ")
            End If
            
        Next i
       
    End Function
    examples
    ?propercase("mercedes-benz de la rue")
    Mercedes-Benz de la Rue

    ?propercase("o'brien")
    O'Brien

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

Similar Threads

  1. VBA for Proper Case
    By fjcunninghamjr in forum Forms
    Replies: 1
    Last Post: 11-15-2022, 09:01 AM
  2. Proper Case and Exceptions
    By bcarter17 in forum Access
    Replies: 31
    Last Post: 09-24-2021, 01:58 PM
  3. Replies: 5
    Last Post: 05-14-2019, 07:48 AM
  4. Replies: 4
    Last Post: 04-28-2019, 07:19 PM
  5. Change text case from upper to proper case
    By s.nolan in forum Access
    Replies: 1
    Last Post: 12-02-2015, 10:56 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