An archived post has a function that seems to do what I need, which is to reverse and comma-separate names that are in a single field, except that in this case, some of the names have already been separated so do not need it again. Also, it seems to have a bug as it crashes on the line marked in red. Any ideas or does someone have a better, more up to date function? Thanks.
Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
Code:
Function ParseName(pFullName As String) As String
'**************************************************
'Purpose: Converts full name (e.g. Elmer T. Fudd)
' to lname, fname, mname format
'Coded by: raskew
'Calls: xLastInStr()
'Inputs: ? ParseName("Elmer T. Fudd")
'Output: Fudd, Elmer T.
'Note: As written, this function will correctly
' return no / multiple / initial only / no
' middle name(s) or initials.
' It will not, however, deal with prefix
' (e.g. Dr., Rev., etc) or suffix (e.g.
' Jr., Sr., III, etc.)
'**************************************************
Dim intF As Integer
Dim intL As Integer
Dim strHold As String
Dim strKeep As String
strHold = pFullName
intF = InStr(strHold, " ")
intL = xLastInStr(strHold, " ")
[COLOR=red]strKeep = Trim(Mid(strHold, intL)) & ", " & Trim(Left(strHold, intF))[/color]
strKeep = strKeep & IIf(intF <> intL, RTrim(Mid(strHold, intF, intL - intF)), "")
ParseName = strKeep
End Function
Function xLastInStr(pStr As String, _
pDelim As String) As Integer
Dim i As Integer
Dim n As Integer
Dim tlen As Integer
n = 0
tlen = Len(pDelim)
For i = Len(RTrim(pStr)) To 1 Step -1
If Mid(pStr, i, tlen) = pDelim Then
n = i
Exit For
End If
Next i
xLastInStr = n
End Function
Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases