以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]谁帮我看下这个自动编号的代码哪里的问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=22293)

--  作者:jiskin
--  发布时间:2012/8/8 12:49: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("入库日期") & "#") \'取得该天的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(8,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("入库编号") = "IN" & bh & Format(idx,"000")
        End If
    End If
End If


结果出来的编号是  20120808081 而且都是这个编号

--  作者:blackzhu
--  发布时间:2012/8/8 14:34:00
--  
idx = CInt(max.Substring(10,3)) + 1 \'获得最大编号的后三位顺序号,并加1

  不应该是8位吧,明明你要加上"IN"

--  作者:狐狸爸爸
--  发布时间:2012/8/8 15:08:00
--  
If e.DataCol.Name = "入库日期" Then
    If e.DataRow.IsNull("入库日期") Then
        e.DataRow("入库编号") = Nothing
    Else
        Dim bh As String = "IN" & 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("入库日期") & "#") \'取得该天的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(10,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("入库编号") = bh & Format(idx,"000")
        End If
    End If
End If

--  作者:jiskin
--  发布时间:2012/8/8 15:37:00
--  
“IN”在后面加进去的 也算啊
--  作者:狐狸爸爸
--  发布时间:2012/8/8 15:41:00
--  
3楼代码,改的地方不止一处,仔细看看
--  作者:jiskin
--  发布时间:2012/8/8 15:47:00
--  
狐爸,还有个问题,这样虽然是会自动生成编号了,但是如果我日期是2012-08-08 对应生成的编号是IN20120808001 如果我改了日期 2012-08-09 那么编号居然变成了 IN20120809002 而不是 001 ?而且每改一次日期 后面就递增1
--  作者:狐狸爸爸
--  发布时间:2012/8/8 15:53:00
--  
If e.DataCol.Name = "入库日期" Then
    If e.DataRow.IsNull("入库日期") Then
        e.DataRow("入库编号") = Nothing
    Else
        Dim bh As String = "IN" & 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 [_Identidy] <> " & e.DataRow("_Identify")) \'取得该天的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(10,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("入库编号") = bh & Format(idx,"000")
        End If
    End If
End If

--  作者:jiskin
--  发布时间:2012/8/8 16:24:00
--  
狐爸给力,搞定了~~ Identify