math functions not working

Results 1 to 5 of 5

Thread: math functions not working

  1. #1
    Join Date
    Dec 1969
    Posts
    10

    Default math functions not working

    Can anyone tell me why the math functions in this code are not working. When I try to display any of the variables such as &#060;%= Unit1Total%&#062; in a form field it doesn&#039;t return a value.<BR>Thanks for the help.<BR><BR>&#060;%@ language="VBScript" %&#062;<BR>&#060;!-- #include file="adovbs.inc" --&#062;<BR>&#060;%<BR>Response.Buffer = True<BR><BR>Dim oConn<BR>Dim oRS, Req, Total, Unit1, Unit2, Unit3, Qty1, Qty2, Qty3, Unit1Total, Unit2Total, Unit3Total<BR><BR><BR>Req = Request.QueryString("ReqNum") <BR>set oConn=Server.CreateObject("ADODB.connection")<BR>s et oRS=Server.CreateObject("ADODB.recordset")<BR>oCon n.Open "DSN=a;UID=b;pwd=c"<BR>oRS.Open "Select * From purch_req WHERE REQNBR=" & Req & "", oConn, adOpenDynamic, adlockPessimistic, adCMDText<BR>On Error Resume Next<BR>Qty1 = oRS("REQUEST_QTY1")<BR>Qty2 = oRS("REQUEST_QTY2")<BR>Qty3 = oRS("REQUEST_QTY3")<BR>Unit1 = oRS("UNIT_COST1")<BR>Unit2 = oRS("UNIT_COST2")<BR>Unit3 = oRS("UNIT_COST3")<BR>Unit1Total = Unit1 * Qty1<BR>Unit2Total = Unit2 * Qty2<BR>Unit3Total = Unit3 * Qty3<BR>Total = Unit1Total + Unit2Total + Unit3Total<BR><BR>%&#062;<BR>

  2. #2
    Join Date
    Dec 1969
    Posts
    96,118

    Default Get rid of the interference...

    First of all, dump that silly ON ERROR RESUME NEXT.<BR><BR>If you are getting an error from *any* of that code you are showing, you sure as heck want to know about it!<BR><BR>*Now* tell us what happens.<BR><BR>

  3. #3
    Join Date
    Dec 1969
    Posts
    155

    Default RE: math functions not working

    Try converting the variables to double (or integer if quantyty suppose to be integer, w@ever Cdbl will work fine.) But first check that the variables are not NULL<BR><BR>if isNull(Qty1)=True Then<BR> Qty1 = 0<BR>End If<BR><BR>ect...<BR>.<BR>.<BR>.<BR><BR><BR>Unit1To tal = Cdbl(Unit1) * Cdbl(Qty1)<BR>Unit2Total = Cdbl(Unit2) * Cdbl(Qty2)<BR>Unit3Total = Cdbl(Unit3) * Cdbl(Qty3)<BR><BR>&#039;**now we can add because all are in the same format**<BR>Total = Unit1Total + Unit2Total + Unit3Total<BR><BR>&#039;**if you use on error resume next take advantage of it... use the Response.Write to get the error description....**<BR>Response.Write "&#060;h2&#062;err=" & Err.Description & "&#060;/h2&#062;"<BR>

  4. #4
    Join Date
    Dec 1969
    Posts
    96,118

    Default In theory, you are right...

    ...but as a practical matter, VBScript will automatically convert most anything to Double when you use the multiply operator. <BR><BR>When it sees a multiply, it first checks if both arguments are already integers. If so, it does integer multiplication. If not, then it does the internal equivalent of CDBL and, if both values are indeed convertible to double, does double multiplication. It gives a "type mismatch error" if either one can&#039;t be converted.<BR><BR>Since he has ON ERROR RESUME NEXT in place, I&#039;d bet it is because he was previously getting those mismatch errors. And rather than fixing the cause, he thought he could just hide the symptoms. Kind of like taking heavy duty cough syrup when you have pneuomonia. You&#039;re gonna die, but at least you won&#039;t cough as much.<BR><BR>


  5. #5
    Join Date
    Dec 1969
    Posts
    165

    Default RE: In theory, you are right...

    That&#039;s a great metaphor, Bill.<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •