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

Last reviewed: June 8, 1997
Article ID: Q117914
The information in this article applies to:
  • Microsoft Access version 2.0

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

REFERENCES

For additional information about parsing strings, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q99405
   TITLE     : ACC: Part 2 DDE in Visual Basic to Request Data from MS
               Access


Additional query words: parsing
Keywords : ExrStrg kbprg PgmParse
Version : 2.0
Platform : WINDOWS
Hardware : X86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.