Microsoft KB Archive/117914

= ACC2: Sample Function to Obtain the Last Word in a String =

Article ID: 117914

Article Last Modified on 11/6/2000

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q117914



SUMMARY
This article demonstrates a sample user-defined Access Basic function that you can use to return the last word in any text string.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Building Applications" manual.



MORE INFORMATION
You can use the LastWord function to return the last word in a string. For example, the LastWord function will return "Doe" from the string "Mr. and Mrs. John Doe."

You can also use the LastWord function in your application to sort a list of names by last name, if the first and last names are in the same field, or if the middle names are included with the first names in a field.

The LastWord Function
Function LastWord (anystring) If Not InStr(anystring, Chr(32)) = 0 Then Dim FindSpace As String, Pos As Integer, StrLen As Integer FindSpace = "Z" StrLen = Len(anystring) Pos = StrLen Do Until FindSpace = Chr(32) Pos = Pos - 1 FindSpace = Mid(anystring, Pos, 1) Loop LastWord = Mid(anystring, Pos + 1, StrLen - Pos + 1) End If  End Function

