Results 1 to 10 of 10
  1. #1
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142

    Extract Country Code from a larger field

    I have a field called "comment" that will contain some information including a country code. The total length of the comment will vary. The country code can be 2 or 3 positions, will always be at the end of the comments field, and will always be enclosed inside parenthesis. I was to extract the 2 or 3 position country code from that comment field.



    Here are some examples:

    Package(FR) I want FR extracted from that into another field

    Package(GB) I want GB extracted from that into another field

    Letter(IRE) IRE

    Fax(UAE) UAE

    Thanks

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Something like this should do the trick.

    Code:
    Dim strFile As String
    strFile = "Package(GB)foo(f)d"
    strFile = Left(strFile, InStr(strFile, ")") - 1)   'Trim everything to the right of first Parenthesis
    strFile = Mid(strFile, InStr(strFile, "(") + 1)   'Trim everything to the Left of first Parenthesis
    MsgBox strFile

  3. #3
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142
    Is there any way to do this within one statement?

    This is a field inside a table that's already part of a query and I just want to extract the country inside one of the columns.

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    try this in a query

    Countrycode:strrev(mid(strrev([Comment]),2,instr(strrev([Comment]),")")-2))

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    strrev should probably be InStrRev
    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.

  6. #6
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    strrev should probably be InStrRev
    no - reversed the comment in case there happened to be another bracket in the comment

    but could also do

    Countrycode:left(mid([comment],instrrev([Comment],"(")),len(mid([comment],instrrev([Comment],"(")))-1)

    or

    Countrycode:replace(mid([comment],instrrev([Comment],"(")),")","")

    which is probably simplest

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    StrRev is not valid function in VBA immediate window for my test.

    This works. x represents the field.

    Left(Mid(x,InStr(x,"(")+1), Len(Mid(x,InStr(x,"(")+1))-1)

    EDIT: agree, Replace simplest
    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.

  8. #8
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142
    Quote Originally Posted by Ajax View Post
    try this in a query

    Countrycode:strrev(mid(strrev([Comment]),2,instr(strrev([Comment]),")")-2))
    This got an undefined function. strrev

  9. #9
    wcrimi is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    142
    Quote Originally Posted by June7 View Post
    StrRev is not valid function in VBA immediate window for my test.

    This works. x represents the field.

    Left(Mid(x,InStr(x,"(")+1), Len(Mid(x,InStr(x,"(")+1))-1)

    EDIT: agree, Replace simplest
    We have a WINNER!

    Thank you!

  10. #10
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    StrRev is not valid function in VBA immediate window for my test.
    Ah yes - me thinking in shorthand - should be strReverse

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

Similar Threads

  1. Where Condition larger than 255
    By intrepid in forum Access
    Replies: 7
    Last Post: 02-28-2015, 02:44 PM
  2. Replies: 3
    Last Post: 02-10-2015, 07:01 PM
  3. Replies: 1
    Last Post: 04-30-2013, 07:00 AM
  4. extract something from a field
    By tozey in forum Queries
    Replies: 4
    Last Post: 02-24-2012, 08:54 AM
  5. Country Code Calculation
    By nchesebro in forum Forms
    Replies: 14
    Last Post: 06-15-2011, 02:03 PM

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