Results 1 to 7 of 7
  1. #1
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200

    Len() Function not Including Spaces

    I am trying to use the Len() function to return the number of characters for a string. However, the function does not return any trailing spaces. Is there a workaround for this? Any other function I could use?

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    What is the context in which you are using Len()? A quick test showed that it will count the trailing spaces if they are there. For example, len("A ") (3 spaces following 'A') will yield 4, as expected.

    However, if you type 'A' followed by 3 spaces into a form field, and then test the length in the After Update, it yields only 1 - trailing spaces are removed.

    HTH

    John

  3. #3
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    I am using it on a form field, so I am running into that second scenario.

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by tylerg11 View Post
    I am using it on a form field, so I am running into that second scenario.
    That being the case, as John said, Access automatically removes trailing spaces!

    Perhaps if you told us why you're trying to do this, we could suggest a workaround.

    Linq ;0)>

  5. #5
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    I have a textbox that is used as a filter box. After the textbox text is changed (i.e. a new character is added) then it filters the subform below. After it filters the subform, it returns to the filter box placing the cursor after the string. However, if a criteria item is typed like "se ", the cursor will go right after the "e". Basically, a space can't be inserted as filter criteria in the text box because the Len() that is returned eliminates the space.

  6. #6
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    Here is the code of what I am trying to do..

    Code:
    Private Sub txtFilter_Change()
     
        If Nz(Me.txtFilter.Text, "") = "" Then
            Me.FilterOn = False
            Me.txtFilter.SetFocus
            Exit Sub
        End If
     
        Me.Filter = "lastName like '" + Me.txtFilter.Text + "%' or userName like '" & _
                    Me.txtFilter.Text + "%'"
     
        Me.FilterOn = True
        Me.txtFilter.SetFocus
     
        Me.txtFilter.SelStart = Len(Nz(Me.txtFilter.Text, "")) + 1
     
    End Sub
    This last line is where I am having issues. If the last character of txtFilter is a " ", then Access trims it out, and so I can never account for a space when setting the cursor location.

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You might take a look at Allen Browne's article and hack called "Find as you type" that does what you're attempting, moving to a Record as you enter text:

    http://allenbrowne.com/AppFindAsUType.html

    Also note that in Access VBA, in your code
    Code:
    Me.Filter = "lastName like '" + Me.txtFilter.Text + "%' or userName like '" & _
                    Me.txtFilter.Text + "%'"
    the Plus Signs (+) should probably be replaced with Ampersands (&), and the correct Wildcard Character is an Asterisk (*) rather than the Percent Sign (%).

    Using the Plus Sign to Concatenate Strings has been retained, for backward compatibility, but it can lead to unexpected results at times.

    The Percent Sign is valid for Wildcard characters in Access SQL, I believe, but not in Access VBA code.

    Linq ;0)>

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

Similar Threads

  1. Replies: 8
    Last Post: 06-08-2012, 07:17 PM
  2. Report to Excel but including header.
    By bbarclay in forum Import/Export Data
    Replies: 1
    Last Post: 05-16-2012, 01:00 PM
  3. Including a user guide within an application
    By PippaPippa in forum Forms
    Replies: 7
    Last Post: 03-10-2012, 04:36 AM
  4. Replies: 2
    Last Post: 08-04-2011, 08:07 AM
  5. Including two queries in one report
    By kulanga in forum Reports
    Replies: 1
    Last Post: 03-23-2010, 10:21 PM

Tags for this Thread

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