以文本方式查看主题

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

--  作者:seal51
--  发布时间:2014/6/30 19:28:00
--  自动编号

想生成下面的自动编号, 老师看看代码如何更改,自己搞不出来了

 

采购单号=公司代号(ST)-厂家编号(AA, BB,CC 等)+年(14年)+流水(01,02,03.。。) 比如: ST-AA1401, ST-AA1402, ST-BB401...


 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目51.foxdb

[此贴子已经被作者于2014-6-30 19:36:27编辑过]

--  作者:seal51
--  发布时间:2014/6/30 19:38:00
--  
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 = "SM-" & e.DataRow("厂家编号") & "" & Format(d,"yy") & "" \'生成编号的前缀
            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(7,2)) + 1 \'获得最大采购单号的后四位顺序号,并加1
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                e.DataRow("采购单号") = bh & Format(idx,"00")
            End If
        End If
End Select

--  作者:seal51
--  发布时间:2014/6/30 19:39:00
--  

现在问题是流水号怎么不加1呢


--  作者:有点甜
--  发布时间:2014/6/30 19:42:00
--  
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 bh As String = "SM-" & e.DataRow("厂家编号") & "" & Format(d,"yy") & "" \'生成编号的前缀
           
            Dim max As String
            Dim idx As Integer
            Dim flt As String
            flt = "采购单号 like \'" & bh & "%\' And [_Identify] <> " & e.DataRow("_Identify")
            max = e.DataTable.Compute("Max(采购单号)",flt) \'取得该月的相同厂家编号的最大采购单号
            If max > "" Then \'如果存在最大采购单号
                idx = CInt(max.Substring(bh.Length,2)) + 1 \'获得最大采购单号的后四位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("采购单号") = bh & Format(idx,"00")
        End If
End Select

--  作者:seal51
--  发布时间:2014/6/30 19:48:00
--  
谢谢甜老师, 还有一个问题, 如果厂家编号不是固定的两个字母, 有的是2个, 有的是3个,或4个字母, 怎么办
--  作者:有点甜
--  发布时间:2014/6/30 19:50:00
--  

没有问题的,直接输入多个字母也是可以的。


--  作者:seal51
--  发布时间:2014/6/30 19:53:00
--  

测试可以,谢谢甜老师!


--  作者:seal51
--  发布时间:2014/7/1 9:12:00
--  

老师,我还需要一个自动编号

比如, 排产单号=P14-0601, P是前缀, 14是年,06是月份, 01是流水号, 这个如何写代码?


--  作者:Bin
--  发布时间:2014/7/1 9:16:00
--  
Dim bh As String = "P" & Format(date.today,"yy-MM") \'生成编号的前缀



--  作者:seal51
--  发布时间:2014/7/1 9:22:00
--  
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 = "P" & Format(d,"yy") & "-" & Format(d,"MM") \'生成排产单号的前6位,2位年,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(6,2)) + 1 \'获得最大排产单号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("排产单号") = bh & Format(idx,"00")
        End If
    End If
End If