以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  生成不重复的条码  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=123901)

--  作者:tuohai
--  发布时间:2018/8/26 14:12:00
--  生成不重复的条码
请老师帮我看看这段代码,为什么有的时候他生成重复的条码???

Select e.DataCol.Name
    Case "录入日期","服装资料_B"
    
        If e.DataRow.IsNull("录入日期") OrElse e.DataRow.IsNull("服装资料_B") 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 Integer = e.DataRow("服装资料_B") & Format(d,"MMdd") \'生成编号的前缀
            If e.DataRow("条码").StartsWith(bh) = False \'如果条码前缀不符
                Dim max As String
                Dim idx As Integer
                Dim flt As String
                flt = "服装资料_B = \'"& e.DataRow("服装资料_B") & "\' And 录入日期 >= #" & fd & "# And 录入日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")
                max = e.DataTable.Compute("Max(条码)",flt) \'取得该月的相同服装资料_B的最大条码
                If max > "" Then \'如果存在最大条码
                    idx = CInt(max.Substring(0,3)) + 1 \'获得最大条码的后四位顺序号,并加1
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                e.DataRow("条码") =  Format(idx,"000") & bh
            End If
        End If
End Select

--  作者:有点甜
--  发布时间:2018/8/26 23:18:00
--  

你是不是多人一起使用了?如果是,试试改成

 

Select e.DataCol.Name
    Case "录入日期","服装资料_B"
       
        If e.DataRow.IsNull("录入日期") OrElse e.DataRow.IsNull("服装资料_B") 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 Integer = e.DataRow("服装资料_B") & Format(d,"MMdd") \'生成编号的前缀
            If e.DataRow("条码").StartsWith(bh) = False \'如果条码前缀不符
                Dim max As String
                Dim idx As Integer
                Dim flt As String
                flt = "服装资料_B = \'"& e.DataRow("服装资料_B") & "\' And 录入日期 >= #" & fd & "# And 录入日期 <= #" & ld & "# And [_Identify] <> " & e.DataRow("_Identify")
                max = e.DataTable.sqlCompute("Max(条码)",flt) \'取得该月的相同服装资料_B的最大条码
                If max > "" Then \'如果存在最大条码
                    idx = CInt(max.Substring(0,3)) + 1 \'获得最大条码的后四位顺序号,并加1
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                e.DataRow("条码") =  Format(idx,"000") & bh
                e.DataRow.save
            End If
        End If
End Select


--  作者:tuohai
--  发布时间:2018/8/27 14:22:00
--  
.NET Framework 版本:2.0.50727.8793
Foxtable 版本:2018.7.23.1
错误所在事件:
详细错误信息:
\'#\' 附近有语法错误。


--  作者:有点甜
--  发布时间:2018/8/27 14:25:00
--  

改成

 

Select e.DataCol.Name
    Case "录入日期","服装资料_B"
       
        If e.DataRow.IsNull("录入日期") OrElse e.DataRow.IsNull("服装资料_B") 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 Integer = e.DataRow("服装资料_B") & Format(d,"MMdd") \'生成编号的前缀
            If e.DataRow("条码").StartsWith(bh) = False \'如果条码前缀不符
                Dim max As String
                Dim idx As Integer
                Dim flt As String
                flt = "服装资料_B = \'"& e.DataRow("服装资料_B") & "\' And 录入日期 >= \'" & fd & "\' And 录入日期 <= \'" & ld & "\' And [_Identify] <> " & e.DataRow("_Identify")
                max = e.DataTable.sqlCompute("Max(条码)",flt) \'取得该月的相同服装资料_B的最大条码
                If max > "" Then \'如果存在最大条码
                    idx = CInt(max.Substring(0,3)) + 1 \'获得最大条码的后四位顺序号,并加1
                Else
                    idx = 1 \'否则顺序号等于1
                End If
                e.DataRow("条码") =  Format(idx,"000") & bh
                e.DataRow.save
            End If
        End If
End Select


--  作者:tuohai
--  发布时间:2018/8/27 16:19:00
--  
老师,还有一个问题,总是在新增行的时候出现:

.NET Framework 版本:2.0.50727.8793
Foxtable 版本:2018.7.23.1
错误所在事件:
详细错误信息:
未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。

--  作者:有点甜
--  发布时间:2018/8/27 16:21:00
--  
以下是引用tuohai在2018/8/27 16:19:00的发言:
老师,还有一个问题,总是在新增行的时候出现:

.NET Framework 版本:2.0.50727.8793
Foxtable 版本:2018.7.23.1
错误所在事件:
详细错误信息:
未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。

 

1、你表格的主键是什么?要设置成自动递增的主键

 

http://www.foxtable.com/webhelp/scr/1807.htm

 

2、如果主键没问题,那你删除【关联】重新设置关联


--  作者:tuohai
--  发布时间:2018/8/27 16:36:00
--  
我的主键是   _Identify           ,我没有创建关联表。
--  作者:有点甜
--  发布时间:2018/8/27 16:51:00
--  
以下是引用tuohai在2018/8/27 16:36:00的发言:
我的主键是   _Identify           ,我没有创建关联表。

 

1、设置成自动递增 http://www.foxtable.com/webhelp/scr/1807.htm

 

2、如果还有问题,看看你数据库那里是否设置了【约束】、【外键】等东西。