'---------------------------------------------------------------------------------------
' Procedure : ParseName
' Author : Jack
' Created : 2/23/2010
' Purpose : To parse a field containing the person's full name and to return
' the first name, or the initial if it exists, or last name depending on the
' value of strWhich.
'
' NOTE: The format of the fullname field is
' Lastname, Firstname Initial(may not be present)
' eg a)De Jesus, Charlene K.
' b)O'Sullivan, Margaret
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: strname == the person's fullname
' strWhich = F First Name
' = M Middle Initial
' = L Last Name
' Dependency: N/A
'------------------------------------------------------------------------------
'
Function ParseName(strName As String, strWhich As String) As String
Dim strUtil As String
Dim strLastname As String
Dim strFirstname As String
Dim strMiddle As String
On Error GoTo ParseName_Error
strUtil = Trim(strName)
strLastname = Left(strUtil, InStr(1, strUtil, ",") - 1)
strMiddle = Mid(strUtil, InStrRev(strUtil, " ") + 1)
If Len(strMiddle) <> 1 Then
strMiddle = vbNullString
Else
ParseName = strMiddle
strUtil = Mid(strUtil, 1, Len(strUtil) - 2)
End If
strFirstname = LTrim(Mid(strUtil, InStr(1, strUtil, ",") + 1))
Select Case strWhich
Case "F"
ParseName = strFirstname
Case "L"
ParseName = strLastname
Case "M"
ParseName = strMiddle
Case Else
ParseName = vbNullString
End Select
On Error GoTo 0
Exit Function
ParseName_Error:
MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure ParseName of Module Module4"
End Function