以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  多层嵌套查找计算  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=84982)

--  作者:kaituozhe
--  发布时间:2016/5/14 16:02:00
--  多层嵌套查找计算
一个合同可能多次续签,续签后在此合同的新合同编号列写上新合同的编号,两个表,分别合同明细、合同合并,合同合并表中截止日期是续签后最后一份合同的截止日期,以下的代码是逐层查找并计算,如果续签次数超过10次、甚至以上代码就会很长,有无改进办法?

If e.DataCol.name = "合同编号" Then
    Dim dr As DataRow = DataTables("合同明细").find("合同编号 = \'" & e.DataRow("合同编号") & "\'")
    If dr IsNot Nothing Then
        If dr.Isnull("新合同编号") = True Then
            e.DataRow("截止日期") = dr("截止日期")
        Else
            Dim dr1 As DataRow = DataTables("合同明细").find("合同编号 = \'" & dr("合同编号") & "\'")
            If dr IsNot Nothing Then
                If dr1.Isnull("新合同编号") = True Then
                    e.DataRow("截止日期") = dr1("截止日期")
                Else
                    Dim dr2 As DataRow = DataTables("合同明细").find("合同编号 = \'" & dr1("合同编号") & "\'")
                    If dr2 IsNot Nothing Then
                        If dr2.Isnull("新合同编号") = True Then
                            e.DataRow("截止日期") = dr2("截止日期")
                        Else
                            Dim dr3 As DataRow = DataTables("合同明细").find("合同编号 = \'" & dr2("合同编号") & "\'")
                            If dr3 IsNot Nothing Then
                                If dr.Isnull("新合同编号") = True Then
                                    e.DataRow("截止日期") = dr3("截止日期")
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If
End If

--  作者:Hyphen
--  发布时间:2016/5/14 16:26:00
--  
建议增加一个冗余字段

如果合同明细之上还有主合同,那么就在主合同增加一个字段,记录最后的合同编号

也可以在合同明细增加一个字段,记录第一份合同的编号

--  作者:kaituozhe
--  发布时间:2016/5/14 21:37:00
--  
还有没有其他更好的办法了?
--  作者:Hyphen
--  发布时间:2016/5/15 13:56:00
--  
递归

定义递归函数名称GetDate,函数体

Dim dt As DataTable = Args(0)
Dim 合同编号 As String = Args(1)
Dim dr As DataRow = dt.find("合同编号 = \'" & 合同编号 & "\'")
If dr IsNot Nothing Then
    If dr.Isnull("新合同编号") Then
        Return dr("截止日期")
    Else
        Return Functions.Execute("GetDate",dt,dr("新合同编号") )
    End If
Else
    Return Nothing
End If

调用事件改为

If e.DataCol.name = "合同编号" Then
    Dim d = Functions.Execute("GetDate",DataTables("合同明细"),e.DataRow("合同编号"))
    If d IsNot Nothing Then e.DataRow("截止日期") = d
End If

--  作者:kaituozhe
--  发布时间:2016/8/3 23:22:00
--  
一个合同可能多次续签,续签后在此合同的新合同编号列写上新合同的编号,两个表,分别合同明细、合同合并,合同合并表中截止日期是续签后最后一份合同的截止日期,以下的代码是逐层查找并计算,如果续签次数超过10次、甚至以上代码就会很长,有无改进办法?

If e.DataCol.name = "合同编号" Then
    Dim dr As DataRow = DataTables("合同明细").find("合同编号 = \'" & e.DataRow("合同编号") & "\'")
    If dr IsNot Nothing Then
        If dr.Isnull("新合同编号") = True Then
            e.DataRow("截止日期") = dr("截止日期")
e.DataRow("合同金额") = dr("合同金额")
e.DataRow("发货金额") = dr("发货金额")
        Else
            Dim dr1 As DataRow = DataTables("合同明细").find("合同编号 = \'" & dr("合同编号") & "\'")
            If dr IsNot Nothing Then
                If dr1.Isnull("新合同编号") = True Then
                    e.DataRow("截止日期") = dr1("截止日期")
e.DataRow("合同金额") = dr1("合同金额")
e.DataRow("发货金额") = dr1("发货金额")
                Else
                    Dim dr2 As DataRow = DataTables("合同明细").find("合同编号 = \'" & dr1("合同编号") & "\'")
                    If dr2 IsNot Nothing Then
                        If dr2.Isnull("新合同编号") = True Then
                            e.DataRow("截止日期") = dr2("截止日期")
e.DataRow("合同金额") = dr2("合同金额")
e.DataRow("发货金额") = dr2("发货金额")
                        Else
                            Dim dr3 As DataRow = DataTables("合同明细").find("合同编号 = \'" & dr2("合同编号") & "\'")
                            If dr3 IsNot Nothing Then
                                If dr.Isnull("新合同编号") = True Then
                                    e.DataRow("截止日期") = dr3("截止日期")
e.DataRow("合同金额") = dr3("合同金额")
e.DataRow("发货金额") = dr3("发货金额")
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If
End If

--  作者:kaituozhe
--  发布时间:2016/8/3 23:23:00
--  
需要返回多个参数
--  作者:Hyphen
--  发布时间:2016/8/4 8:30:00
--  
参考4楼,用递归。不会就上传例子
--  作者:kaituozhe
--  发布时间:2016/8/4 11:56:00
--  

多个递归函数吗?代码就是6楼的


--  作者:大红袍
--  发布时间:2016/8/4 12:56:00
--  
 上传实例说明,你要做什么吧。