Results 1 to 5 of 5
  1. #1
    drinniol is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    2

    Separating text fields from the right hand side.


    I'm having trouble with separating a text field, starting from the right hand side.

    The field is [Br1Suburb], and the data includes the suburb, the state, and the postcode separated by spaces. The problem is the suburb can have one (or more) spaces in it, for example;
    Kingsley WA 6026
    Alexander Heights WA 6064

    I need to split the data as;

    AddressPostCode: All text after the rightmost space (6026, 6064)
    AddressState: All text between the second-rightmost space, and the rightmost space (WA)
    AddressSuburb: All text left of the second-rightmost space (Kingsley, Alexander Heights)

    I was able to do AddressPostCode easily enough:
    Code:
    Mid([Br1Suburb],InStrRev([Br1Suburb]," ")+1)
    However, I can't for the life of me figure out how to do the others. All the solutions I have tried either give an error, or return the text between the first- and second-leftmost space.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,421
    remove the postcode and surplus space at end

    replace([Br1Suburb],Mid([Br1Suburb],InStrRev([Br1Suburb]," "),""))

    now apply your instrrev again

    state=Mid(replace([Br1Suburb],Mid([Br1Suburb],InStrRev([Br1Suburb]," "))),InStrRev(replace([Br1Suburb],Mid([Br1Suburb],InStrRev([Br1Suburb]," ")))," ")+1)

    or if you already have your postcode

    postcode=Mid([Br1Suburb],InStrRev([Br1Suburb]," ")+1)

    remove the postcode and surplus space at end
    state=replace([Br1Suburb]," " & postcode,"")

    now use the mid function again
    state=Mid(state,InStrRev(state," ")+1)

    or in one line
    state=Mid(replace([Br1Suburb]," " & postcode,""),InStrRev(replace([Br1Suburb]," " & postcode,"")," ")+1)

    the above is aircode, so may have got brackets in the wrong place but you should get the idea

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,421
    there is another way using vba using the split function


    Code:
    function splitStr(s as string, r as string) as string
    'this function assumes there are at least 3 elements in the s string, if not the function will need additional error handling
    dim arr() as string
    dim i as integer
    
    arr=split(s," ")
    select case r
        case "postcode"
            splitStr=arr(ubound(arr))
        case "State"
            splitStr=arr(ubound(arr)-1)
        case "Address"
            for i=1 to ubound(arr)-2
                splitStr=splitStr & arr(i) & " "
            next i
        case else
            msgbox "return string not recognised"
    end select
    splitstr=trim(splitstr)
    end function
    put the above function in a module (not a form module)

    in a query you would call it like this

    State: splitStr(Br1Suburb,"State")



  4. #4
    drinniol is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    2
    Hi Ajax,

    Thanks for that, I think it's the right track. The State case works perfectly. However, the Address case is truncating the first word of the suburb, so it's either blank or returns only the second (and third, on some suburbs) word.

    Example - A suburb named The Vines is only returning Vines. A suburb names Munno Para West returns Para West.

    **EDIT***
    Solved it;

    Code:
    Case "Address"
            For i = 0 To UBound(arr) - 2
                splitStr = splitStr & arr(i) & " "
            Next I

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,421
    well caught!

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

Similar Threads

  1. Replies: 2
    Last Post: 07-25-2016, 01:12 PM
  2. Replies: 5
    Last Post: 12-29-2015, 12:30 PM
  3. Separating fields
    By markbitman in forum Queries
    Replies: 2
    Last Post: 07-31-2014, 09:05 AM
  4. Showing parameter fields side by side
    By sebgrb in forum Queries
    Replies: 2
    Last Post: 03-07-2014, 02:25 AM
  5. Side-by-Side Fields
    By DEG in forum Queries
    Replies: 1
    Last Post: 07-25-2011, 04:41 PM

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