Title - (如果第一個字符合標準 Title 的清單)
Degree - (第一個逗號後的項目)
Pedigree - (如果最後一個字符合標準 Pedigree 清單)
Last Name - (不可包含空格,但可以有連字符號)
First Name - (不可包含空格)
Middle Initial(s) - (剩下的項目)
Function CutLastWord (ByVal S As String, Remainder As String) _
As String
' CutLastWord: returns the last word in S.
' Remainder: returns the rest.
'
' Words are separated by spaces
'
Dim I As Integer, P As Integer
S = Trim$(S)
P = 1
For I = Len(S) To 1 Step -1
If Mid$(S, I, 1) = " " Then
P = I + 1
Exit For
End If
Next I
If P = 1 Then
CutLastWord = S
Remainder = ""
Else
CutLastWord = Mid$(S, P)
Remainder = Trim$(Left$(S, P - 1))
End If
End Function
Function CutWord (ByVal S As String, Remainder As String) As String
'
' CutWord: returns the first word in S.
' Remainder: returns the rest.
'
Dim P As Integer
S = Trim$(S)
P = InStr(S, " ")
If P = 0 Then P = Len(S) + 1
CutWord = Left$(S, P - 1)
Remainder = Trim$(Mid$(S, P + 1))
End Function
Sub ParseName (ByVal S As String, Title As String, FName As String, _
MName As String, LName As String, _
Pedigree As String, Degree As String)
Dim Word As String, P As Integer, Found As Integer
Const Titles = "Mr.Mrs.Ms.Dr.Miss,Sir,Madam,Mayor,President"
Const Pedigrees = "Jr.Sr.III,IV,VIII,IX,XIII"
Title = ""
FName = ""
MName = ""
LName = ""
Pedigree = ""
Degree = ""
'
' Get Title
'
Word = CutWord(S, S)
If InStr(Titles, Word) Then
Title = Word
Else
S = Word & " " & S
End If
'
' Get Degree
'
P = InStr(S, ",")
If P > 0 Then
Degree = Trim$(Mid$(S, P + 1))
S = Trim$(Left$(S, P - 1))
End If
'
' Get Pedigree
'
Word = CutLastWord(S, S)
If InStr(Pedigrees, Word) Then
Pedigree = Word
Else
S = S & " " & Word
End If
'
' Get the rest
'
LName = CutLastWord(S, S) ' Last Name
FName = CutWord(S, S) ' First Name
MName = Trim(S) ' Initials/Middle Name(s)
End Sub
Sub Command1_Click()
Dim Title As String, FName As String, MI As String
Dim LName As String, Pedigree As String, Degree As String
ParseName txtName, Title, FName, MI, LName, Pedigree, Degree
txtTitle = Title
txtFirstName = FName
txtMI = MI
txtLastName = LName
txtPedigree = Pedigree
txtDegree = Degree
End Sub