0

我有一个辅助函数,它应该连接名为IBM Notes的 Access 表中的Notes First NameNotes Middle NameNotes Surname字段。

助手本身工作正常,但是如果我尝试在上述字段之一具有空条目的行上调用它,则不会调用助手。

无论用作参数的字段是否为空,如何修改用于调用帮助程序的 SQL 以确保它返回结果?

这是用于调用助手的 SQL(DB设置为CurrentDb) -

DB.Execute "UPDATE [IBM Notes] SET [Notes Full Name] = NotesFullName([Notes First Name], [Notes Middle Name], [Notes Surname])"

这是助手本身 -

Function NotesFullName(Optional ByVal firstName As String = "", _
                       Optional ByVal middleName As String = "", _
                       Optional ByVal surname As String = "") As String

    NotesFullName = Trim( _
        IIf(Not firstName = "", firstName & " ", "") & _
        IIf(Not middleName = "", middleName & " ", "") & _
        IIf(Not surname = "", surname, "") _
    )

End Function
4

2 回答 2

1

那是因为空字段是Null。尝试这个:

Function NotesFullName(Optional ByVal firstName As Variant = Null, _
                       Optional ByVal middleName As Variant = Null, _
                       Optional ByVal surName As Variant = Null) As Variant

    If IsNull(firstName & middleName & surName) Then
        NotesFullName = Null
    Else
        NotesFullName = Trim( _
            IIf(Not IsNull(firstName), firstName & " ", "") & _
            IIf(Not IsNull(middleName), middleName & " ", "") & _
            IIf(Not IsNull(surName), surName, "") _
        )
    End If

End Function
于 2016-03-10T11:49:13.020 回答
1

这个查询会在不需要辅助函数的情况下做同样的事情吗?

UPDATE [IBM Notes] 
SET [IBM Notes].[Notes Full Name] = 
    TRIM(Replace(Nz([Notes First Name],"") & " " & 
    Nz([Notes Middle Name],"") & " " & 
    Nz([Notes Surname],""),"  "," "))

注意: 替换功能是用一个空格替换一个双空格。

编辑:在这种情况下NZ,用空字符串替换 Null 字段。

于 2016-03-10T11:59:40.930 回答