ifnull not enough for testing for numerical rs

0

I have a function, which is supposed to return zero, if the input cannot be converted to an integer.

But sometimes it fails, if the field from a resultset is not a proper value, whatever it is.

Function nulblank(str)
    dim val
    if IsNull(str) then
        val=0
    else
        str = trim(str)
        if isNumeric(str) then
            val = cDbl(str)
        else
            val = 0
        end if
    end if
    nulblank = val
end function

I get an error 0x80020009 on str = trim(str)

This function is only called on

set rs = conn.execute(sql)
i = nulblank(rs("somefield"))

How can I make this function "failsafe", so it never dies, but returns 0 on "bad" values?

I guess I could do on error resume next and if Err.Number <> 0 then something.

But what can be in a rs("somefield") which is not null, but cannot be trim()'ed?

asp-classic
asked on Stack Overflow Aug 11, 2016 by Leif Neland

1 Answer

0

That error usually relates to an empty recordset.

You should check that the recordset has a row before attempting to retrieve a column value, eg:

set rs = conn.execute(sql)
if not rs.eof then
    i = nulblank(rs("somefield"))
end if
answered on Stack Overflow Aug 15, 2016 by johna

User contributions licensed under CC BY-SA 3.0