2012-07-19 3 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) 

이 이중으로 값을 캐스팅합니다. 값이 null의 경우, 당신이 먼저 확인해야합니다, 그래서 당신은

... 오류 도중 얻을 것이다 또는 당신은 항상 올바른 유형 얻을 수있는 함수를 작성할 수 있습니다

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의 솔루션이 작동을하지만,

totalqty = totalqty + ToDbl(rs("QTY"), 0) 
을 :이
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 

은 그럼 그냥 전화 ... 간단합니다
관련 문제