-- 作者:akzzwj
-- 发布时间:2012/8/24 17:16:00
-- 我做了一个按日生成编号,结果在输入数据的时候,出现如下错误
有个入库单,包含日期、编号 两列。需要按天自动生成编号,四位年,两位月,两位日,最后三位是顺序号。
要自动生成编号,代码如下(就是官方的代码):
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(9,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If e.DataRow("编号") = bh & "-" & Format(idx,"000") End If End If End If
结果在输入数据的时候,确出现如下的错误:
此主题相关图片如下:qq截图20120822151353.jpg

|
-- 作者:狐狸爸爸
-- 发布时间:2012/8/24 17:27:00
--
我测试没有问题,可能你原来已经在编号列输入了不规范的数据。
你可以这么改改看看:
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 > "" AndAlso max.Length = 12 Then \'如果存在最大编号 idx = CInt(max.Substring(9,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If e.DataRow("编号") = bh & "-" & Format(idx,"000") End If End If End If
|