Results 1 to 8 of 8
  1. #1
    razkowski is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    16

    Replace Function

    Hello-



    I'm trying to use the replace function in an access query to find and replace certain characters. The data in the field is: (Unit #2436; Contract #03-36-036801).

    I only want the 10 digit code with the dashes: 03-36-036801. So, I need to remove "(Unit #2436; Contract #" and ")" at the end.

    How do I go about this?

    Thanks

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Do all you entries ends with a ")", and are the 10 digits you want always the last 10 characters before this ")"?
    If so, you can use the MID and LEN functions to get what you need:
    MID([FieldName],LEN([FieldName])-12,12)

  3. #3
    razkowski is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    16
    Sometimes there will be characters after the last ")".

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    OK, but is it always the 12 characters previous to the ")"? If so, then this will work:
    Mid([FieldName],InStr([FieldName],")")-12,12)

  5. #5
    razkowski is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    16
    99% of the time, yes. However, I've just realized that on rare occasions, there are additional characters before the last ")" that I do not wish to include. Like this: (Unit 46; Contract #03-11-003831-47-35)

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why is multiple discrete data in the same field to begin with?

    You want the 12 characters following the second # character (oh wait, second example has only 1 #)? Will "Contract #" always be in the string? There will never be a space following the #?

    Consistency is critical with string manipulation.

    x represents your field:

    Mid(x, InstrRev(x, "#") + 1, 12)
    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.

  7. #7
    razkowski is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    16
    June7 - your response did the trick. Works perfectly. To answer your question, because that's the way the data comes from our vendor. We have tried numerous times to get them to report in a fashion that is more suitable to us. It hasn't happened. So, plan B is to extract the data that is meaningful to me from all their nonsense data. That's what you just accomplished for me.

    Thanks

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    As you can see, the "trick" is usually identifying the pattern/rule to the data you are working. That is the first thing you want to identify, all the possible scenarios. Then from there, you can create a formula to do that.

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

Similar Threads

  1. Using a wildcard with the replace function
    By razkowski in forum Queries
    Replies: 6
    Last Post: 08-12-2014, 08:13 AM
  2. Replace Function
    By thescottsman92 in forum Access
    Replies: 5
    Last Post: 09-02-2013, 01:25 AM
  3. Help with Replace Function...
    By redbull in forum Programming
    Replies: 5
    Last Post: 06-27-2013, 04:05 PM
  4. Error with Replace Function
    By Juan4412 in forum Queries
    Replies: 1
    Last Post: 09-30-2012, 05:48 PM
  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