Results 1 to 12 of 12
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046

    Query -- Extract string between two characters ("," and "~")

    Experts:

    I need some assistance with an **IN BETWEEN TWO CHARACTERS** query.

    Below are two (2) sample strings which include variable length last names, first names, and ranks:
    Jackson, Tom ~ PO2 (E5)
    Smith, Mary Ann ~ PO1 (E6)

    - All last names and first names are separated by a comma (,)
    - All first names and ranks are separated by a tilde (~)



    Now, I figured out how to extract both the last name and rank title. However, I have NOT successfully extracted the variable length first name.

    LastName: Left([CHOPS],InStr([CHOPS],", ")-1):
    Jackson
    Smith

    RankTitle: Mid([CHOPS],InStr([CHOPS],"~")+2)
    PO2 (E5)
    PO1 (E6)

    For first name though, I tried the following: FirstName: Mid([CHOPS],InStr([CHOPS],",")+2,Len([CHOPS])-InStr([CHOPS]," ~ ")-1)

    I only provided these two examples (Tom, Ann); unfortunately, based on my current data set, the names are something chopped of though. How to I modify the FirstName query so that I get all characters between the "," and "~" (without leading/trailing spaces before/after name?
    Please keep in mind that some first names have blank space (e.g., Mary Ann) while others may be hyphenated (Jean-Pierre).


    Thank you,
    EEH

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Code:
    'loop thru all records
    While Not .EOF
        'get vLine
        vLine  = 'get line here
    
        i = InStr(vLine, "~")
        While i > 0
            vName = Left(vLine, i - 1)
            vLine = Mid(vLine, i + 1)
            
            'now with vName, separate last & first
            i = InStr(vName, ",")
            vLast = Left(vName, i - 1)
            vFirst = Left(vName, i + 1)
            
              'get next name
            i = InStr(vLine, "~")
        Wend
        .MoveNext
    Wend
    

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    you nearly had it right with

    FirstName: Mid([CHOPS],InStr([CHOPS],",")+2,Len([CHOPS])-InStr([CHOPS]," ~ ")-1)

    modify to this

    FirstName: trim(left(mid([CHOPS],InStr([CHOPS],",")+1),InStr([CHOPS],"~")-1))

  4. #4
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    RANMAN: I prefer to use a simple query statement (vs. VBA function). Just like I did for lastname and rank. Back to my question:

    Do you know how to modify "FirstName: Mid([CHOPS],InStr([CHOPS],",")+2,Len([CHOPS])-InStr([CHOPS]," ~ ")-1) so that I get all characters between comma and tilde?

  5. #5
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    Ajax:

    Thank you for the support. I plugged it in and I get the following sample results:

    Mike ~ PO2 (
    Patrick ~ PO1
    Kenneth ~ PO2 (E5
    Mary ~ PO2

    Instead, I should only have:
    Mike
    Patrick
    Kenneth
    Mary

    How should this be tweak to only get everything until the ~ (-1 character for blank space)?

    Tom

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    my bad

    trim(left(mid([CHOPS],InStr([CHOPS],",")+1),InStr(mid([CHOPS],InStr([CHOPS],",")+1),"~")-1))

  7. #7
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    Ajax -- it works perfectly now!! Thank you so much for the help.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    happy to help

  9. #9
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Just for the challenge.
    Code:
    Trim(Mid(Left([CHOPS],InStrRev([CHOPS],"~")-1),InStrRev([CHOPS],",")+1))
    Saves one mid() and one +1.

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    good one - have you noticed how often by reversing the logic, the code becomes simpler and/or shorter?

  11. #11
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Oh yes!
    Always, we have to keep alive our deviant thinking, the childhood creative ability that we lose as we grow older.

  12. #12
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    Thank you for the valuable contributions... very much appreciated.

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

Similar Threads

  1. Simple table relationships ("faces" to "spaces" to "chairs")
    By skydivetom in forum Database Design
    Replies: 36
    Last Post: 07-20-2019, 01:49 PM
  2. Replies: 1
    Last Post: 01-18-2019, 06:32 AM
  3. Replies: 3
    Last Post: 06-06-2018, 08:26 PM
  4. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  5. Replies: 1
    Last Post: 10-08-2012, 09:01 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