我有一个名为PATRN_NAME的字段的表格,其中设置了First_Name,Last_Name M.I。
示例:
史密斯,詹姆斯M
琼斯,克里斯J。
我正在尝试将该字段拆分为FIRST_NAME,LAST_NAME和MI字段。我刚刚问了一个关于此的问题,有人帮助我使用Split()来获取LAST_NAME字段。但是,当我尝试为FIRST_NAME使用Split()函数时,它不起作用,因为该字段的记录不符合字段的名称约定,而是如下:“Town Library - GW”或“Donation from纽约市“。
当我的代码遇到这些类型的名称时,它会在我使用的行上抛出错误“下标超出范围”!FIRST_NAME =拆分(修剪(拆分(rst!PATRN_NAME,“,”)(1)) ,“”)(0)。如何使我的代码仅运行在大多数字段的标准名称约定之后的数据上?
Function Change_Name()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Active Patrons", dbOpenDynaset)
rst.MoveFirst
Do While Not rst.EOF
rst.Edit
rst!LAST_NAME = Split(rst!PATRN_NAME, ",")(0)
rst!FIRST_NAME = Split(Trim(Split(rst!PATRN_NAME, ",")(1)), " ")(0)
rst.Update
rst.MoveNext
Loop
End Function
答案 0 :(得分:1)
你有两个分裂:一个用于逗号;另一个空间。因此,声明两个字符串数组来保存这些拆分的结果。
Dim astrComma() As String
Dim astrSpace() As String
然后我认为在循环中使用这些数组会更简单。
rst.Edit
astrComma = Split(rst!PATRN_NAME, ",")
If UBound(astrComma) > 0 Then
' this means PATRN_NAME contains at least one comma,
' so assume LAST_NAME is everything before first comma
rst!LAST_NAME = astrComma(0)
' expect FIRST_NAME present in second member of astrComma
astrSpace = Split(Trim(astrComma(1)), " ")
Else
MsgBox "no LAST_NAME in " & rst!PATRN_NAME
End If
If UBound(astrSpace) >= 0 Then
' you may also want to check whether this is an empty
' string before you store it; does the field allow
' empty strings?
rst!FIRST_NAME = astrSpace(0)
Else
MsgBox "no FIRST_NAME in " & rst!PATRN_NAME
End If
rst.Update
答案 1 :(得分:0)
如果字段中没有逗号,则Last_name将等于Patrn_name 所以
rst!last = split()....
if rst!lasst<> rst!patrn_name)然后rst!first = split()....