2012-07-19 43 views
2

是否可以編輯從記錄集抓取的數據?在我的情況下,我試圖將數量加在一起,以便我可以得到總數。那麼什麼,我試圖做一個例子是:編輯從記錄集抓取的數據

<% 
    set rs = server.CreateObject("ADODB.recordset") 
    totalqty = 0 

    do NOT while rs.EOF 
     totalqty = totalqty + rs("QTY") 
    loop 
>% 

每當我試圖做這樣的事情,我總是得到一個「類型不匹配」的錯誤,我不知道如何解決這個問題。

一如既往,任何和所有的幫助將不勝感激。

回答

1

嘗試在記錄「中投」的值,像這樣:

CDbl(rs.fields("QTY").value) 

這將值轉換爲雙。如果該值爲空,你會得到錯誤的連接,所以你必須檢查第一...

或者,您可以編寫一個函數來始終得到正確的類型:

public function parse(value, alternative) 
    dim val 
    val = trim(value & "") 
    parse = alternative 
    if val = "" then exit function 
    on error resume next 
    select case varType(parse) 
     case 2, 3 'integer, long 
      parse = cLng(val) 
     case 4, 5 'single, double 
      parse = cdbl(val) 
     case 6 'currency 
      parse = ccur(val) 
     case 7 'date 
      parse = cDate(val) 
     case 11 'bool 
      parse = cBool(val) 
     case 8 'string 
      parse = value & "" 
     case else 
      on error goto 0 
      lib.throwError("type not supported. val:" & value & " alt:" & alternative) 
    end select 
    on error goto 0 
end function 

dim val : val = rs("QTY") 
val = parse(val, 0) 

' now val is always an integer (either the value from db or 0) 
0

ulluoink的解決方案將工作,但這是簡單的...

function ToDbl(vIn, nDefault) 
    'Convert a variant to an integer using default where necessary 
    if isnull(vIn) then 
     ToDbl = nDefault 
    else 
     if IsNumeric(CStr(vIn)) Then 
      ToDbl = CDbl(vIn) 
     else 
      ToDbl = nDefault 
     end if 
    end if 
end function 

然後只要致電:

totalqty = totalqty + ToDbl(rs("QTY"), 0)