Results 1 to 13 of 13
  1. #1
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63

    Extra spaces not removed by Trim or Replace functions

    I have text fields in a table that for some reason aren't responding to the Trim or Replace functions. I'm trying to remove the trailing spaces after the text but whatever the length of the text it pads the field with enough spaces to make 255 and I can't get rid of them. I enter the text using SQL from another table that I imported from an Excel workbook.

    Has anyone run into this before? Any ideas?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Please post an example o the text and the routine you're using to Trim it/them.
    Are you certain they are spaces?

  3. #3
    Join Date
    Apr 2017
    Posts
    1,681
    Are data copy-pasted or exported from some web site? There often are used non-ASCII characters/UNICODE characters, which look like normal ones, but have different code, and for any database are not same as ASCII characters you enter when typing. Copy some such "space" into Excel, enter normal space into another field, and use CODE() function on both fields - is the code returned same for both spaces?

  4. #4
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    I didn't understand your post. I assume that they are spaces after the text.

  5. #5
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    UPDATE tblTemplate SET tblTemplate.Field1 = Replace([Field1]," ","");
    UPDATE tblTemplate SET tblTemplate.Field1 = Trim([Field1]);

  6. #6
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    I copied some of the extra space into Excel and used the Code function. It returned 32, indicating spaces.

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    If the target field is a string (and it must be if it has any spaces), this example will work. Both updates do the same thing, except the replace will also eliminate embedded spaces.

    Code:
    Private Sub cmdClean_Click()
        Dim sSQL As String
        sSQL = "UPDATE tblInformation SET tblInformation.CustomerName = '" & Replace([CustomerName], " ", "") & "';"
        CurrentDb.Execute sSQL, dbFailOnError
        sSQL = "UPDATE tblInformation SET tblInformation.CustomerName = '" & Trim([CustomerName]) & "';"
         'Debug.Print sSQL
        CurrentDb.Execute sSQL, dbFailOnError
        Me.Requery
    End Sub

  8. #8
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    Thank you for that. I tried using a loop structure that tests the ASCII value of each element of the string and if it's a letter then it adds it to a new string. Otherwise, it doesn't add the character. I then add the new string to a new field.

  9. #9
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    I tried using a loop structure
    That approach should work, and it's a good exercise in code structure.
    Not a good approach if you have a large number of records to process, as it will take some time.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    In my experience, Access automatically drops trailing spaces. So some other character would have to follow the spaces in order for them to be retained which in this case must be non-printing since you can't see it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  12. #12
    Join Date
    Apr 2017
    Posts
    1,681
    Quote Originally Posted by Euler271 View Post
    I copied some of the extra space into Excel and used the Code function. It returned 32, indicating spaces.

    Copy the whole string, and get the code for rightmost character of string.

  13. #13
    Euler271 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2017
    Posts
    63
    Thanks to all who helped. Interestingly, I found that when I programatically created a new table using SQL ("CREATE TABLE") and copy records to it the problem happens. But when I create the same table in DAO ("CreateTableDef") I don't get the problem. I hope this will help others with this frustrating issue.

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

Similar Threads

  1. Replies: 9
    Last Post: 02-25-2017, 09:15 PM
  2. How to remove extra spaces from a field?
    By accessmatt in forum Queries
    Replies: 1
    Last Post: 09-22-2014, 06:57 AM
  3. Replies: 1
    Last Post: 04-16-2013, 07:57 PM
  4. Replies: 2
    Last Post: 04-11-2013, 06:36 PM
  5. Replace Carriage Returns with Spaces
    By chitan in forum Queries
    Replies: 1
    Last Post: 12-15-2011, 11:14 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