以文本方式查看主题

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

--  作者:1qaz2wsx
--  发布时间:2021/4/2 17:11:00
--  求助防重复录入的问题
我是一个初学者,做了一个小程序,里面有一段防重复输入的程序,在日期中只想用到年月,而不去比较日,就是每月一个姓名只录入一次,请教该如何写这个程序呀?个人认为应该把日期及 XH格式化一下再比较,用到Format("日期","yyyyMM") ,但不知道怎么写这段代码。下面是我写的一段代码,实测好用,但同月换一天就又可以录入了,起不到防重复的作用。
Select Case e.DataCol.name   \'日期姓名防重复输入验证
    Case "日期","姓名"
        Dim xh As String
        Dim gg As String
        If e.DataCol.Name= "日期" Then
            xh = e.NewValue
            gg = e.DataRow("姓名")
        Else
            gg = e.NewValue
            xh = e.DataRow("日期")
        End If
        If xh > "" AndAlso gg  > "" Then 
            Dim dr As DataRow = e.DataRow
            If e.DataTable.Find("日期 = \'" & xh & "\' And 姓名 = \'" & gg & "\'") IsNot Nothing Then
                MessageBox.Show("该学员本月已录入完成,不能重复,只能修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
                e.Cancel = True
            End If
        End If
End Select
[此贴子已经被作者于2021/4/6 15:50:56编辑过]

--  作者:有点蓝
--  发布时间:2021/4/2 17:31:00
--  
Select Case e.DataCol.name   \'日期姓名防重复输入验证
    Case "日期","姓名"
        Dim xh As date
        Dim gg As String
        If e.DataCol.Name= "月份" Then
            xh = new date(e.NewValue.year,e.NewValue.month,1)
            gg = e.DataRow("姓名")
        Else
            gg = e.NewValue
            xh = new date(e.DataRow("日期").year,e.DataRow("日期").month,1)
        End If
        If xh > "" AndAlso gg  > "" Then 
            Dim dr As DataRow = e.DataRow
            If e.DataTable.Find("日期 >= #" & xh & "# and 日期 < #" & xh.addmonths(1) & "# And 姓名 = \'" & gg & "\'") IsNot Nothing Then
                MessageBox.Show("该学员本月已录入完成,不能重复,只能修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
                e.Cancel = True
            End If
        End If
End Select

--  作者:1qaz2wsx
--  发布时间:2021/4/6 11:01:00
--  
感谢!上机调试后发现出现如下代码,不知何故。

.NET Framework 版本:4.0.30319.42000
Foxtable 版本:2020.5.29.8
错误所在事件:表,学员,DataColChanging
详细错误信息:
调用的目标发生了异常。
从字符串“”到类型“Date”的转换无效。

我的需求是:每月每个学员的数据只可以录入一次,不重复。但可以跨年度。在这个数据表中,有月份(根据日期输入自动填入)一列,本来可以用这个来比较,但不能跨年度。
Select Case e.DataCol.name   \'日期姓名防重复输入验证
    Case "日期","姓名"
        Dim xh As String
        Dim gg As String
        If e.DataCol.Name= "日期" Then
            xh = e.NewValue
            gg = e.DataRow("姓名")
        Else
            gg = e.NewValue
            xh = e.DataRow("日期")
        End If
        If xh > "" AndAlso gg  > "" Then 
            Dim dr As DataRow = e.DataRow
            If e.DataTable.Find("日期 = \'" & xh & "\' And 姓名 = \'" & gg & "\'") IsNot Nothing Then
                MessageBox.Show("该学员本月已录入完成,不能重复,只能修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
                e.Cancel = True
            End If
        End If
End Select

--  作者:有点蓝
--  发布时间:2021/4/6 11:15:00
--  
看2楼
--  作者:1qaz2wsx
--  发布时间:2021/4/6 15:49:00
--  
感谢@有点蓝 朋友的帮助。我就是用了你二楼的代码才出现如下提示代码:

.NET Framework 版本:4.0.30319.42000
Foxtable 版本:2020.5.29.8
错误所在事件:表,学员,DataColChanging
详细错误信息:
调用的目标发生了异常。
从字符串“”到类型“Date”的转换无效。

以下是我编写的代码,实测好用,但只有日期列的年月日与姓名列值都相同时才起到防重复的作用。我现在的目的是只需日期列的年月与姓名相同时防重复起作用,也就是说每个姓名每月只可以录入一次数据,但可以跨年。
Case "日期","姓名"
        Dim xh As String
        Dim gg As String
        If e.DataCol.Name= "日期" Then
            xh = e.NewValue
            gg = e.DataRow("姓名")
        Else
            gg = e.NewValue
            xh = e.DataRow("日期")
        End If
        If xh > "" AndAlso gg  > "" Then 
            Dim dr As DataRow = e.DataRow
            If e.DataTable.Find("日期 = \'" & xh & "\' And 姓名 = \'" & gg & "\'") IsNot Nothing Then
                MessageBox.Show("该学员本月已录入完成,不能重复,只能修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
                e.Cancel = True
            End If
        End If
End Select

[此贴子已经被作者于2021/4/6 15:50:01编辑过]

--  作者:有点蓝
--  发布时间:2021/4/6 15:52:00
--  
Select Case e.DataCol.name   \'日期姓名防重复输入验证
    Case "日期","姓名"
if  e.datarow.isnull("日期") = false
        Dim xh As date
        Dim gg As String
        If e.DataCol.Name= "日期" Then
            xh = new date(e.NewValue.year,e.NewValue.month,1)
            gg = e.DataRow("姓名")
        Else
            gg = e.NewValue
            xh = new date(e.DataRow("日期").year,e.DataRow("日期").month,1)
        End If
        If xh > "" AndAlso gg  > "" Then 
            Dim dr As DataRow = e.DataRow
            If e.DataTable.Find("日期 >= #" & xh & "# and 日期 < #" & xh.addmonths(1) & "# And 姓名 = \'" & gg & "\'") IsNot Nothing Then
                MessageBox.Show("该学员本月已录入完成,不能重复,只能修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
                e.Cancel = True
            End If
        End If
end if
End Select

--  作者:1qaz2wsx
--  发布时间:2021/4/6 17:33:00
--  
@有点蓝,你好!
6楼的代码我上机测试了一个,还是不行,显示调用错误。我建议用:Format(d,"yyyyMM") \'生成前6位,4位年,2位月把xh、日期格式化后进行比较,但在:If e.DataTable.Find("日期 = \'" & xh & "\' And 姓名 = \'" & gg & "\'") IsNot Nothing Then一句中,如何把日期格式化成yyyymm格式的语名我不会写。

Case "日期","姓名"
        Dim xh As String
        Dim gg As String
        If e.DataCol.Name= "日期" Then
            xh = e.NewValue
            gg = e.DataRow("姓名")
        Else
            gg = e.NewValue
            xh = e.DataRow("日期")
        End If
        Dim bh As String = Format(xh,"yyyyMM") \'生成前6位,4位年,2位月。新加一行
        If xh > "" AndAlso gg  > "" Then 
            Dim dr As DataRow = e.DataRow
            If e.DataTable.Find("日期 = \'" & bh & "\' And 姓名 = \'" & gg & "\'") IsNot Nothing Then   \'这里的日期如何格式化成前六位?也就是4位年,2位月?这个语句我不会写,不知道语法呀
                MessageBox.Show("该学员本月已录入完成,不能重复,只能修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
                e.Cancel = True
            End If
        End If
End Select

--  作者:有点蓝
--  发布时间:2021/4/6 20:14:00
--  
Select Case e.DataCol.name   \'日期姓名防重复输入验证
    Case "日期","姓名"
        Dim xh As Date
        Dim gg As String
        If e.DataCol.Name= "日期" Then
            xh = new Date(e.NewValue.year,e.NewValue.month,1)
            gg = e.DataRow("姓名")
        Else
            gg = e.NewValue
            xh = new Date(e.DataRow("日期").year,e.DataRow("日期").month,1)
        End If
        If xh <> Nothing AndAlso gg  > "" Then
            Dim dr As DataRow = e.DataRow
            If e.DataTable.Find("日期 >= #" & xh & "# and 日期 < #" & xh.AddMonths(1) & "# And 姓名 = \'" & gg & "\'") IsNot Nothing Then
                MessageBox.Show("该学员本月已录入完成,不能重复,只能修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
                e.Cancel = True
            End If
        End If
End Select

--  作者:1qaz2wsx
--  发布时间:2021/4/7 7:01:00
--  
高手呀!上机测试后好用,达到了要求。感谢@有点蓝的帮助。