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?

Leif Neland
  • 1,416
  • 1
  • 17
  • 40
  • You tried `i = nulblank(rs("somefield") & "")`? – user692942 Aug 11 '16 at 11:14
  • http://stackoverflow.com/questions/30922176/what-is-the-asp-operator/30923091#30923091 http://stackoverflow.com/questions/38663205/asp-session-variables-is-same-as-isempty/38720449#38720449 – Zam Aug 11 '16 at 14:29
  • @zam doesn't even need the `IsNull()` check which isn't doing what the OP expects anyway *(as your answer details)*. – user692942 Aug 11 '16 at 18:05
  • `But what can be in a rs("somefield") which is not null, but cannot be trim()'ed?` should not be error if it's not null. – Zam Aug 11 '16 at 20:04
  • 1
    @zam Simple `Null` in VBScript and `ADODB` provider `Null` are not the same thing. Which is why `& ""` bypasses it as you are always passing a string. – user692942 Aug 12 '16 at 11:49
  • I've had `IsNumeric()` return False for a field that was specifically `Cast`-ed to `numeric`. As Lankymart said, database data types and ASP datatypes are not always similar/compatible enough. `& ""` really is your best friend when it comes to data type detection/conversion, because it gives you a dependable baseline - the only way it won't turn something into a string is if said something is an array. – Martha Aug 12 '16 at 17:56

1 Answers1

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
johna
  • 10,540
  • 14
  • 47
  • 72