以文本方式查看主题

-  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=114395)

--  作者:kinglw110
--  发布时间:2018/2/5 13:30:00
--  自动编号
在表的DataRowAdding 写了下面代码,

Dim d As Date=Date.Today \'以当天的年月作为编号的前6位
Dim y As Integer = d.Year
Dim m As Integer = d.Month
Dim bh As String = Format(d,"yyyy-MM")

Dim max As String
Dim idx As Integer
max = e.DataTable.Compute("max(产品编号)","Substring(产品编号,1,4)= & y &  and Substring(产品编号,5,2)= & m & ")   ‘查找年,月相同的编号的最大编号
  If max > "" Then \'如果存在最大编号
idx = CInt(max.Substring(8,3)) + 1 \'获得最大编号的后三位顺序号,并加1
Else
    idx = 1 \'否则顺序号等于1
End If
e.DataRow("产品编号") = bh & "-" & Format(idx,"000")


编号不会增加
[此贴子已经被作者于2018/2/5 14:33:34编辑过]

--  作者:kinglw110
--  发布时间:2018/2/5 13:31:00
--  
产品编号 2017-12-001  这样的格式
--  作者:kinglw110
--  发布时间:2018/2/5 14:29:00
--  
知道原因在哪了,新版的SUBTRING 的参数设定更改了,老版本是从0 开始算的, 新版本是1开始的

但编号不会自动 查找最大编号的写法还有错误,请老师帮忙改下
[此贴子已经被作者于2018/2/5 14:34:43编辑过]

--  作者:有点甜
--  发布时间:2018/2/5 14:47:00
--  
 
Dim d As Date=Date.Today \'以当天的年月作为编号的前6位
Dim bh As String = Format(d,"yyyy-MM")

Dim max As String
Dim idx As Integer
max = e.DataTable.Compute("max(产品编号)","产品编号 like \'" & bh & "%\'")   ‘查找年,月相同的编号的最大编号
If max > "" Then \'如果存在最大编号
    idx = CInt(max.Substring(bh.length+1,3)) + 1 \'获得最大编号的后三位顺序号,并加1
Else
    idx = 1 \'否则顺序号等于1
End If
e.DataRow("产品编号") = bh & "-" & Format(idx,"000")
 
[此贴子已经被作者于2018/2/5 14:47:35编辑过]

--  作者:kinglw110
--  发布时间:2018/2/6 12:48:00
--  
可以正常使用了,谢谢