以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  自动编号的实现  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=43519)

--  作者:test321
--  发布时间:2013/12/9 14:15:00
--  自动编号的实现
CN131210-001这种编号怎么变?

--  作者:Bin
--  发布时间:2013/12/9 14:16:00
--  
http://www.foxtable.com/help/topics/2403.htm
--  作者:test321
--  发布时间:2013/12/9 14:45:00
--  
If e.DataCol.Name = "订单日期" Then
    If e.DataRow.IsNull("订单日期") Then
        e.DataRow("流水单号") = Nothing
    Else
        Dim bh As String = Format(e.DataRow("订单日期"),"yyyyMMdd") \'取得编号的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
End If

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

--  作者:Bin
--  发布时间:2013/12/9 14:48:00
--  
不能直接就复制,完全不考虑自己的实际情况

至少你字符长度都没有9+3 这么长 肯定会报错的啊.

--  作者:test321
--  发布时间:2013/12/9 14:49:00
--  
我给了订单编号18个字符长度啊、、
--  作者:狐狸爸爸
--  发布时间:2013/12/9 14:49:00
--  

这代码是帮助逇,你的需求有变化,要稍微调整,见红色部分:

 

If e.DataCol.Name = "订单日期" Then
    If e.DataRow.IsNull("订单日期") Then
        e.DataRow("流水单号") = Nothing
    Else
        Dim bh As String = "CN" & 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
End If

 

你的编号和帮助相比,长度不变,后面的序号也是三位,但是前面有字符CN,编号中年不是四位,是两位,调整这两部分就行。

关键你要去理解这种代码,消化了,就能举一反三,遇到问题,答案信手拈来。

[此贴子已经被作者于2013-12-9 14:51:56编辑过]

--  作者:Bin
--  发布时间:2013/12/9 14:50:00
--  
上例子.

--  作者:test321
--  发布时间:2013/12/9 14:53:00
--  

If e.DataCol.Name = "订单日期" Then
    If e.DataRow.IsNull("订单日期") Then
        e.DataRow("流水单号") = Nothing
    Else
        Dim bh As String ="PO"& 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(7,2)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("流水单号") = bh & "-" & Format(idx,"00")
        End If
    End If

 

 

ok了   但是不我知道  为什么刚才直接拿过去  用的时候要提示字符串太长?


End If


--  作者:Bin
--  发布时间:2013/12/9 14:56:00
--  
你列字段设置长度太短导致的吧
--  作者:狐狸爸爸
--  发布时间:2013/12/9 14:56:00
--  

因为你没有加cn,编号的长度少了2位,总长只有10位了,从第9位开始数三位,自然就长度不够:

idx = CInt(max.Substring(9,3)) + 1

 

你8楼的代码也有问题,应该用6楼的代码。

[此贴子已经被作者于2013-12-9 14:56:03编辑过]