Error checking for NULL in VBScript

Vivian River picture Vivian River · Jan 24, 2013 · Viewed 129.3k times · Source

I have the following VBScript in a Classic ASP page:

function getMagicLink(fromWhere, provider)
    dim url 
    url = "magic.asp?fromwhere=" & fromWhere
    If Not provider is Nothing Then ' Error occurs here
        url = url & "&provider=" & provider 
    End if
    getMagicLink = "<a target='_blank' href='" & url & "'>" & number & "</a>"
end function

I keep getting an "Object Required" error messager on the line that says If Not provider Is Nothing Then.

Either the value is NULL, or it's not NULL, so why am I getting this error?

Edit: When I invoke the object, I pass in either NULL, or I pass in a string.

Answer

From your code, it looks like provider is a variant or some other variable, and not an object.

Is Nothing is for objects only, yet later you say it's a value that should either be NULL or NOT NULL, which would be handled by IsNull.

Try using:

If Not IsNull(provider) Then 
    url = url & "&provider=" & provider 
End if

Alternately, if that doesn't work, try:

If provider <> "" Then 
    url = url & "&provider=" & provider 
End if