以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  自动编号的求助  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=23356)

--  作者:paullqxp
--  发布时间:2012/9/10 8:01:00
--  自动编号的求助

想做一个新增记录的自动编号,帮助里的例子是在“日期”列的基础上做判断

我想在不用“日期”列的情况下,对自动编号做如下设置:

格式为:XS20120910001 (XS是前缀 20120910是生成该条记录时的日期  001是流水号)

望能得到帮助,谢谢!

 

 


--  作者:Fotable
--  发布时间:2012/9/10 9:45: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 \'  这里是不是少了一个  then

            Dim max As String
           
Dim idx As Integer
            max = e.
DataTable.Compute("Max(编号)","日期 = #" & e.DataRow("日期") & "#") \'编号列是string型的列 怎么能用Max求值呢?  
           
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


--  作者:Fotable
--  发布时间:2012/9/10 9:47:00
--  
我用这个代码 实验测试 max语句不能正常工作,每次得到是 年月日-001
--  作者:paullqxp
--  发布时间:2012/9/10 10:56:00
--  

这段帮助我看了 我在问题里也写了  帮助的那个例子 是有“日期”列的 在日期列的基础上做判断的

我想不通过“日期”列 在新增记录时,根据今天的日期来做判断


--  作者:狐狸爸爸
--  发布时间:2012/9/10 13:17:00
--  
If e.DataCol.Name = "日期" Then
    If e.DataRow.IsNull("日期") Then
        e.DataRow("编号") = Nothing
    Else
        Dim bh As String = "XS" & 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,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("编号") = bh &  Format(idx,"000")
        End If
    End If
End If

--  作者:paullqxp
--  发布时间:2012/9/10 14:55:00
--  
狐爸,我的表里 没有“日期”这个列啊 , 怎么写代码?
--  作者:狐狸爸爸
--  发布时间:2012/9/10 15:09:00
--  

将下面的代码加入DataRowAdding事件:

 

Dim bh As String = "XS" & Format(Date.Today,"yyyyMMdd")
Dim max As String
Dim idx As Integer
max = e.DataTable.Compute("Max(编号)","编号 Like \'" & bh & "*\'" )
If max > "" Then \'如果存在最大编号
    idx = CInt(max.Substring(10,3)) + 1 \'获得最大编号的后三位顺序号,并加1
Else
    idx = 1 \'否则顺序号等于1
End If
e.DataRow("编号") = bh &  Format(idx,"000")


--  作者:paullqxp
--  发布时间:2012/9/10 16:13:00
--  
好用 感谢狐爸回复!