I'm rusty and could use help, I have table where First Name, Last Name and Initial are all in one field. What is the syntax to remove initial? Been playing with Right and Len with no success, I'm sure this has been done before.
Public Function SplitReturn(ByVal str As String, _
ByVal delimiter As String, _
ByVal index As Integer) As String
SplitReturn = Split(str, delimiter)(index)
End Function
SELECT
SplitReturn(name, " ", 0) As [First Name],
SplitReturn(name, " ", 2) As [Last Name]
FROM People
Public Function ShortestWordLength(ByVal str As String) As Integer
Dim var_split As Variant
ShortestWordLength = 0
For Each var_split In Split(str, " ")
If Len(var_split) > ShortestWordLength Then: ShortestWordLength = Len(var_split)
Next
End Function
SELECT DISTINCT ShortestWordLength([name]) As [Shortest World Length]
FROM People
'---------------------------------------------------------------------------------------
' 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, Cheryl W.
' 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
10 On Error GoTo ParseName_Error
20 strUtil = Trim(strName)
30 strLastname = Left(strUtil, InStr(1, strUtil, ",") - 1)
40 strMiddle = Mid(strUtil, InStrRev(strUtil, " ") + 1)
50 If Len(strMiddle) <> 1 Then
60 strMiddle = vbNullString
70 Else
80 ParseName = strMiddle
90 strUtil = Mid(strUtil, 1, Len(strUtil) - 2)
100 End If
110 strFirstname = LTrim(Mid(strUtil, InStr(1, strUtil, ";") + 1))
120 Select Case strWhich
Case "F"
130 ParseName = strFirstname
140 Case "L"
150 ParseName = strLastname
160 Case "M"
170 ParseName = strMiddle
180 Case Else
190 ParseName = vbNullString
200 End Select
210 On Error GoTo 0
220 Exit Function
ParseName_Error:
230 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ParseName of Module Module4"
End Function
Sub jnames()
Dim db As DAO.Database
Dim rs As DAO.Recordset
10 Set db = CurrentDb
20 Set rs = db.OpenRecordset("TestNames")
30 Do While Not rs.EOF
40 Debug.Print rs!FullName & " -**- " & ParseName(rs!FullName, "F") & ParseName(rs!FullName, "M") & " " & Trim(ParseName(rs!FullName, "L"))
50 rs.MoveNext
60 Loop
End Sub
Public Function SplitReturn(ByVal str As String, _
ByVal delimiter As String, _
ByVal index As Integer) As String
Dim split_str() As String: split_str = Split(str, delimiter)
If UBound(split_str) >= index Then: SplitReturn = split_str(index)
End Function