Results 1 to 14 of 14
  1. #1
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146

    Mask out leading space and trailing space from a string of characters


    After a long time figuring out that my problem was due to a space before a string of characters I realized I needed to prevent a space before and after a string of characters, but not inside the string.

    Thanks, will check tomorrow, past my bedtime

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Mystring = Trim(Mystring)

  3. #3
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    Right answer to the question. However wrong question. That's the real battle, asking the right question.

    A better question might be: How to build an input mask that would not allow a leading or trailing space and allow for variable length string with an underscore in the midst. I tried, I'm not sure it's possible.

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Doesn't sound possible to design it to me either. While you could build one, the impossible part might be deciding how many optional characters to allow for, plus how would the user know that the n, x and y positions are not optional? You can build the input mask in code, so if you can identify the condition you could build the string in a Select Case block? The InputMask property belongs to the textbox control. I've never done this though.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    100% true: the real battle, asking the right question.


    Perhaps to clarify exactly what is required, you could post some typical values. Probably better to show "acceptable and not acceptable values" and where exactly this will be used. If any of this is the responsibility of a user, then explicit instructions/training may be required. If it is"free form by user", then all bets are off. Why input mask? You could parse user input against validation info and adjust anything a user enters to meet your requirement.
    Last edited by orange; 08-08-2021 at 11:08 AM. Reason: spelling

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,980
    I wouldn't even attempt to do this with an input mask. They often make life more difficult for end users.

    Just run code to trim the textbox input string in the after update event.
    Also, only the leading space(s) are an issue. Access will ignore any trailing spaces anyway.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    I would trim in BeforeUpdate. Wouldn't use a mask as I noted but how does that simple approach accommodate an underscore character in a seemingly random spot?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,980
    I would trim in BeforeUpdate.
    Hmm.... Not sure how that would be possible.

    BeforeUpdate is used for validating data ... not for modifying it.
    Attempting to trim the entered data using code in before update event would lead to a runtime error.
    Am I missing something here?
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Probably not.
    Not sure I will be able to prove you right until tomorrow. However it is usual to not permit incorrect values with the intention to modify to make the values conform?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,980
    Quote Originally Posted by Micron View Post
    Not sure I will be able to prove you right until tomorrow
    Love that comment ....
    Here's an example

    Click image for larger version. 

Name:	Capture.PNG 
Views:	18 
Size:	18.3 KB 
ID:	45938

    It works fine in the AfterUpdate event
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    That's pretty clear.
    I'd still like to hear the rationale for this requirement from the OP.

  12. #12
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Colin, I have to agree with you and I forgot about that runtime error. I presume that a form update event will take care of editing the record (the Trim'd value) as long as the control is bound?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,980
    Using exactly the same code in the control's after update event trims the entered data and updates the record accordingly.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    This is what I ended up with, for now anyways:

    Code:
      Private Sub tbxWOInfo2_Click()
      Dim S1 As String, S2 As String
       
      S2 = tbxWOInfo2.Value
       
              S1 = Trim(InputBox("ENTER DESCRIPTION AND WO#" & vbNewLine & vbNewLine & "Example: C-7 PM_T103892", "Statement")) ' TRIM ANY LEADING SPACES ON INPUT
              tbxWOInfo2.Value = S1
              
              If S1 = "" Then
              tbxWOInfo2.Value = S2
              End If
      End Sub
    This deals with the leading edge, avoids the pitfalls of making an input mask, and deals with keeping previous input if user decides to cancel.

    Thanks for all reply's

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

Similar Threads

  1. Replies: 1
    Last Post: 09-14-2017, 02:19 PM
  2. Replies: 2
    Last Post: 04-08-2016, 12:24 PM
  3. Get trailing Characters from String
    By jaykappy in forum Queries
    Replies: 4
    Last Post: 09-03-2014, 09:19 AM
  4. Leading Spaces and space after name
    By Sumanth.Ganjam in forum Access
    Replies: 5
    Last Post: 11-13-2013, 04:04 AM
  5. Replies: 6
    Last Post: 10-18-2013, 07:30 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