Results 1 to 6 of 6
  1. #1
    Fais is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2014
    Posts
    7

    Angry Extract part of string

    Hello, i have the following scenario. I want to extract part of text from a string in Access 2010.

    i have the following field:

    column1:
    1234-Main st-1234567890-11218-US-
    4321-Not main st-NYC-1234567890-11214-US-



    I want the extract '1234567890' from both of the fields above, as you can see it some times falls after the 2nd '-' and sometimes after the 3rd '-', i want to write a logic that if there are 5 '-' total then look after 2nd '-' and if there are 6 '-' total then look after 3rd '-' and extract the number before the next dash. Please assist, thanks.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Personally I would create a function that accepted the field as an input and returned the desired portion. You can use the Split() function in the function. The UBound function will tell you how many parts there are, and you can grab the desired portion from there.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    I don't quite understand the criteria for the text required.
    ... i want to write a logic that if there are 5 '-' total then look after 2nd '-' and if there are 6 '-' total then look after 3rd '-' ....
    In the second field the required data is after the third "-" but both fields only have 5 '-'s
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    Fais is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2014
    Posts
    7
    My apologies, i have edited the post.

  5. #5
    Fais is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2014
    Posts
    7
    Hi,
    thanks for the reply. I would appreciate it if you can give me some sort of a syntax because i am fairly new to Access. thanks in advance.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Something like:

    Function GetPart(strInput As String) As String
    Dim strAry As Variant
    strAry = Split(strInput,"-")
    GetPart = strAry(IIf(UBound(strAry)=5,2,3))
    End Function

    Call function from query:

    GetPart([fieldname])

    This code assumes every record has a value in the field.
    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.

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

Similar Threads

  1. Extract a number from a string
    By webisti in forum Access
    Replies: 3
    Last Post: 09-16-2013, 08:29 AM
  2. Replies: 8
    Last Post: 11-08-2012, 07:08 AM
  3. Replies: 8
    Last Post: 09-24-2012, 12:34 PM
  4. Extract Value from Variable in String
    By nguyenak in forum Programming
    Replies: 3
    Last Post: 05-24-2012, 03:50 PM
  5. Extract String From Between 2 Values
    By kathleencampbell in forum Queries
    Replies: 5
    Last Post: 03-23-2012, 10:52 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