2016-02-22 16 views
8

Tôi đang sử dụng lệnh hình dạng ado trên báo cáo dữ liệu của tôi, nó hoạt động tốt nhưng khi hàm tổng hợp CALC của tôi (agrProfit/agrExtended * 100) là null hoặc 0/0 * 100 nó hiển thị lỗi chung và báo cáo dữ liệu không hiển thị. Hãy giúp tôi.VB6 CALC tập hợp trên ADO Shape khi trả về Null nhận được lỗi chung trong báo cáo dữ liệu

mRS.Open "SHAPE {select products.productid,products.productcode,isnull(products.description,descr) as description,isnull(vendor.description,'*** NOT FOUND ***') as groupdescription, " & _ 
    "isnull(sum(totalcost),0) as mTotalCost,isnull(sum(extended) - (sum(totalcost)),0) as mProfit, " & _ 
    "sum(charges) as mCharges,sum(discount) as mDiscounts, sum(retextended) as mReturns, " & _ 
    "reportuom, sum(totalcost) as mTotalCost, isnull(case when sum(extended) = 0 then 0 else (sum(extended) - (sum(totalcost)))/sum(extended)*100 end,0) as mgpm, sum(totalcost) as mTotalCost, case when sum(extended) = 0 then 0 else (sum(extended) - (sum(totalcost)))/sum(extended)*100 end as mgpm, sum(case when extended < 0 then (0 - (totalqty/products.reportqty)) else (totalqty/products.reportqty) end) as mTotalQty, isnull(sum(extended),0) as mExtended, sum(case when extended < 0 then (0 - (totalqty/products.reportqty)) else (totalqty/products.reportqty) end)/" & mTotalQty & " * 100 as mPercTotalQty, sum(extended)/" & mTotalExtended & " * 100 as mPercExtended " & _ 
    "From " & _ 
     "(select finishedsales.QtyReturned,finishedsales.productid,finishedsales.description as descr, finishedsales.averageunitcost* case when [return]=1 then convert(money,0-totalqty) else totalqty end as TotalCost,(chargeallowance * qty) + (chargeamountdiscounted * qty) as charges,(allowance * qty) + (amountdiscounted * qty)+ (extended-(extended * multiplier)) as discount,0 as rettotalqty, 0 as retextended,totalqty,round(extended * multiplier,4) as extended From finishedsales " & _ 
     " left join products on products.productid = finishedsales.productid " & _ 
     .gReportCriteria & _ 
     "Union All " & _ 
     "select finishedsales.QtyReturned, finishedsales.productid,finishedsales.description as descr,0 as totalcost,0 as charges,0 as discount,totalqty as rettotalqty ,abs(round(extended,4)) as retextended,0 as totalqty, 0 as extended From finishedsales " & _ 
      "left join products on products.productid = finishedsales.productid " & _ 
     Replace(UCase(.gReportCriteria & " and [RETURN] = 1"), "[RETURN] = 0", "[return] = 1") & _ 
    ") as finishedsales " & _ 
    "left join products on products.productid=finishedsales.productid " & _ 
    "left join vendor on products.vendorcode=vendor.vendorcode " & _ 
    "group by descr,products.productid,products.productcode,products.description,vendor.description,reportuom " & _ 
    "order by groupdescription, " & IIf(frmReportProducts.chkTop And fVal(frmReportProducts.txtTop) > 0, "finishedsales.mtotalqty desc,", "") & " products.description} AS Command1 COMPUTE Command1, SUM(Command1.mTotalQty) AS agrTotalQty, SUM(Command1.mExtended) AS agrExtended, SUM(Command1.mProfit) AS agrProfit, CALC(agrProfit/agrExtended*100) As agrGPM BY groupdescription", mcn 
+0

Đây có phải là trong MS Access không? –

+0

nope SQL Server – FatalError

Trả lời

2

Vì vậy, có vẻ như bạn đang sử dụng ADO Data Shaping functions tại đây và CALC(expression) cho phép bạn sử dụng các chức năng VBA được liệt kê here trong biểu thức. @ Đề nghị C-Pound Guru của gây ra một lỗi từ NULLIF() không phải là một hàm VBA, nhưng toàn bộ biểu thức có thể được viết lại như thế này:

CALC(IIF(IsNull(agrProfit), 0, IIF(agrProfit=0, 0, agrProfit/agrExtended) *100)) 

Hãy cho tôi biết nếu điều này sẽ chăm sóc của vấn đề của bạn.

+0

Nó đã làm việc cho một thiên tài của bạn !!! – FatalError

1

Nếu SQL Server của bạn là 2005 hoặc mới hơn, bạn có thể sử dụng NULLIF kết hợp với ISNULL:

Thay agrProfit/agrExtended với

ISNULL(agrProfit/NULLIF(agrExtended,0),0) 

này sẽ trả lại bằng không khi agrExtended = 0 chứ không phải gây ra một chia cho số không lỗi.

+0

CALC (ISNULL (agrProfit/NULLIF (agrExtended * 100,0), 0)) nó nói cột NULLIF được sử dụng trong biểu thức CALC nhưng không được định nghĩa trong các hàng – FatalError

+0

Calc không được tích hợp sẵn Hàm SQL (theo như tôi biết). Có lẽ bạn có thể cho thấy chức năng đó và chúng ta có thể thấy nếu chia cho số không có thể được xử lý ở đó ... –

0

Dường như bạn đang sử dụng MS Access hoặc một cái gì đó giao tiếp với MS Access. Nếu đúng như vậy, có thể bạn có thể sử dụng Switch:

Thay thế:

CALC(agrProfit/agrExtended * 100)

Với:

Switch(
    ISNULL(SUM(Command1.mExtended)), 0, 
    ISNULL(SUM(Command1.mProfit)), 0, 
    IIF(SUM(Command1.mExtended) = 0, 0, SUM(Command1.mProfit)/SUM(Command1.mExtended) * 100) 
    ) 

Ý tưởng là để thay thế NULL với 0, thay thế Divide bằng 0 với 0, hoặc trả về tỷ lệ thực tế.

Các vấn đề liên quan