以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  关于导入合并数据时碰到的自动生成编号的问题  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=68880)

--  作者:新航程小何
--  发布时间:2015/5/26 18:24:00
--  关于导入合并数据时碰到的自动生成编号的问题

图片点击可在新窗口打开查看此主题相关图片如下:qq截图20150526181619.png
图片点击可在新窗口打开查看
各位老师,我现在遇到的问题是,如截图,请问老师我该怎么改(如果带入的是只有一行数据,代码是合适的,但如果超过两行就出现截图问题)

我的代码如下:
1、导入按钮(代码是导入合并的代码)
2、datacolchangged 代码,生成编号
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位月.
        
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.sqlCompute("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")
         e.DataRow.save
        
    End If
End If
3、在窗口的mouseenter中写入代码:重置列代码
DataTables("临时工表").DataCols("时间").RaiseDataColChanged()
[此贴子已经被作者于2015/5/26 18:23:51编辑过]

--  作者:大红袍
--  发布时间:2015/5/26 19:21:00
--  
 代码没有问题,做个例子发上来测试。
--  作者:新航程小何
--  发布时间:2015/5/26 20:55:00
--  又出现新问题
bin 老师,我做了个例子,但是我经过测试,没有之前的问题了,但是,编号总是在增大,连之前的导入的也一并在累加
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:导入编号问题.zip


--  作者:大红袍
--  发布时间:2015/5/26 21:01: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 = Format(d,"yyyyMM") \'生成编号的前6位,4位年,2位月.
        If e.DataRow("编号").StartsWith(bh) = False Then
            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")
         e.DataRow.save
        End If
    End If
End If

--  作者:新航程小何
--  发布时间:2015/5/27 8:33: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 = Format(d,"yyyyMM") \'生成编号的前6位,4位年,2位月.
          If e.DataRow("编号").StartsWith(bh) = False Then
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.sqlCompute("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")
         e.DataRow.save
        End If 
    End If
End If

--  作者:Bin
--  发布时间:2015/5/27 8:36:00
--  
学会举一反三

你弹出max看看 messagebox.show(max) 看看是怎么样的.  就知道为什么取 7,3位出错了.



--  作者:新航程小何
--  发布时间:2015/5/27 8:49:00
--  
这个,真心不懂,我想生成是三位顺序号,前几行数据我是在foxtable里直接做的,结果是201501-001,但是我导入数据时,就出现了一楼错误,bin老师,您能不能说详细些
--  作者:Bin
--  发布时间:2015/5/27 8:50: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 = Format(d,"yyyyMM") \'生成编号的前6位,4位年,2位月.
          If e.DataRow("编号").StartsWith(bh) = False Then
            Dim max As String
            Dim idx As Integer
            max = e.DataTable.sqlCompute("Max(编号)","时间 >= \'" & fd & "\' And 时间 <= \'" & ld & "\' And [_Identify] <> " & e.DataRow("_Identify")) \'取得该月的最大编号
             messagebox.show(max) 
            If max > "" Then \'如果存在最大编号
                idx = CInt(max.Substring(7,3)) + 1 \'获得最大编号的后三位顺序号,并加1
            Else
                idx = 1 \'否则顺序号等于1
            End If
            e.DataRow("编号") = bh & "-" & Format(idx,"000")
         e.DataRow.save
        End If 
    End If
End If

--  作者:新航程小何
--  发布时间:2015/5/27 8:54:00
--  
这个我调试了,弹出的是201501-01,关键问题是,我直接表上修改日期时候,弹出是201501-001,我就不明白了
--  作者:Bin
--  发布时间:2015/5/27 8:55:00
--  
检查你是否有其他代码. 或者是否表里本身有 201501-01 这一行数据. (数据库里面)  因为你用的是sqlCompute