以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]吐血,单行计算有数据整表计算就是0  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=121462)

--  作者:ajie5211
--  发布时间:2018/7/6 16:27:00
--  [求助]吐血,单行计算有数据整表计算就是0

下面这段代码,红色代码没有注释掉时(单行计算时),dr("往年后三个月月平均用量"), dr("往年当月总用量"), dr("往年当月和后两月总用量"),dr("近12月平均值"),这几个都能算到值,如果注释掉,就是整表计算时,这几个值就都是0,但dr("同族料号")单行计算和整表计算,值都是一样的。这是怎么会事啊,吐血了。

For Each dr As DataRow In DataTables("库存界线表").DataRows    
    If dr("料号") <> "SSD0350011" Then
        Continue For
    End If

    If dr("产品形态") = "金属零件"  OrElse dr("产品形态") = "五金原料" Then
        Dim lh As String = dr("料号")
        Dim cls As new List(of String)
        cls = DataTables("BOM及材料库存").SQLGetValues("F252","F257 = \'" & lh & "\' and F250 <> \'成品\'")  \'找出上级成品有几个料号
        Dim tzlhs As new List(of String)  \'定义同族料号集合
        Dim tzlhzds As new Dictionary(of String,Decimal) \'定义料号用量字典
        Dim yl As Decimal
        If cls.Count > 0 Then
            For Each cl As String In cls  \'上级成品料号加入同族集合
                Dim lss As new List(of String())
                lss = DataTables("BOM及材料库存").SQLGetValues("F257|F259|F255","F252 = \'" & cl & "\'") \'找出下级物料有几个料号
                If lss.Count = 1 Then
                    Dim lstzs As new List(of String)
                    lstzs = DataTables("BOM及材料库存").SQLGetValues("F252","F257 = \'" & cl & "\' and F250 <> \'成品\'")  \'找出上级成品有几个料号
                    If lstzs.Count < 1 Then
                        If tzlhs.Contains(cl) = False Then
                            tzlhs.Add(cl)
                            If tzlhzds.ContainsKey(lss(0)(0)) Then
                                yl = iif(yl > 0,lss(0)(1) / lss(0)(2) * tzlhzds(lss(0)(0)) * yl,lss(0)(1) / lss(0)(2) * tzlhzds(lss(0)(0)))
                            Else
                                yl = iif(yl > 0 ,lss(0)(1) / lss(0)(2) * yl, lss(0)(1) / lss(0)(2))
                            End If
                            tzlhzds.Add(cl,yl)
                        End If
                    Else
                        yl = iif(yl > 0 ,lss(0)(1) / lss(0)(2) * yl, lss(0)(1) / lss(0)(2))
                        Functions.Execute("同族顶层料号",tzlhs,lstzs,tzlhzds,yl)  \'找出成品料号的上级成品料号加入同族
                    End If
                End If
            Next
        End If
        If tzlhzds.Count > 0 Then
            Dim stz As String = Nothing
            Dim lskc As Decimal = 0
            Dim lskc1 As Decimal = 0
            Dim lskc2 As Decimal = 0
            Dim lskc3 As Decimal = 0
            For Each tzlh As String In tzlhzds.Keys
                stz = tzlh & "|" & stz
                Dim lsdr As DataRow = DataTables("库存界线表").SQLFind("料号 = \'" & tzlh & "\'")
                If lsdr IsNot Nothing Then
                    lskc = lskc + lsdr("往年后三个月月平均用量") * tzlhzds(tzlh)
                    lskc1 = lskc1 + lsdr("往年当月总用量") * tzlhzds(tzlh)
                    lskc2 = lskc2 + lsdr("往年当月和后两月总用量") * tzlhzds(tzlh)
                    lskc3 = lskc3 + lsdr("近12月平均值") * tzlhzds(tzlh)
                End If
            Next
            stz = IIF(stz IsNot Nothing, stz.TrimEnd("|"), Nothing)
            dr("同族料号") = IIF(stz IsNot Nothing, stz, Nothing)
            dr("往年后三个月月平均用量") = lskc
            dr("往年当月总用量") = lskc1
            dr("往年当月和后两月总用量") = lskc2
            dr("近12月平均值") = lskc3
        End If
    End If
Next


--  作者:ajie5211
--  发布时间:2018/7/6 16:37:00
--  

Dim yl As Decimal
这一行好像变成

Dim yl As new Decimal 就可以了,再跑跑试试。


--  作者:有点甜
--  发布时间:2018/7/6 17:28:00
--  

定义变量的时候,赋初值,养成习惯,如

 

Dim yl As Decimal = 0

 

如果还有问题,做个具体例子上来说明一下。