以文本方式查看主题

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

--  作者:mikee
--  发布时间:2017/10/19 14:22:00
--  [求助] 带字母的自动编号的问题
因为习惯了用字母表示年月,所以在DataRowAdding中写了个增加行则自动编号, 可是在取编号最大值得时候有点问题,请问如何修改?

If e.DataRow.IsNull("日期") Then
    e.DataRow("日期") = Date.Now()
End If

Dim str As String = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Dim d As Date = e.DataRow("日期")
Dim y As Integer = d.Year - 2001
Dim m As Integer = d.Month - 1
Dim bh As String = str.Substring(y,1) & str.Substring(m,1) 
If e.DataRow("编号").StartsWith(bh) = False 
    Dim max As String
    Dim idx As Integer
    max = e.DataTable.Compute("max(编号)","编号 like \'" & bh & "*\'") 
    If max > "" Then 
        idx = CInt(max.Substring(7,3)) + 1 
    Else
        idx = 1
    End If
    e.DataRow("编号") = bh & "-" & Format(idx,"000")
End If

--  作者:有点甜
--  发布时间:2017/10/19 15:41:00
--  

If e.DataRow.IsNull("日期") Then
    e.DataRow("日期") = Date.Now()
End If


Dim str As String = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"


Dim d As Date = e.DataRow("日期")
Dim y As Integer = d.Year - 2001
Dim m As Integer = d.Month - 1
Dim bh As String = str.Substring(y,1) & str.Substring(m,1)
If e.DataRow("编号").StartsWith(bh) = False
    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
    Else
        idx = 1
    End If
    e.DataRow("编号") = bh & "-" & Format(idx,"000")
End If


--  作者:lxy060669
--  发布时间:2017/10/19 15:48:00
--  
 修改:idx = CInt(max.Substring(7,3)) + 1 为
 idx = CInt(max.Substring(3,3)) + 1
因为:例子中是从第8位取3位,你的编号总共只有6位数,从第4位起取值。所以7替换为3。