以文本方式查看主题

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

--  作者:q2520
--  发布时间:2018/4/15 10:23:00
--  [求助]简化代码
If e.DataCol.name = "姓名" Then
    Dim fdr As DataRow = DataTables("目录").Find("姓名 = \'" & e.newValue & "\'")
    If fdr Is Nothing Then
        MessageBox.Show("姓名输入错误","提示")
        e.DataRow.SetError("姓名", "姓名输入错误")
    Else
        e.DataRow.SetError("姓名", "")
        \'e.cancel = True
    End If
End If

If e.DataCol.name = "罚款人1" Then
    Dim fdr As DataRow = DataTables("目录").Find("姓名 = \'" & e.newValue & "\'")
    If fdr Is Nothing Then
        MessageBox.Show("姓名输入错误","提示")
        e.DataRow.SetError("罚款人1", "姓名输入错误")
    Else
        e.DataRow.SetError("罚款人1", "")
        \'e.cancel = True
    End If
End If

If e.DataCol.name = "罚款人2" Then
    Dim fdr As DataRow = DataTables("目录").Find("姓名 = \'" & e.newValue & "\'")
    If fdr Is Nothing Then
        MessageBox.Show("姓名输入错误","提示")
        e.DataRow.SetError("罚款人2", "姓名输入错误")
    Else
        e.DataRow.SetError("罚款人2", "")
        \'e.cancel = True
    End If
End If

If e.DataCol.name = "罚款人3" Then
    Dim fdr As DataRow = DataTables("目录").Find("姓名 = \'" & e.newValue & "\'")
    If fdr Is Nothing Then
        MessageBox.Show("姓名输入错误","提示")
        e.DataRow.SetError("罚款人3", "姓名输入错误")
    Else
        e.DataRow.SetError("罚款人3", "")
        \'e.cancel = True
    End If
End If

If e.DataCol.name = "罚款人4" Then
    Dim fdr As DataRow = DataTables("目录").Find("姓名 = \'" & e.newValue & "\'")
    If fdr Is Nothing Then
        MessageBox.Show("姓名输入错误","提示")
        e.DataRow.SetError("罚款人4", "姓名输入错误")
    Else
        e.DataRow.SetError("罚款人4", "")
        \'e.cancel = True
    End If
End If


怎么样简化写

--  作者:wyz20130512
--  发布时间:2018/4/15 10:36:00
--  
Select Case e.DataCol.Name
    Case "姓名"
        
    Case "罚款人1"

    Case "罚款人2"

    Case "罚款人3"

    Case "罚款人4"

End Select

--  作者:有点甜
--  发布时间:2018/4/15 10:55:00
--  

参考

 

Select Case e.DataCol.Name
    Case "姓名","罚款人1","罚款人2","罚款人3","罚款人4"              
        Dim fdr As DataRow = DataTables("目录").Find("姓名 = \'" & e.newValue & "\'")
        If fdr Is Nothing Then
            MessageBox.Show(e.DataCol.name & "输入错误","提示")
            e.DataRow.SetError(e.DataCol.name, "姓名输入错误")
        Else
            e.DataRow.SetError(e.DataCol.name, "")
            \'e.cancel = True
        End If
End Select