Foxtable(狐表)用户栏目专家坐堂 → 自动生成编码,有时可以,有时顺序码不对(不能最大顺序码)


  共有2187人关注过本帖树形打印复制链接

主题:自动生成编码,有时可以,有时顺序码不对(不能最大顺序码)

帅哥哟,离线,有人找我吗?
舞楼名角
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:84 积分:1129 威望:0 精华:0 注册:2014/11/13 16:20:00
自动生成编码,有时可以,有时顺序码不对(不能最大顺序码)  发帖心情 Post By: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

 回到顶部
帅哥哟,离线,有人找我吗?
有点色
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:13837 积分:69650 威望:0 精华:0 注册:2016/11/1 14:42:00
  发帖心情 Post By: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

 回到顶部
帅哥哟,离线,有人找我吗?
舞楼名角
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:84 积分:1129 威望:0 精华:0 注册:2014/11/13 16:20:00
  发帖心情 Post By:2017/4/25 16:39:00 [只看该作者]


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

 回到顶部
帅哥哟,离线,有人找我吗?
舞楼名角
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:84 积分:1129 威望:0 精华:0 注册:2014/11/13 16:20:00
  发帖心情 Post By: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


 回到顶部
帅哥哟,离线,有人找我吗?
有点色
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:13837 积分:69650 威望:0 精华:0 注册:2016/11/1 14:42:00
  发帖心情 Post By:2017/4/25 16:48:00 [只看该作者]

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

 

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


 回到顶部
帅哥哟,离线,有人找我吗?
舞楼名角
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:84 积分:1129 威望:0 精华:0 注册:2014/11/13 16:20:00
  发帖心情 Post By:2017/4/25 16:52:00 [只看该作者]

我的编码是HTZS53CQFZ170404007,我要判断HTZS(项目模式)+年份+顺序码(红色部分)来判断怎么递增。

 回到顶部
帅哥哟,离线,有人找我吗?
有点色
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:13837 积分:69650 威望:0 精华:0 注册:2016/11/1 14:42:00
  发帖心情 Post By: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

 


 回到顶部
帅哥哟,离线,有人找我吗?
舞楼名角
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:84 积分:1129 威望:0 精华:0 注册:2014/11/13 16:20:00
  发帖心情 Post By:2017/4/25 17:10:00 [只看该作者]

OK,谢谢,终于搞定这个编码问题了。

 回到顶部
帅哥哟,离线,有人找我吗?
有点色
  9楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:13837 积分:69650 威望:0 精华:0 注册:2016/11/1 14:42:00
  发帖心情 Post By:2017/4/25 17:12:00 [只看该作者]

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

 

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


 回到顶部