Results 1 to 10 of 10
  1. #1
    JimO is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    126

    Best way to eliminate extra spaces in a formula

    This is my formula to display the Full Name. My issue is that if there is only say a first name and a last name the full name has an extra space in it (Jim space space Jones), I am looking for a way to correct this without using an IIf clause for each argument and building a long involved formula. I am not sure if this is possible, I thought using the Trim would be sufficient but it is not.



    Code:
    FullName: Trim([Prefix] & " " & [FirstName] & " " & [MiddleName] & " " & [LastName] & " " & [Suffix])
    Jim O

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051

    Best way to eliminate extra spaces in a formula

    Since trim only removes leading and end spaces why not use


    Replace (text, " "," ")


    Sent from my iPhone using Tapatalk

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You mighty try just using a "+" instead of a "&" in the right place. The "+" will propagate the null value so no space will be added if the [MiddleName] is null.

    FullName: Trim([Prefix] & " " & [FirstName] & " " & [MiddleName] + " " & [LastName] & " " & [Suffix])

  4. #4
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Now I never knew that. Thanks rural!![emoji3]


    Sent from my iPhone using Tapatalk

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Glad to provide the information. It's a slick trick.

  6. #6
    JimO is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    126
    Would I use that in addition to Trim or in place of Trim?

    Jim O

  7. #7
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Don't think trim will make any difference.


    Sent from my iPhone using Tapatalk

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Trim will take care of any leading and trailing spaces so it is in addition to...

  9. #9
    JimO is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    126
    Thanks to all for the time and expertize.

    Jim O

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Just for the record, you could use the same scheme for the [Prefix] and [Suffix] fields and not use Trim:
    FullName: [Prefix] + " " & [FirstName] & " " & [MiddleName] + " " & [LastName] & " " + [Suffix]

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

Similar Threads

  1. Replies: 19
    Last Post: 07-13-2015, 10:15 AM
  2. Replies: 41
    Last Post: 05-29-2015, 02:58 PM
  3. How to remove extra spaces from a field?
    By accessmatt in forum Queries
    Replies: 1
    Last Post: 09-22-2014, 06:57 AM
  4. Replies: 2
    Last Post: 08-14-2014, 04:00 PM
  5. Replies: 2
    Last Post: 04-11-2013, 06:36 PM

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