为什么我不能比较vba字符串类型和SQL短文本类型?

时间:2016-08-28 10:08:49

标签: sql vba ms-access

我有一个带有短文本字段的表和一个返回字符串类型的函数。当我将字段与函数a的返回值进行比较时,得到以下错误:

条件表达式上的数据类型不匹配。

表格

enter image description here

查询

UPDATE myTable
SET clean = ""
WHERE trimWebAddress(webAddress) in (
SELECT domain FROM genericDomains)

功能

Public Function trimWebAddress(ByVal address As String) As String
    Dim cleanAddress As String

    If IsNull(address) = False Then
        cleanAddress = address
        If InStr(1, cleanAddress, "no_domain_available") = 1 Then
            cleanAddress = ""
        ElseIf InStr(1, cleanAddress, "https://") = 1 Then
            cleanAddress = Replace(cleanAddress, "https://", "")
        Else
            cleanAddress = Replace(cleanAddress, "http://", "")
        End If
        If InStr(1, cleanAddress, "www.") = 1 Then
            cleanAddress = Replace(cleanAddress, "www.", "")
        End If
        If InStr(1, cleanAddress, "/") > 0 Then
            cleanAddress = Left(cleanAddress, InStr(1, cleanAddress, "/") - 1)
        End If
    Else
        cleanAddress = ""
    End If
    trimWebAddress = cleanAddress
End Function

1 个答案:

答案 0 :(得分:2)

您的数据中可能包含 Null 值。

你可以像这样修改:

Public Function trimWebAddress(ByVal address As Variant) As String

    Dim cleanAddress As String

    If Nz(address) <> "" Then
        cleanAddress = address
        If InStr(1, cleanAddress, "no_domain_available") = 1 Then
            cleanAddress = ""
        ElseIf InStr(1, cleanAddress, "https://") = 1 Then
            cleanAddress = Replace(cleanAddress, "https://", "")
        Else
            cleanAddress = Replace(cleanAddress, "http://", "")
        End If
        If InStr(1, cleanAddress, "www.") = 1 Then
            cleanAddress = Replace(cleanAddress, "www.", "")
        End If
        If InStr(1, cleanAddress, "/") > 0 Then
            cleanAddress = Left(cleanAddress, InStr(1, cleanAddress, "/") - 1)
        End If
    End If

    ' Prevent zero-length output.
    If cleanAddress = "" Then
        cleanAddress = "NotToBeFound"
    End If

    trimWebAddress = cleanAddress

End Function

如果不允许零长度字符串,则可能必须更新为Null:

UPDATE myTable
SET clean = Null
WHERE trimWebAddress(webAddress) in (
SELECT domain FROM genericDomains)