以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  求助:自动编号只能实现一次  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=70106)

--  作者:minxizai
--  发布时间:2015/6/15 23:34:00
--  求助:自动编号只能实现一次

“DD、登记时间、编号”都是订单表的列,

 

DataRowAdded事件代码:

e.DataRow("登记时间") = Date.now
e.DataRow("DD")="DD"

 

DataColChanged事件代码:

Select e.DataCol.Name
    Case "DD","登记时间"
        If e.DataRow.IsNull("登记时间") OrElse e.DataRow.IsNull("DD") Then
            e.DataRow("编号") = Nothing
        Else
            Dim d As Date = e.DataRow("登记时间")
            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("DD") & "-" & Format(d,"yyyyMM") & "-" \'生成编号的前缀
            If e.DataRow("编号").StartsWith(bh) = False \'如果单据编号前缀不符
                Dim max As String
                Dim idx As Integer
                Dim flt As String
                flt = "DD = \'"& e.DataRow("DD") & "\' And 登记时间 >= #" & fd & "# And 登记时间 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")
                max = e.DataTable.Compute("Max(编号)",flt) \'取得该月的相同工程代码的最大单据编号
                If max > "" Then \'如果存在最大单据编号
                    idx = CInt(max.Substring(12,4)) + 1 \'获得最大单据编号的后四位顺序号,并加1
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                e.DataRow("编号") = bh & Format(idx,"0000")
            End If
        End If
End Select

 

按上述设置后,第一次新增行时没有问题,第二次开始出错。

提示信息:

.NET Framework 版本:2.0.50727.3053
Foxtable 版本:2014.11.11.1
错误所在事件:表,订单,DataColChanged
详细错误信息:
Exception has been thrown by the target of an invocation.
Index and length must refer to a location within the string.
Parameter name: length


--  作者:狐狸爸爸
--  发布时间:2015/6/16 8:26:00
--  

根据这句代码:

Dim bh As String = e.DataRow("DD") & "-" & Format(d,"yyyyMM") & "-" \'生成编号的前缀

你变好的前缀是9位,不是12位,

 

所以:

 

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

应该改为:

 

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