以文本方式查看主题
- Foxtable(狐表) (http://foxtable.com/bbs/index.asp)
-- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2)
---- 有关自动编号的问题 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=46451)
|
-- 作者:xvkewen
-- 发布时间:2014/2/23 23:37:00
-- 有关自动编号的问题
我想做一个自动编号功能,因为是在局域网中有多个人共同使用,所以为避免出现重复编号,所以我把自动编号功能的代码放到BeforesaveDatarow事件中了,只有在保存的时候才发放编号;但是我还想在保存前删除数据表中没用的空行;正因为此,所以才出现以下问题;怎么样才能做到保存数据时只能新增的有效数据行发放编号呢?为了让老师们能清楚的理解我想表达的意思,做了个小例子,请帮忙给看看;先拜谢了;这是例子中的数据表,当“内容”列有数据时,保存表时发放编号;  此主题相关图片如下:qq截图20140223232159.jpg  当“内容”列为空时,保存时则自动删除该行;虽然要删除的行是删除了,但在这里出现了以下问题;这样的问题怎么解决?  此主题相关图片如下:qq截图20140223232221.jpg 
|
-- 作者:jimmyhwang
-- 发布时间:2014/2/24 0:49:00
--
正好我也有个类似的编号问题,困扰着我。好类似哦。也希望得到指导。前面是编号,编号有几个相同的,然后想给相同编号的数据行添加子编号。
此主题相关图片如下:qq截图20140224004606.jpg

[此贴子已经被作者于2014-2-24 0:51:36编辑过]
|
-- 作者:lindong
-- 发布时间:2014/2/24 4:33:00
--
把表A beforsavedatarowg事件的代码改为如下:
If e.DataRow.IsNull("内容") = True Then DataTables("表A").DeleteFor("内容 Is Null") Else
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 \'如果编号的前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(7,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If e.DataRow("编号") = bh & "-" & Format(idx,"000") End If End If End If
[此贴子已经被作者于2014-2-24 4:38:10编辑过]
|
-- 作者:xvkewen
-- 发布时间:2014/2/24 13:04:00
--
多谢楼上的解答,这样改单行的问题是解决了,但是如果表中有多个无效数据的空行的话,保存的时候仍然会报错的;求指导?
以下是引用lindong在2014-2-24 4:33:00的发言:
把表A beforsavedatarowg事件的代码改为如下:
If e.DataRow.IsNull("内容") = True Then DataTables("表A").DeleteFor("内容 Is Null") Else
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 \'如果编号的前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(7,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If e.DataRow("编号") = bh & "-" & Format(idx,"000") End If End If End If
[此贴子已经被作者于2014-2-24 4:38:10编辑过]
|
-- 作者:Bin
-- 发布时间:2014/2/24 14:07:00
--
参考帮助http://www.foxtable.com/help/topics/1994.htm
|
-- 作者:test138
-- 发布时间:2014/2/24 14:58:00
--
改成这样就可以了
Dim drs As List (of DataRow) drs = DataTables("表A").Select("[内容] Is null") If drs IsNot Nothing Then Dim n As Integer = drs.count If n>0 Then For Each dr As DataRow In drs dr.Delete() Next End If
Else
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 \'如果编号的前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(7,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If e.DataRow("编号") = bh & "-" & Format(idx,"000") End If End If End If
|
-- 作者:xvkewen
-- 发布时间:2014/2/24 19:12:00
--
以下是引用test138在2014-2-24 14:58:00的发言:
改成这样就可以了
为什么加了Else下面那个编号的代码怎么都不执行呢? drs 也有Nothing的时候,为什么条件为否的时候下面的代码不执行了呢?
Dim drs As List (of DataRow) drs = DataTables("表A").Select("[内容] Is null") If drs IsNot Nothing Then Dim n As Integer = drs.count If n>0 Then For Each dr As DataRow In drs dr.Delete() Next End If
Else
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 \'如果编号的前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(7,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If e.DataRow("编号") = bh & "-" & Format(idx,"000") End If End If End If
|
-- 作者:xvkewen
-- 发布时间:2014/2/24 19:13:00
--
那个太复杂了,我还是想用我自己的这段代码,有什么办法可以解决这个难题吗?
|
-- 作者:xvkewen
-- 发布时间:2014/2/24 19:15:00
--
为什么加了Else下面那个编号的代码怎么都不执行呢? drs 也有Nothing的时候,为什么条件为否的时候下面的代码不执行了呢?
以下是引用test138在2014-2-24 14:58:00的发言:
改成这样就可以了
Dim drs As List (of DataRow) drs = DataTables("表A").Select("[内容] Is null") If drs IsNot Nothing Then Dim n As Integer = drs.count If n>0 Then For Each dr As DataRow In drs dr.Delete() Next End If
Else
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 \'如果编号的前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(7,3)) + 1 \'获得最大编号的后三位顺序号,并加1 Else idx = 1 \'否则顺序号等于1 End If e.DataRow("编号") = bh & "-" & Format(idx,"000") End If End If End If
|
-- 作者:lsy
-- 发布时间:2014/2/24 19:43:00
--
解决了:
|