以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  自动生成编码,有时可以,有时顺序码不对(不能最大顺序码)  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=96616)

--  作者:舞楼名角
--  发布时间:2017/2/24 12:11:00
--  自动生成编码,有时可以,有时顺序码不对(不能最大顺序码)
代码如下:

Select e.DataCol.Name
    Case "编码日期","项目模式","省份","城市代码","合同甲方"
        If e.DataRow.IsNull("编码日期") Or e.DataRow.IsNull("项目模式") Or e.DataRow.IsNull("城市代码") Or e.DataRow.IsNull("合同甲方") OrElse e.DataRow.IsNull("省份") Then
            e.DataRow("项目编号") = Nothing
        Else
            Dim d As Date = e.DataRow("编码日期")
            Dim y As Integer = d.Year
            Dim m As Integer = d.Month
            Dim dd As Integer = d.Day
            Dim Days As Integer = Date.DaysInMonth(y,m)
            Dim fd As Date = New Date(y,1,1) \'获得该月的一天
            Dim ld As Date = New Date(y+1,1,1) \'获得该月的最后一天
            Dim bh As String = e.DataRow("项目模式") & e.DataRow("省份") & e.DataRow("城市代码") & e.DataRow("合同甲方") & Format(d,"yyMMdd") \'生成编号的前缀
            If e.DataRow("项目编号").StartsWith(bh) = False \'如果单据编号前缀不符
                Dim max As String
                Dim idx As Integer
                Dim flt As String
                flt = "项目模式 = \'"& e.DataRow("项目模式") & "\' And 编码日期 >= #" & fd & "# And 编码日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")
                max = e.DataTable.Compute("Max(项目编号)",flt) \'取得该月的相同工程代码的最大单据编号
                If max > "" Then \'如果存在最大单据编号
                    idx = CInt(max.Substring(bh.length)) + 1 \'获得最大单据编号的后四位顺序号,并加1
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                e.DataRow("项目编号") = bh & Format(idx,"000")
            End If
        End If
End Select

--  作者:有点色
--  发布时间:2017/2/24 12:44:00
--  
Select e.DataCol.Name
    Case "编码日期","项目模式","省份","城市代码","合同甲方"
        If e.DataRow.IsNull("编码日期") Or e.DataRow.IsNull("项目模式") Or e.DataRow.IsNull("城市代码") Or e.DataRow.IsNull("合同甲方") OrElse e.DataRow.IsNull("省份") Then
            e.DataRow("项目编号") = Nothing
        Else
            Dim d As Date = e.DataRow("编码日期")
            Dim y As Integer = d.Year
            Dim m As Integer = d.Month
            Dim dd As Integer = d.Day
            Dim Days As Integer = Date.DaysInMonth(y,m)
            Dim fd As Date = New Date(y,1,1) \'获得该月的一天
            Dim ld As Date = New Date(y+1,1,1) \'获得该月的最后一天
            Dim bh As String = e.DataRow("项目模式") & e.DataRow("省份") & e.DataRow("城市代码") & e.DataRow("合同甲方")
           
            Dim max As String
            Dim idx As Integer
            Dim flt As String
            flt = "项目编号 like \'" & bh & "%\' And 编码日期 >= #" & fd & "# And 编码日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")
            bh &= Format(d,"yyMMdd") \'生成编号的前缀
            max = e.DataTable.Compute("Max(项目编号)",flt) \'取得该月的相同工程代码的最大单据编号
            If max > "" Then \'如果存在最大单据编号
                idx = CInt(max.Substring(bh.length)) + 1 \'获得最大单据编号的后四位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("项目编号") = bh & Format(idx,"000")
           
        End If
End Select

--  作者:舞楼名角
--  发布时间:2017/4/25 16:39:00
--  

[此贴子已经被作者于2017/4/25 16:47:40编辑过]

--  作者:舞楼名角
--  发布时间:2017/4/25 16:47:00
--  
我调整了代码,我是后来加了一列过渡编号,通过过渡编号来判断增加顺序号(顺序号主要是按年的),然后通过过渡编号来编项目编码,我发代码,帮我看看可不可以简化,我是觉得有些复杂(代码是可行的)。代码如下:
Select e.DataCol.Name
    Case "编码日期","项目模式","省份","城市代码","合同甲方"
        If e.DataRow.IsNull("编码日期") Or e.DataRow.IsNull("项目模式") Or e.DataRow.IsNull("城市代码") Or e.DataRow.IsNull("合同甲方") OrElse  e.DataRow.IsNull("省份") Then
            e.DataRow("过渡编号") = Nothing
        Else
            Dim d As Date = e.DataRow("编码日期")
            Dim y As Integer = d.Year
            Dim m As Integer = d.Month
            Dim dd As Integer = d.Day
            Dim Days As Integer = Date.DaysInMonth(y,m)
            Dim fd As Date = New Date(y,1,1) \'获得该年的第一天
            Dim ld As Date = New Date(y+1,1,1) \'获得该明的第一天
            Dim bh As String = e.DataRow("项目模式")
            Dim max As String
            Dim idx As Integer
            Dim flt As String
            flt = "过渡编号 like \'" & bh & "%\' And 编码日期 >= #" & fd & "# And 编码日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")
            bh &= Format(d,"yy") \'生成编号的前缀
            max = e.DataTable.Compute("Max(过渡编号)",flt) \'取得该年的相同项目模式的最大编号
            If max > "" Then \'如果存在最大单据编号
                idx = CInt(max.Substring(bh.length)) + 1 \'获得最大单据编号的后四位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("过渡编号") = bh & Format(idx,"000")
        End If
End Select
Select e.DataCol.Name
    Case "编码日期","项目模式","省份","城市代码","合同甲方","过渡编号"
        If e.DataRow.IsNull("编码日期") Or e.DataRow.IsNull("项目模式") Or e.DataRow.IsNull("城市代码") Or e.DataRow.IsNull("合同甲方") Or e.DataRow.IsNull("省份") OrElse e.DataRow.IsNull("过渡编号") Then
            e.DataRow("项目编号") = Nothing
        Else
            Dim d As Date = e.DataRow("编码日期")
            Dim flt2 As String = e.DataRow("过渡编号")
            Dim bb As String = e.DataRow("项目模式") & e.DataRow("省份") & e.DataRow("城市代码") & e.DataRow("合同甲方")
            e.DataRow("项目编号") = bb & Format(d,"yyMMdd") & flt2.SubString(6,3)
        End If
End Select


--  作者:有点色
--  发布时间:2017/4/25 16:48:00
--  

 为什么要加过度编号?直接判断项目编号不能得到结果?

 

 具体问题截图或者上传一个例子说明。


--  作者:舞楼名角
--  发布时间:2017/4/25 16:52:00
--  
我的编码是HTZS53CQFZ170404007,我要判断HTZS(项目模式)+年份+顺序码(红色部分)来判断怎么递增。
--  作者:有点色
--  发布时间:2017/4/25 17:04:00
--  

哦,那就必须记录一下,代码也无需优化了,合并一下即可

 

Select e.DataCol.Name
    Case "编码日期","项目模式","省份","城市代码","合同甲方"
        If e.DataRow.IsNull("编码日期") Or e.DataRow.IsNull("项目模式") Or e.DataRow.IsNull("城市代码") Or e.DataRow.IsNull("合同甲方") OrElse  e.DataRow.IsNull("省份") Then
            e.DataRow("过渡编号") = Nothing
        Else
            Dim d As Date = e.DataRow("编码日期")
            Dim y As Integer = d.Year
            Dim m As Integer = d.Month
            Dim dd As Integer = d.Day
            Dim Days As Integer = Date.DaysInMonth(y,m)
            Dim fd As Date = New Date(y,1,1) \'获得该年的第一天
            Dim ld As Date = New Date(y+1,1,1) \'获得该明的第一天
            Dim bh As String = e.DataRow("项目模式")
            Dim max As String
            Dim idx As Integer
            Dim flt As String
            flt = "过渡编号 like \'" & bh & "%\' And 编码日期 >= #" & fd & "# And 编码日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")
            bh &= Format(d,"yy") \'生成编号的前缀
            max = e.DataTable.Compute("Max(过渡编号)",flt) \'取得该年的相同项目模式的最大编号
            If max > "" Then \'如果存在最大单据编号
                idx = CInt(max.Substring(bh.length)) + 1 \'获得最大单据编号的后四位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("过渡编号") = bh & Format(idx,"000")
            Dim flt2 As String = e.DataRow("过渡编号")
            Dim bb As String = e.DataRow("项目模式") & e.DataRow("省份") & e.DataRow("城市代码") & e.DataRow("合同甲方")
            e.DataRow("项目编号") = bb & Format(d,"yyMMdd") & flt2.SubString(6,3)
        End If
End Select

 


--  作者:舞楼名角
--  发布时间:2017/4/25 17:10:00
--  
OK,谢谢,终于搞定这个编码问题了。
--  作者:有点色
--  发布时间:2017/4/25 17:12:00
--  

 你不添加过渡列也可以,查找的时候,你截取对应位置来比较就好了。比如 substring(项目编号, 0, 本行的项目模式字符的长度) = \'本行的项目模式字符\' and substring(项目编号, 年份的位置, 2) = \'本行年份\'

 

 根据这样的条件,同样可以查询到对应的顺序号的。