以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]自动编号代码设置之后经常出现重复  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=151474)

--  作者:lfc2019
--  发布时间:2020/6/24 9:14:00
--  [求助]自动编号代码设置之后经常出现重复
在表事件datacolchanged设置代码如下:
If e.DataCol.Name = "modate" Then
    If e.DataRow.IsNull("modate") Then
        e.DataRow("mono") = Nothing
    Else
        Dim u As String = e.DataRow("useri")
        Dim d As Date = e.DataRow("modate")
        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,m,1) \'获得该月的第一天
        Dim ld As Date = New Date(y,m,Days) \'获得该月的最后一天
        Dim bh As String = Format(d,"yyyyMM") \'生成编号的前6位,4位年,2位月.
        If e.DataRow("mono").StartsWith(bh) = False \'如果编号的前6位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(mono)","modate >= #" & fd & "# And modate <= #" & ld & "# And [num] <> " & e.DataRow("num")) \'取得该月的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(7,4)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("mono") = bh & "-" & Format(idx,"0000") & "-" & u
        End If
    End If
End If
表默认不加载任何数据,设置自动编号之后,经常会出现编号重复。求解???

--  作者:有点蓝
--  发布时间:2020/6/24 9:25:00
--  
        If e.DataRow("mono").StartsWith(bh) = False \'如果编号的前6位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.sqlCompute("Max(mono)","modate >= #" & fd & "# And modate <= #" & ld & "# And [num] <> " & e.DataRow("num")) \'取得该月的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(7,4)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("mono") = bh & "-" & Format(idx,"0000") & "-" & u
 e.DataRow.save
        End If

--  作者:lfc2019
--  发布时间:2020/6/24 9:32:00
--  

图片点击可在新窗口打开查看此主题相关图片如下:69d3165e-6f1e-4e26-b965-833afd8f33c7.png
图片点击可在新窗口打开查看
代码改了之后提示这个?

--  作者:cd_tdh
--  发布时间:2020/6/24 9:41:00
--  

#改\'

 


--  作者:有点蓝
--  发布时间:2020/6/24 9:43:00
--  
http://www.foxtable.com/webhelp/topics/2343.htm