在MS Access中修剪名称

时间:2014-09-25 12:46:34

标签: ms-access ms-access-2007

我是MS Access的新手。我在访问的列中有这样的名字列表,需要修改下面的名称。

姓名:

  Clooney, George Timothy
  Willam Pitt, Brad
  Wilson, Larry

预期输出:

  Clooney, George
  Willam, Brad
  Wilson, Larry

我需要在逗号之前和之后有第一个单词。另外,我需要在逗号后面添加一个空格。

真的很感激建议。

1 个答案:

答案 0 :(得分:1)

您可以使用用户定义的功能实现此目的。这样的事情可能会有所帮助。将代码复制到标准模块,然后进行编译。

Public Function getFirstName(inputStr As String) As String
'********************
'Code Courtesy of
'  Paul Eugin
'********************
    Dim tmpArr() As String, retStr As String
    Dim iCtr As Integer, charPos As Integer

    'Strip the Input String to an Array.
    tmpArr = Split(inputStr, ",")

    'Loop through the Array
    For iCtr = 0 To UBound(tmpArr)
        'Find the position of the SPACE character
        charPos = InStr(Trim(tmpArr(iCtr)), " ")

        'If the position is not found, return the whole string
        If charPos = 0 Then charPos = Len(tmpArr(iCtr))

        'Append the result String
        retStr = retStr & Trim(Left(tmpArr(iCtr), charPos)) & ", "
    Next

    'Finally return the actual generated String
    getFirstName = Left(retStr, Len(retStr) - 2)
End Function 

要在即时窗口中使用它,请将其用作

? getFirstName("Wilson, Larry")
Wilson, Larry

? getFirstName("Clooney, George Timothy")
Clooney, George

这也可以在查询中使用。

SELECT 
    yourTableName.yourFieldName, 
    getFirstName(yourTableName.yourFieldName) As NewFieldName 
FROM
    yourTableName;