以文本方式查看主题

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

--  作者:KWK001
--  发布时间:2016/9/8 8:24:00
--  [求助] 自动编号
本人参考帮助后写的自动编号,有时可以顺序下期,有时又不行,编号不会变,请老师看看那里有问题?
Select e.DataCol.Name
    Case "作业日期","工程"
        If e.DataRow.IsNull("作业日期") OrElse e.DataRow.IsNull("工程") Then
            e.DataRow("编号") = Nothing
        Else
            Dim fd As Date = Date.Today
            Dim bh As String = e.DataRow("工程") & "-" & Format(fd,"yyyyMMdd") & "-" \'生成编号的前缀
            If e.DataRow("编号").StartsWith(bh) = False \'如果编号前缀不符
                Dim max As String
                Dim idx As Integer
                Dim flt As String
                flt = "编号 like \'" & bh & "%\' And [_Identify] <> " & e.DataRow("_Identify")
                max = e.DataTable.Compute("Max(编号)",flt) \'取得该月的相同工程的最大编号
                If max > "" Then \'如果存在最大编号
                    
                    idx = CInt(max.Substring(bh.length+3)) + 1 \'获得最大编号的后三位顺序号,并加1
                    \'
                    \'idx = CInt(max.Substring(bh.Length)) + 1 \'获得最大编号的后四位顺序号,并加1
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                If user.name = "1课"
                    e.DataRow("编号") = bh & "1" & Format(idx,"0000")
                Else If user.name = "2课"
                    e.DataRow("编号") = bh & "2" & Format(idx,"0000")
                Else If user.name = "开发者"
                    e.DataRow("编号") = bh & "0" & Format(idx,"0000")

--  作者:有点蓝
--  发布时间:2016/9/8 9:01:00
--  
看不出问题,上例子测试
--  作者:KWK001
--  发布时间:2016/9/14 9:38:00
--  
切换用户后,新增行后编号会重复,请大神看看。
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目5.table

[此贴子已经被作者于2016/9/14 9:38:59编辑过]

--  作者:有点蓝
--  发布时间:2016/9/14 9:41:00
--  
表的数据如果是根据用户加载的,改为SQLCompute

max = e.DataTable.SQLCompute("Max(编号)",flt) \'取得该月的相同工程的最大编号

--  作者:KWK001
--  发布时间:2016/9/14 9:49:00
--  

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

蓝老师,还是不可以啊,编号没有变啊

--  作者:有点蓝
--  发布时间:2016/9/14 10:15:00
--  
你这种是属于不同类型使用同一种顺序号,按照下面方法处理,缺点是新增的行必须保存,否则下次获取还是会重复

Select e.DataCol.Name
    Case "作业日期","工程"
        If e.DataRow.IsNull("作业日期") OrElse e.DataRow.IsNull("工程") Then
            e.DataRow("编号") = Nothing
        Else
            Dim fd As Date = Date.Today
            Dim bh As String = e.DataRow("工程") & "-" & Format(fd,"yyyyMMdd") & "-" \'生成编号的前缀
            If e.DataRow("编号").StartsWith(bh) = False \'如果编号前缀不符
                Dim max As String
                Dim idx As Integer
                Dim flt As String
                flt = "编号 like \'" & bh & "%\' And [_Identify] <> " & e.DataRow("_Identify")
                max = e.DataTable.SQLCompute("Max(Mid(编号,len(编号) - 3,4))",flt) \'取得该月的相同工程的最大编号
                If max > "" Then \'如果存在最大编号
                    idx = CInt(max) + 1 
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                If user.name = "1课"
                    e.DataRow("编号") = bh & "1" & Format(idx,"0000")
                Else If user.name = "2课"
                    e.DataRow("编号") = bh & "2" & Format(idx,"0000")
                Else If user.name = "开发者"
                    e.DataRow("编号") = bh & "0" & Format(idx,"0000")
                End If
            End If
            
        End If
End Select

--  作者:KWK001
--  发布时间:2016/9/14 11:33:00
--  
谢谢 ,蓝老师!

下次编号这样写,可以吗? 这样应该是同一种顺序号吧!
If e.DataCol.Name = "订单日期" Then
    If e.DataRow.IsNull("订单日期") Then
        e.DataRow("订单编号") = Nothing
    Else
        Dim bh As String = User.Name & Format(e.DataRow("订单日期"),"yyMMdd") \'取得编号的8位前缀
        If e.DataRow("订单编号").StartsWith(bh) = False \'如果编号的前8位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(订单编号)","订单日期 = #" & e.DataRow("订单日期") & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该天的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(9,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("订单编号") = bh & "-" & Format(idx,"000")
        End If
    End If

--  作者:有点蓝
--  发布时间:2016/9/14 14:28:00
--  
可以,行不行测试过就知道