以文本方式查看主题

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

--  作者:nbsugu_z
--  发布时间:2020/6/20 12:39:00
--  自动编号
\'自动编号
If e.DataCol.Name = "rq" Then
    If e.DataRow.IsNull("rq") Then
        e.DataRow("xsbh") = Nothing
    Else
        Dim bh As String = Format(e.DataRow("rq"),"yyyyMMdd") \'取得编号的8位前缀
        If e.DataRow("xsbh").StartsWith(bh) = False \'如果编号的前8位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(xsbh)","rq = #" & e.DataRow("rq") & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该天的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(9,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("xsbh") = bh & "-" & Format(idx,"000")
        End If
    End If
End If


老师,以上是按日编号。现在我要不是按日编号,前面日期如20200620随着rq变动,后面3位一直累加下去,直到年份变化再重新从001开始,这代码是何修改?请指教

--  作者:有点蓝
--  发布时间:2020/6/20 13:52:00
--  
If e.DataCol.Name = "rq" Then
    If e.DataRow.IsNull("rq") Then
        e.DataRow("xsbh") = Nothing
    Else
        Dim d As Date = e.DataRow("rq")
        Dim bh As String = Format(d,"yyyyMMdd") \'取得编号的8位前缀
        Dim d1 As Date = new Date(d.Year,d.Month,1)
        If e.DataRow("xsbh").StartsWith(bh) = False \'如果编号的前8位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(xsbh)","rq >= #" & d1 & "# and rq < #" & d1.AddYears(1) & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该天的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(9,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("xsbh") = bh & "-" & Format(idx,"000")
        End If
    End If
End If