extracting partial field value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trainer4063
    New Member
    • Jul 2007
    • 4

    #1

    extracting partial field value

    using access 2000 database. I have a need to extract a partial value in a field. for example I have a table with full names, for example "John Smith", "Jamie Johnson", and I need to be able to extract just the last name. Basically everything right of the last space.

    Help?
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    You have to understand that this will only work if the names are always entered in the format you gave, i.e. "Jamie Johnson."

    [CODE=vb] LastName = right(FullName, (len(FullName)-instr(FullName, " ")))[/CODE]

    Good Luck and Welcome to TheScripts!

    Linq ;0)>

    Comment

    • trainer4063
      New Member
      • Jul 2007
      • 4

      #3
      Thanks, worked great. but like you said, it only works if there is only one space in the field. If the field has a entry like "Jamie V Johnson", "V Johnson" shows. Is there a way to pull just "Johnson"?

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Try this ...

        [CODE=vb]
        Dim pos As Integer
        Dim newpos As Integer
        Dim lastname As String
        Dim rpt As Boolean

        pos = 1
        rpt = True

        Do Until rpt = False
        newpos = InStr(pos + 1, FullName, " ")
        If newpos <> 0 Then
        pos = newpos
        Else
        rpt = False
        End If
        Loop

        lastname = Right(FullName, Len(FullName) - pos)
        [/CODE]

        Comment

        Working...