首先关闭apoligies,我对VBA for Access中的编码完全不熟悉,因此可能存在很多错误。但无论如何,我在第一个名字和中间名词时遇到了一些问题。目前,名称被格式化为[lastName,firstName middleInitial]中的字符串我有3个文本框,标注为lastName firstName和middleInitial。下面的代码应该做的是将该字符串分成三个适当的框,但它挂在第一个名字上,它不知道如何停留在空间或者它切断了几个字符,中间初始它只有在有中间的初始值时才有效,但如果找不到则会抛出错误。我一直在试图弄清楚我的头发,因此我已经到了一个指导,我在这里问。
非常感谢!
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
答案 0 :(得分:2)
您可以使用拆分:
' "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.