以文本方式查看主题

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

--  作者:朱女士
--  发布时间:2022/11/18 14:00:00
--  自动编号

老师您好!

     我想在订单清单表里加“条码编码”字段,编码规则:是用当天的日期+记录号自动生成。这个编码是具有每条记录的唯一性。我写的代码,只能生成一条记录,谢谢指教!第二条就出现如下错误:

NET Framework 版本:4.0.30319.42000
Foxtable 版本:2022.8.18.1
错误所在事件:表,ddqdb,DataColChanged
详细错误信息:
调用的目标发生了异常。
索引和长度必须引用该字符串内的位置。
参数名: length

代码:

If e.DataCol.Name = "接单日期" Then
    If e.DataRow.IsNull("接单日期") Then
        e.DataRow("条码编码") = Nothing
    Else
        Dim dl As Date = Date.Today
        Dim y As Integer = dl.Year
        Dim m As Integer = dl.Month
         Dim d As Integer = dl.day
        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(dl, "yyyyMMdd") \'生成编号的前6位,4位年,2位月.
        If e.DataRow("条码编码").StartsWith(bh) = False Then\'如果编号的前8位不符
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.Compute("Max(条码编码)", "接单日期 >= #" & fd & "# And 接单日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")) \'取得该月的最大编号
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(13, 5)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("条码编码") = bh & "-" & Format(idx, "00000")
        End If
    End If
End If


--  作者:有点蓝
--  发布时间:2022/11/18 14:13:00
--  
idx = CInt(max.Substring(13, 5)) + 1
改为
idx = CInt(max.Substring(max.length - 5)) + 1