以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  年份自动编号求助  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=84495)

--  作者:李睿涵
--  发布时间:2016/5/3 19:00:00
--  年份自动编号求助
想对年份做自动编号 但编号里要体现月份
比如: 16030001   16040002   16040003
下面是我改编帮助的代码,但实际运行后发现,编号不太智能
比如我先录入了大月份的(05月),生成编号:16050001,
然后录入小月份的,如:04-11,编号为16040002
再选择一个小月份的,如04-12,编号还是16040002
请高手能完善一下代码

If e.DataCol.Name = "接单日期" Then
    If 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 Days As Integer = Date.DaysInMonth(y,m)
        Dim fd As Date = New Date(y,1,1) \'获得该月的第一天
        Dim ld As Date = New Date(y,12,31) \'获得该月的最后一天
        Dim bh As String = Format(d,"yyMM") \'生成编号的前6位,4位年,2位月.
        If e.DataRow("工单编号").StartsWith(bh) = False \'如果编号的前4位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(工单编号)","接单日期 >= #" & fd & "# And 接单日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该月的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(4,4)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("工单编号") = bh & Format(idx,"0000")
        End If
    End If
End If


--  作者:大红袍
--  发布时间:2016/5/3 20:57:00
--  
If e.DataCol.Name = "接单日期" Then
    If 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 fd As Date = New Date(y,m,1) \'获得该月的第一天
        Dim ld As Date = fd.AddMonths(1)
        Dim bh As String = Format(d,"yyMM") \'生成编号的前6位,4位年,2位月.
        If e.DataRow("工单编号").StartsWith(bh) = False \'如果编号的前4位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(工单编号)","接单日期 >= #" & fd & "# And 接单日期 < #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该月的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(bh.length)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("工单编号") = bh & Format(idx,"0000")
        End If
    End If
End If

--  作者:李睿涵
--  发布时间:2016/5/3 22:28:00
--  

不是我要的效果

如下图,假设是按顺序往下录入数据

只判断是否为同一年份的,不考虑月份(但编号里要体现)

可能会先录入晚些的日期,后录入早些的日期

编号按照相同年份做流水递增

 

 


图片点击可在新窗口打开查看此主题相关图片如下:希望效果.png
图片点击可在新窗口打开查看
[此贴子已经被作者于2016/5/3 22:28:41编辑过]

--  作者:大红袍
--  发布时间:2016/5/3 22:56:00
--  

那就这样做

 

If e.DataCol.Name = "接单日期" Then
    If 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 fd As Date = New Date(y,m,1) \'获得该月的第一天
        Dim ld As Date = fd.AddYears(1)
        Dim bh As String = Format(d,"yy") \'生成编号的前6位,4位年,2位月.
        If e.DataRow("工单编号").StartsWith(bh) = False \'如果编号的前4位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(工单编号)","接单日期 >= #" & fd & "# And 接单日期 < #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该月的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(bh.length)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("工单编号") = bh & Format(idx,"0000")

            e.DataRow("加上月份的编号") = Format(d,"yyMM") & Format(idx,"0000")
        End If
    End If
End If


--  作者:李睿涵
--  发布时间:2016/5/4 9:56:00
--  

还是不理想

关键的问题就是:如何在compute时 能截取部分编号

比如已有的编号是: 16050001   16040002

希望能只对后四位流水号做max的compute计算 得出下一个流水号是0003


--  作者:大红袍
--  发布时间:2016/5/4 10:06:00
--  

看4楼啊,多加一个列啊


--  作者:李睿涵
--  发布时间:2016/5/4 10:30:00
--  

图片点击可在新窗口打开查看此主题相关图片如下:qq截图20160504102904.png
图片点击可在新窗口打开查看
这个效果不对的
--  作者:李睿涵
--  发布时间:2016/5/4 10:39:00
--  

是要这个效果:

 


图片点击可在新窗口打开查看此主题相关图片如下:11.png
图片点击可在新窗口打开查看

 

代码写出来了:


If e.DataCol.Name = "接单日期" Then
    If 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 fd As Date = New Date(y,1,1) \'获得该月的第一天
        Dim ld As Date = new Date(y,12,31)
        Dim bh As String = Format(d,"yyMM") \'生成编号的前6位,4位年,2位月.       
       
        Dim max As Integer = DataTables("表a").Compute("count(工单编号)","接单日期 >= #" & fd & "# And 接单日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify"))
        If max =0 Then
            e.DataRow("工单编号") = bh & "0001"
        Else
            e.DataRow("工单编号") = bh & Format(max+1,"0000")
        End If

       
    End If
End If


--  作者:大红袍
--  发布时间:2016/5/4 10:43:00
--  

4楼代码没问题,你8楼的代码肯定有问题。

 

你如果确实不会写,就请上传实例。