Re: Type Mismatch ??? SOLVED: I think. by dm_dal
dm_dal
Fri Aug 01 12:44:05 CDT 2003
[ bob writes ]
> I'm still curious as to why you think the ISNUMERIC check is needed: the
> columns are decimal datatypes which do not allow Nulls - what could
possible
> be stored in there that is NOT numeric?
[ dave replies ]
Idunno.....call me a creature of habit. I alway check isNumeric before
trying to explicitly convert any expression to a/n Integer, Long, Single,
Double, Currency or Byte. Make it a habit and you never have to worry about
trying to type-cast a null value.
[ bob writes ]
> As for vbscript not handling the decimal data subtype, this isn't quite
the
> case. The issue is that IsNumeric cannot handle the the subtype. Isnumeric
> expects a string, or a value that can be implicitly converted to a string.
> It turns out that the decimal subtype does not seem to be implicitly
> convertible to a string: an explicit conversion is required. Note the
> difference between these two if statements:
[ dave replies ]
Actually, I dont think IsNumeric "expects" a sting. The doc's say it
expects ANY expression (Variant?). And, obviously the vbDecimal CAN
implicitly be converted to the subtype of string, since Trim() didn't throw
an exception and returned a string.
Personally, I think someone a MS messed up and forgot to upgrade all of
VBScripts functions and methods when the introduced the subtype vbDecimal.
They updated the VarType() function, and obviously Trim(), but forgot to
update the IsNumeric() function and that's what the problem is.
Dave
"Bob Barrows" <reb_01501@yahoo.com> wrote in message
news:%23%23hJqBFWDHA.1748@TK2MSFTNGP12.phx.gbl...
> I'm still curious as to why you think the ISNUMERIC check is needed: the
> columns are decimal datatypes which do not allow Nulls - what could
possible
> be stored in there that is NOT numeric?
>
> As for vbscript not handling the decimal data subtype, this isn't quite
the
> case. The issue is that IsNumeric cannot handle the the subtype. Isnumeric
> expects a string, or a value that can be implicitly converted to a string.
> It turns out that the decimal subtype does not seem to be implicitly
> convertible to a string: an explicit conversion is required. Note the
> difference between these two if statements:
>
> IF ISNUMERIC(MAX_F) THEN
> Response.Write "MAX_F was numeric"
> MAX_F = CCur(MAX_F)
> else
> Response.Write "MAX_F was not numeric"
> end if
>
> IF ISNUMERIC(cstr(MAX_F)) THEN
> Response.Write "MAX_F was numeric"
> MAX_F = CCur(MAX_F)
> else
> Response.Write "MAX_F was not numeric"
> end if
>
> Just to reiterate, the Isnumeric check is not necessary. Simply do this:
>
> If Not Rs.EOF Then
> LIFE_ADD_COVRG = Rs("LIFE_ADD_COVRG") ''' SqlServer type = char
> FLAT_AMOUNT = ccur(Rs("FLAT_AMOUNT")) ''' SqlServer type = decimal
> FACTOR_XSALARY = ccur(Rs("FACTOR_XSALARY")) ''' SqlServer type =
> decimal
> MAX_F = ccur(Rs("COVERAGE_MAXIMUM_FLAT")) ''' SqlServer type =
> decimal
> MIN_F = ccur(Rs("COVERAGE_MINIMUM_FLAT")) ''' SqlServer type =
> decimal
> MAX_X = ccur(Rs("COVERAGE_MAXIMUM_XFACTOR")) '''SqlServer type =
> decimal
> MIN_X = ccur(Rs("COVERAGE_MINIMUM_XFACTOR")) ''' SqlServer type =
> decimal
> End If
>
> HTH,
> Bob Barrows
>
> dm_dal wrote:
> > [ walter said ]
> >> I don't think that VBScript supports using a Decimal subtype in
> >> expressions. I think that you'll have to convert the variable in
> >> question to another subtype such as double, single currency or long
> >> before it will be usable in expressions.
> >
> > [ dave replies ]
> > I originally started to say that I tried this and still got the same
> > error, however, when I tried this, I did it a little differently.
> >
> > Example:
> > MAX_X = rs("MAXIMUM_COVERAGE_XFACTOR")
> >
> > Then I did a little check and some conversion:
> >
> > IF IsNumeric(MAX_X) THEN MAX_X = CDbl(MAX_X)
> >
> > Ok, with that said, and taking your point into consideration, this
> > caused me to wonder {{{ was the type casting really working? }}}
> >
> > Answer: NO
> > Since VBScript cannot handle the data type vbDecimal, then the
> > IsNumeric was retuning false and therefore was not casting it as a
> > double as expected.
> >
> > So, it's now working and here's the work around.
> > First, when I pull the values from the database, I use the Trim()
> > function to cast the resltant data as a string.
> >
> > MAX_X = Trim(rs("MAXIMUM_COVERAGE_XFACTOR")
> >
> > Then I do the IsNumeric test and type it as a double using CDbl() and
> > now it all works.
> >
> > Which still leaves me with the question though....If VBScript can't
> > handle vbDecimal types, then why would VarType() even be allowed to
> > return the type as a result? And secondly, if IsNumeric() actually
> > evaluates the expression to see if it can be returned as a numeric
> > type, then why does it return false if the value of the expression is
> > 1 or 0 or some other number? But....that for another day.
> >
> > Thank you Walter.....
> >
> > Dave
> >
> >
> > "Walter Zackery" <PleaseRespond@Newsgroup.com> wrote in message
> > news:OF0U7$DWDHA.532@TK2MSFTNGP10.phx.gbl...
> >> I don't think that VBScript supports using a Decimal subtype in
> >> expressions. I think that you'll have to convert the variable in
> >> question to another subtype such as double, single currency or long
> >> before it will be usable in expressions.
> >>
> >> "dm_dal" <dmy75252@yahoo.com> wrote in message
> >> news:OZphOODWDHA.2340@TK2MSFTNGP10.phx.gbl...
> >>> Bob,
> >>>
> >>> [bob asked]
> >>>> What does MAX_X contain if either MAX_F or MAX_X is NOT numeric??
> >>>> Null? A string? There's your type mismatch right there. You should
> >>>> put some "else" logic in these if statements ...
> >>>
> >>> [dave replies]
> >>> These are required fields (not nullable) in the db. In this
> >>> instance MAX_F = 0 and MAX_X = 1
> >>>
> >>> [bob asked]
> >>>> This makes no sense. On my machine, VarType(BASE_SALARY) returns
> >>>> 3, not 14. Why would 120000 cause the subtype to be vbDecimal? Is
> >>>> this a typo on your part?
> >>>
> >>> [dave replies]
> >>> My bad...it's a typo.
> >>> BASE_SALARY 's VarType returns as 3 (vbLong)
> >>> MAX_X 's VarType returns as 14 (vbDecimal)
> >>>
> >>> dave
> >>>
> >>> "Bob Barrows" <reb_01501@yahoo.com> wrote in message
> >>> news:uCAWZFDWDHA.548@tk2msftngp13.phx.gbl...
> >>>> OK, let's whittle this down to the relevant lines of code:
> >>>> dm_dal wrote:
> >>>>> Ok... so I didn't leap. Don't think I'm weak ( I was just to beat
> >>>>> down to open the window). However, I've got a bottle of bleach
> >>>>> ready to drink if I cant get this to work. Here the code:
> >>>>>
> >>>>> BASE_SALARY = 120000
> >>>> <snip>
> >>>>> MAX_X =
> >>>>> Rs("COVERAGE_MAXIMUM_XFACTOR") '''SqlServer type =
> >>>>> decimal <snip> IF ISNUMERIC(MAX_F) THEN MAX_F = CCur(MAX_F)
> >>>>> IF ISNUMERIC(MAX_X) THEN MAX_X = CCur(MAX_F)
> >>>>
> >>>> What does MAX_X contain if either MAX_F or MAX_X is NOT numeric??
> >>>> Null? A string? There's your type mismatch right there. You should
> >>>> put some "else" logic in these if statements ...
> >>>>
> >>>> IF ISNUMERIC(MAX_F) THEN
> >>>> MAX_F = CCur(MAX_F)
> >>>> ELSE
> >>>> MAX_F = CCur(0)
> >>>> END IF
> >>>>
> >>>>> ' DEBUGGING
> >>>>> T1 = VarType(MAX_X) ''' This returns type=3 (vbLong)
> >>>>> T2 = VarType(BASE_SALARY) ''' This returns type = 14 (vbDecimal)
> >>>>
> >>>> This makes no sense. On my machine, VarType(BASE_SALARY) returns
> >>>> 3, not 14. Why would 120000 cause the subtype to be vbDecimal? Is
> >>>> this a typo on your part?
> >>>>
> >>>>>
> >>>>> DBG_VAL = MAX_X * BASE_SALARY '''' Here's where the error is
> >>>>> thrown. I get a type mismatch error
> >>>>>
> >>>>> ....clip
> >>>>> End Function
> >>>>>
> >>>> HTH,
> >>>> Bob Barrows
>
>
>