自动编号生成方法
以下例子, 实际测试的时候,如果表中已经有不符合规范的编号,请先删除之,否则会影响编号的生成结果。
一、按月生成编号
假定有个表,需要按月自动生成编号,四位年,两位月,最后三位是顺序号,如下图所示:

要自动生成上述编号,只需将表事件DataColChanged的代码设置为:
If
e.DataCol.Name 
=  "日期"
Then
    If e.DataRow.IsNull("日期")
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 = Format(d,"yyyyMM")
'生成编号的前6位,4位年,2位月.
        If e.DataRow("编号").StartsWith(bh) 
= False '如果编号的前6位不符
            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(7,3)) 
+ 1 
'获得最大编号的后三位顺序号,并加1
            Else
                idx =
1 '否则顺序号等于1
            End 
If
            e.DataRow("编号") 
= bh &
"-" &
Format(idx,"000")
        End If
    End  
If
End 
If
说明:
我们知道,上图中编号的前六位表示年月,后三位为顺序号,是需要递增的,递增顺序号的代码如下:
idx = CInt(max.Substring(7,3)) + 1 '获得最大编号的后三位顺序号,并加1
以编号201105-002为例,从位置7(也就是第8个字符,因为字符的顺序号是从0开始的)开始,取3个字符,就是取得最后的三个字符(位置分别为7、8、9),得到的是“002”:
| 编号 | 2 | 0 | 1 | 1 | 0 | 5 | - | 0 | 0 | 2 | 
| 位置 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 
提示:本节的示例都假定主键列为_Identify,如果主键列不是_Identify,请将代码中的“_Identify”改为实际的主键列名。
二、按日生成编号
假定有个表,需要按天自动生成编号,四位年,两位月,两位日,最后三位是顺序号,如下图所示:

要自动生成上述编号,代码更加简单:
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("日期")
& "# And [_Identify] 
<> " & e.DataRow("_Identify"))
'取得该天的最大编号
            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
三、按类别编号
假定有下图所示的一个表,编号根据类别生成,前两位为类别,后三位为顺序号:

要自动生成上面的编号,可以将DataColChanged事件代码设置为:
Select
e.DataCol.Name四、按日期和类别编号
假定有个表,需要按月自动生成编号,根据工程代码按顺序编号,前4位是工程代码,然后是4位年,2位月,最后4位是顺序号,如下图所示:

要自动生成上面的编号,可以将DataColChanged事件代码设置为:
Select
e.DataCol.Name
    Case 
"制单日期","工程代码"
        If e.DataRow.IsNull("制单日期")
OrElse e.DataRow.IsNull("工程代码")
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("工程代码")
& "-"
& Format(d,"yyyyMM")
& "-"
'生成编号的前缀
            If e.DataRow("单据编号").StartsWith(bh) 
= False  
'如果单据编号前缀不符
                Dim 
max As String
                Dim
idx As
Integer
                Dim
flt As
String
                flt =
"工程代码 
= '"& e.DataRow("工程代码")
&  
"' 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