Standard Name Formatting Routine

Names of people come in many separate parts, some of which can be not present or not known. The hassle begins if you are dealing with a storage system (database or otherwise) where the parts are stored separately. You are now faced with a formidable task of putting it all together and making sure that correct formatting is used.

A common mistake will format a person whose middle initial is not known as:

John . Doe instead of John Doe

However making it right requires some effort and there is no need to repeat it for every time. That's why I created the following functions:

public Function FormatName(firstname as string, _
                           lastname as string, _
                           optional mi as string, _
                           optional title as string, _
                           optional Suffix as string) as string

    If len(Trim$(title)) > 0 then
        FormatName = StrConv(title, vbProperCase)
        If Right$(FormatName, 1) <> "." then FormatName = _
           FormatName & "."
        FormatName = FormatName & " "
    End If

    If len(Trim$(firstname)) > 0 then
        FormatName = FormatName & StrConv(firstname, _
                     vbProperCase) & " "
    End If

    If len(Trim$(mi)) > 0 then
        FormatName = FormatName & StrConv(mi, vbProperCase)
        If Right$(FormatName, 1) <> "." then FormatName = _
           FormatName & "."
        FormatName = FormatName & " "
    End If

    If len(Trim$(lastname)) > 0 then
        FormatName = FormatName & StrConv(lastname, vbProperCase) _
                     & " "
    End If

    If len(Trim$(Suffix)) > 0 then
        FormatName = Trim$(FormatName) & ", " & StrConv(Suffix, _
                     vbProperCase)
    End If

    FormatName = Trim$(FormatName)

End Function

This function is similar to the one above, except that it puts the lastname first.

public Function FormatNameReverse(firstname as string, _
       lastname as string, _
       optional mi as string) as string

    FormatNameReverse = StrConv(lastname, vbProperCase)

    If len(Trim$(firstname)) > 0 Or len(Trim$(mi)) > 0 then
        FormatNameReverse = FormatNameReverse & ","
    End If

    If len(Trim$(firstname)) > 0 then
        FormatNameReverse = FormatNameReverse & " " & _
                            Trim$(StrConv(firstname, vbProperCase))
    End If

    If len(Trim$(mi)) > 0 then
        FormatNameReverse = FormatNameReverse & " " & _
                            Trim$(StrConv(Left$(mi, 1), _
                            vbProperCase)) & "."
    End If

    FormatNameReverse = Trim$(FormatNameReverse)

End Function


Comments

  • Program

    Posted by Legacy on 12/11/2003 12:00am

    Originally posted by: Grea

    how can you check if there is a comma between the wholename then swap the name?  eg: Doe, Joe to Joe Doe
    
    or Joe (doe) to Joe (doe)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds