1、目的:生产批号的自动生成
2、批号生成规则:"P”+”生产日期"+"2位顺序号"
3、批号判断规则:优先在系统找出相同品番的批号,然后再判断此类品番同日期是否有批次,如果有顺序号+1,没有执行2
例子:
生产批号 日期 品番
P2012082701 2012-08-27 A0001
P2012082701 2012-08-27 B0001
P2012082701 2012-08-27 C0001
P2012082702 2012-08-27 A0001
P2012082702 2012-08-27 B0001
P2012082801 2012-08-28 A0001
P2012082801 2012-08-28 B0001
原来我只判断时间的代码如下:
If e.DataCol.Name = "日期" Then
If e.DataRow.IsNull("日期") Then
e.DataRow("生产批号") = Nothing
Else
Dim bh As String = Format(e.DataRow("日期"),"yyyyMMdd") '取得编号的8位前缀
If e.DataRow("生产批号").StartsWith(bh) = False '如果编号的前8位不符
Dim max As String
Dim idx As Integer
max = e.DataTable.Compute("Max(生产批号)","日期 = #" & e.DataRow("日期") & "#") '取得该天的最大编号
If max > "" Then '如果存在最大编号
idx = CInt(max.Substring(10,2)) + 1 '获得最大编号的后2位顺序号,并加1
Else
idx = 1 '否则顺序号等于1
End If
e.DataRow("生产批号") = "P" & bh & "-" & Format(idx,"00")
End If
End If
End If