Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    310

    Replace Function with Left function

    Hi to All,

    I have an Input mask of text field with size 19 characters as follows:

    000.###.###" ">CCCC<aCaa;_



    I want to remove spaces in the first 14 characters and keep the rest (in case the user used space).

    In the after update event of the control on the form, I used the following line of code:
    Me.txtCallNumber = Replace(Left(strCallNumber, 14), " ", "")
    This code is not working, it removes the space from ALL the string not only the first 14 characters.

    Any suggestions?

    Khalil

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You're telling it to take the first 14 chars as a group and remove all the spaces. What you're left with should be nothing because you've dropped the remainder. But you don't indicate that's the case. What do you get as a result?

    EDIT
    Should clarify that would be the case if the first 14 were spaces. Not sure what your input is. I asked for an output example, but you should provide the input for it as well as I get varying results depending on what I feed it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    310
    Quote Originally Posted by Micron View Post
    You're telling it to take the first 14 chars as a group and remove all the spaces. What you're left with should be nothing because you've dropped the remainder. But you don't indicate that's the case. What do you get as a result?

    EDIT
    Should clarify that would be the case if the first 14 were spaces. Not sure what your input is. I asked for an output example, but you should provide the input for it as well as I get varying results depending on what I feed it.
    Here is a sample:
    input output how it should be
    26 72 ADC 4 2672ADC4 2672ADC 4

    In other words it should keep spaces after the alphabetic part (the letters) and remove spaces before the letters only

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    How about:

    ?Replace(Left("26 72 ADC 4 2672ADC4 2672ADC 4", 14), " ", "") & mid("26 72 ADC 4 2672ADC4 2672ADC 4", 15)
    2672ADC42672ADC4 2672ADC 4
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    There's a function that returns a number set, stopping as soon as it encounters text. I'm out now and can't research it. Thinking using Replace on that function's return would be simpler and more dynamic.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Sounds like Val():

    ?val("123abc")
    123

    but the first 14 characters in the example have alpha characters among them.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    310
    Quote Originally Posted by pbaldy View Post
    Sounds like Val():

    ?val("123abc")
    123

    but the first 14 characters in the example have alpha characters among them.

    I tried the following:
    Me.txtCallNumber = Replace(Left(strCallNumber, 14), " ", "") & Mid(strCallNumber, 15)

    My input:
    262 74 RTC 7

    the output was:
    26274RTC7
    The must be a space after the letter C.

    Thanks

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Quote Originally Posted by pbaldy View Post
    Sounds like Val():
    ?val("123abc")
    123
    but the first 14 characters in the example have alpha characters among them.
    it should keep spaces after the alphabetic part (the letters) and remove spaces before the letters only
    Then I guess I don't understand the requirement based on that, as the sample "26 72 ADC 4" is only 11 characters long - including spaces.

    When I ask for sample inputs and outputs, I always hope for more than just one so that I can detect patterns or get clarification when the situation seems to differ from that which was stated. It's getting harder for me to not get frustrated by such minimal inputs/outputs.

    Regardless, Val() it was, but is probably of no use anyway.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Quote Originally Posted by Khalil Handal View Post
    I tried the following:
    Me.txtCallNumber = Replace(Left(strCallNumber, 14), " ", "") & Mid(strCallNumber, 15)

    My input:
    262 74 RTC 7

    the output was:
    26274RTC7
    The must be a space after the letter C.

    Thanks
    That's within the first 14 characters and you said "I want to remove spaces in the first 14 characters and keep the rest".
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    310

    Post

    Quote Originally Posted by pbaldy View Post
    That's within the first 14 characters and you said "I want to remove spaces in the first 14 characters and keep the rest".
    The string length is 19 characters; The user might enter less number of characters, say 6 or 10 or 19.
    The first part of the string is numbers only that might include spaces followed by 3 or 4 letters. After the letters numbers might be entered with spaces. I want these spaces to be kepts.
    Here are more sample for different cases:

    User input: 272 TOM 4-5
    output: 272TOM4-5
    how it should be: 272TOM 4-5

    User input: 27265 SAM 33
    output: 27265SAM33
    How it should be: 27265SAM33

    User input: 168654359EPKH 6 8
    Output: 168654359EPKH68
    Requested: 168654359EPKH 6 8


    These outputs are based on the code I am using now which I mentioned previously.
    I hope this clarify it.

    Khalil

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Ah, a moving target. You were very specific about 14 characters earlier. The only way I can think of is a custom function that loops through the characters and determines the last alpha character. Then using that use a formula like earlier but with the 14 replaced by the value found previously as the last alpha character.

    And why no space here before the 33?

    How it should be: 27265SAM33
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    That will have to be more robust than I think you're thinking. There are spaces before the 6 an 8, both of which remain in the example.
    Last edited by Micron; 04-11-2019 at 01:38 PM. Reason: Stoopid fone ottokorrect

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Quote Originally Posted by Micron View Post
    That will have to be more robust than I think you're thinking. There are spaces before the 6 an 8, not of which remain in the example.
    Is this directed at me? The function I'm envisioning would handle that, the formula supplied earlier was intended for the "14" restriction initially mentioned.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Yes, it was but upon review, I'd say you are right in what you said.

  15. #15
    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,870
    @Khalil

    I suggest you confirm your requirement since it seems to be moving as others have suggested.
    Test, confirm and post a few examples of the revised requirement.
    You might even describe what these text strings represent and why the format is so critical.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replace Value with IFF Function
    By CryptikFox in forum Programming
    Replies: 7
    Last Post: 01-28-2019, 10:34 PM
  2. Replace Function
    By razkowski in forum Queries
    Replies: 7
    Last Post: 10-08-2014, 02:28 PM
  3. Replace Function
    By thescottsman92 in forum Access
    Replies: 5
    Last Post: 09-02-2013, 01:25 AM
  4. mid,left function
    By revned12 in forum Queries
    Replies: 2
    Last Post: 07-06-2012, 12:09 AM
  5. Replies: 3
    Last Post: 06-07-2012, 07:05 AM

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