Split first two words of string into new field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sharkiness
    New Member
    • Sep 2008
    • 19

    Split first two words of string into new field

    Morning All,

    I cannot find the answer to my question anywhere.

    I have an address field named 'ADDRESS'. I want to run an update query so that the first two words of the address are split into a new field named 'AddressSplit'.

    Therefore if the full address showed the following

    'Ruby Cottage Edinburgh Scotland'

    I would get 'Ruby Cottage' in new field.

    Likewise if the address was '5 Aberdeen Way Glasgow Scotland'

    I would get '5 Aberdeen' in the new field.

    Would be grateful of any help and would prefer a query sequence rather than VBA code.

    Thanks

    Sharkiness
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Personally, I feel as though a simple Function, coupled with an Update Query will easily do the trick.
    1. Function Definition:
      Code:
      Public Function fSplitAddress(strAddress As String) As String
      Dim varSplit As Variant
      
      varSplit = Split(strAddress, " ")
      
      'Just in case, 1 word Address
      If UBound(varSplit) = 0 Then Exit Function
      
      fSplitAddress = varSplit(0) & " " & varSplit(1)
      End Function
    2. UPDATE Query:
      Code:
      UPDATE tblAddress SET tblAddress.AddressSplit = fSplitAddress([ADDRESS])
      WHERE tblAddress.ADDRESS Is Not Null;
    3. Any questions, feel free to ask.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      If you can work on the assumption that each word is separated by a single space, then you could use :
      Code:
      AddressSplit: Left([Address],InStr(InStr(1,[Address],' ')+1,[Address],' ')-1)
      Unfortunately, most of the powerful functions (Split(), Join(), etc) can only be used within VBA as they require reference to arrays.

      This is only the version to display the value. You should manage converting that to an update without assistance.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        If you choose to use ADezii's approach (and there's no good reason not to unless performance is an issue across many hundreds of records) then you may want to adjust line #7 to :
        Code:
        If UBound(varSplit) = 0 Then
            fSplitAddress = strAddress
            Exit Function
        End If
        Currently, there appears to be no value set if there are no spaces found in the data. A simple oversight I expect.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Hello NeoPa, won't the Code in Post# 3 fail for 2-Component Addressess such as 2937 Clifford, #47 Sunset, etc.?

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            Only if you ignore the assumption posted before it (which is supported by the example data given). This is a good point to bring up though, nevertheless.

            If the assumption were that each word is separated by a single space, but optionally could include commas before some spaces (as ADezii suggests), then you could use :
            Code:
            AddressSplit: Replace(Left([Address],InStr(InStr(1,[Address],' ')+1,[Address],' ')-1),',','')
            That is certainly a more standard format for addresses, I must admit.

            A further point should be borne in mind. Such SQL will work from within Access, but such embedded function calls may cause trouble when accessing this query from outside of Access.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Originally posted by ADezii
              ADezii:
              Hello NeoPa, won't the Code in Post# 3 fail for 2-Component Addressess such as 2937 Clifford, #47 Sunset, etc.?
              I completely misunderstood your question. You mean for "2937 Clifford", "#47 Sunset", etc.

              The answer is Yes. It does (fail). Let me see if I can find a more successful set of SQL for this, which handles the good point ADezii raised.

              PS. Not only for two word data either. It also fails for a single word (It's quite cr*ppy really when I think about it :-D).
              Last edited by NeoPa; Dec 16 '10, 06:40 PM.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                It's getting a little clumsy now. That is sometimes the nature of working in SQL rather than VBA. If you're determined that it must be done outside of VBA though, the following should work :
                Code:
                AddressSplit: IIf((Len([Address])-Len(Replace([Address],' ','')))>1,[Address],Replace(Left([Address],InStr(InStr(1,[Address],' ')+1,[Address],' ')-1),',',''))
                Any comments are, as always, welcomed. I'd rather know if it's wrong than have everyone see I do cr*ppy answers ;-)

                Thanks are due to ADezii for spotting what I should have spotted before posting.

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  I guess that we are lucky enough to occasionally spot each other! (LOL).

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    It seems it's easier to spot each other's mistakes than our own eh, my friend?

                    I hate to post anything with mistakes in, but if I do, at least you spot them for me :-D

                    Comment

                    • Sharkiness
                      New Member
                      • Sep 2008
                      • 19

                      #11
                      Thanks guys, sorry for delay, only getting round to working on again now. Will get back to you if there are any problems. Although i doubt there will be

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32633

                        #12
                        No worries :-)

                        Let us know if you have any problems.

                        Comment

                        • Sharkiness
                          New Member
                          • Sep 2008
                          • 19

                          #13
                          Not sure if you will know answer but could you take a look at this thread for me:



                          Thanks

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32633

                            #14
                            I looked, but it's not something I do. I export datasets to Excel, but never data formatted by an object such as a form or report. I could never understand why anyone would want to myself, but it seems many do.

                            Comment

                            Working...