以文本方式查看主题

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

--  作者:18920118515
--  发布时间:2022/7/10 15:13:00
--  自动编号问题
老师,我在DataRowAdding事件中写入自动编码代码,运行错误,不能实现按照月份编号,是否自动编号代码不能写在新增行的事件中

Dim d As Date = Date.today
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("关联索引列").StartsWith(bh) = False \'如果编号的前6位不符
    Dim max As String
    Dim idx As Integer
    max = e.DataTable.Compute("Max(关联索引列)","#" & d & "# >= #" & fd & "# And #" & d & "# <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该月的最大编号
    If max > "" Then \'如果存在最大编号
        idx = CInt(max.Substring(7,4)) + 1 \'获得最大编号的后四位顺序号,并加1
    Else
        idx = 1 \'否则顺序号等于1
    End If
    e.DataRow("关联索引列") = bh & "-" & Format(idx,"0000")
End If
[此贴子已经被作者于2022/7/10 15:13:54编辑过]

--  作者:有点蓝
--  发布时间:2022/7/10 21:16:00
--  
Dim d As Date = Date.today
Dim bh As String = Format(d,"yyyyMM") \'生成编号的前6位,4位年,2位月.
    Dim max As String
    Dim idx As Integer
    max = e.DataTable.Compute("Max(关联索引列)","Substring(关联索引列,1,6)=\'" & bh & "\'") \'取得该月的最大编号
    If max > "" Then \'如果存在最大编号
        idx = CInt(max.Substring(7,4)) + 1 \'获得最大编号的后四位顺序号,并加1
    Else
        idx = 1 \'否则顺序号等于1
    End If
    e.DataRow("关联索引列") = bh & "-" & Format(idx,"0000")
End If

--  作者:18920118515
--  发布时间:2022/7/11 11:17:00
--  
谢谢老师
--  作者:18920118515
--  发布时间:2022/7/11 11:47:00
--  

老师,您再帮我看看,下边这段代码怎么改,代码格式应该为:“2位字母-yyyymm-4位顺序号”,受累老师,我试了几次都不行

Dim d As Date = Date.today

        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 = e.DataRow("线索来源") & "-" & Format(d,"yyyyMM") & "-" \'生成编号的前缀

        If e.DataRow("关联索引二列").StartsWith(bh) = False \'如果编号的前10位不符

            Dim max As String

            Dim idx As Integer

            Dim flt As String

            flt = "线索来源 = \'"& e.DataRow("线索来源") & "\' And #" & d & "#  >= #" & fd & "# And #" & d & "#  <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")

            max = e.DataTable.Compute("Max(关联索引二列)",flt) \'取得该月的相同类别代码的最大单据编号

            If max > "" Then \'如果存在最大编号

                idx = CInt(max.Substring(10,4)) + 1 \'获得最大编号的后三位顺序号,并加1

            Else

                idx = 1 \'否则顺序号等于1

            End If

            e.DataRow("关联索引二列") = bh & Format(idx,"0000")

        End If


--  作者:有点蓝
--  发布时间:2022/7/11 11:51:00
--  
这种只能到datacolchanged事件处理,并且必须有一个日期列
[此贴子已经被作者于2022/7/11 11:51:08编辑过]

--  作者:18920118515
--  发布时间:2022/7/11 11:58:00
--  
谢谢老师
--  作者:18920118515
--  发布时间:2022/7/12 13:42:00
--  
老师,您帮我看下代码有什么问题,执行后报错,系统显示Substring有问题,我是在DataColChanged事件中写的,“关联索引二列”的最终格式为:两位字母-YYYYMM-四位序号,您受累给指导下
Else If e.DataCol.Name = "线索来源" Then
    If e.DataRow("关联索引二列") = Nothing Then
        e.DataRow("编号日期") = Date.today
        Dim d As Date = e.DataRow("编号日期") 
        Dim bh As String = e.DataRow("线索来源") & "-" & Format(d,"yyyyMM") \'生成编号的前6位,4位年,2位月.
        Dim max As String
        Dim idx As Integer
        max = e.DataTable.Compute("Max(关联索引二列)","Substring(关联索引二列,0,10)=\'" & bh & "\'") \'取得该月的最大编号
        If max > "" Then \'如果存在最大编号
            idx = CInt(max.Substring(10,4)) + 1 \'获得最大编号的后四位顺序号,并加1
        Else
            idx = 1 \'否则顺序号等于1
        End If
        e.DataRow("关联索引二列") = bh & "-" & Format(idx,"0000")
    End If

--  作者:有点蓝
--  发布时间:2022/7/12 14:02:00
--  
    If e.DataRow.isnull("关联索引二列") Then
        e.DataRow("编号日期") = Date.today
        Dim d As Date = e.DataRow("编号日期") 

        Dim fd As Date = New Date(y,m,1) \'获得该月的第一天

        Dim ld As Date = New Date(y,m,Days) \'获得该月的最后一天

        Dim bh As String = e.DataRow("线索来源") & "-" & Format(d,"yyyyMM") \'生成编号的前6位,4位年,2位月.
        Dim max As String
        Dim idx As Integer
        max = e.DataTable.Compute("Max(关联索引二列)","线索来源=\'" & e.DataRow("线索来源") & "\' and 编号日期>=#" & fd & "# and 编号日期<=#" & ld & "# and  [_Identify] <> " & e.DataRow("_Identify")") \'取得该月的最大编号
        If max > "" Then \'如果存在最大编号
            idx = CInt(max.Substring(max.length - 4)) + 1 \'获得最大编号的后四位顺序号,并加1
        Else
            idx = 1 \'否则顺序号等于1
        End If
        e.DataRow("关联索引二列") = bh & "-" & Format(idx,"0000")
    End If

--  作者:18920118515
--  发布时间:2022/7/12 14:58:00
--  
谢谢老师
--  作者:18920118515
--  发布时间:2022/9/5 12:23:00
--  
xiexielaoshi 
[此贴子已经被作者于2022/9/5 12:32:51编辑过]