Names are formatted as a string in [lastName, firstName middleInitial].
I have three text boxes that are labeled lastName firstName and middleInitial.
The code should separate that string into the three boxes but it gets hung up on:
-the first name, where it doesn't stop at the space or it cuts off a few characters
-the middle initial, where it works if they have a middle initial, but throws an error if it doesn't find one.
Private Sub cbxMbr_AfterUpdate()
If Not Me.opgMngRoster.Value = 1 Then
'Member's Last Name
Dim lastName As String
lastName = Left(cbxMbr.Text, (InStr(1, cbxMbr.Text, ",") - 1))
Me.txtLastName.Value = lastName
'Member's First Name
Dim firstName As String
firstName = Mid(cbxMbr.Text, InStr(1, cbxMbr.Text, " "), (Len(cbxMbr.Text) - InStr(1, cbxMbr.Text, " ")))
Me.txtFirstName.Value = firstName
'Member's Middle Initial
Dim midName As String
midName = Mid(cbxMbr.Text, InStr(InStr(1, cbxMbr.Text, " ") + 1, cbxMbr.Text, " "))
If midName = vbNullString Then
Me.txtMidInit.Value = " "
Else
Me.txtMidInit.Value = midName
'DoCmd.RunSQL "SELECT MEMBER ROSTER WHERE "
End If
End If
End Sub
You can use Split:
' "Duck, Donald D."
' "Duck, Donald"
Me!txtLastName.Value = Split(Me!cbxMbr.Value, ",")(0)
' Duck
Me!txtFirstName.Value = Split(Trim(Split(Me!cbxMbr.Value, ",")(1)), " ")(0)
' Donald
Me.txtMidtInit.Value = Split(Trim(Split(Me!cbxMbr.Value, ",")(1)) & " ", " ")(1)
' D. or zero-length string.