以文本方式查看主题

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

--  作者:deliangzhaoe
--  发布时间:2019/1/15 21:22:00
--  禁止重复

窗口A中表1选中的行,通过按钮添加到表2中,如果表2中已经存在与表1中列7、列8、列9、列10内容相同的行,则不添加,否则添加。现在代码是这样:
For Each r As Row In Tables("快速添加组织机构_table1").GetCheckedRows
    Dim xz As winform.checkbox = e.Form.controls("checkbox1")
    Dim nr As DataRow = DataTables("年度安全目标").addnew
    nr("企业名称") = r("企业名称")
    nr("部门设置") = r("部门设置")
    nr("岗位设置") = r("岗位设置")
    nr("第四层机构设置") = r("第四层机构设置")
    nr("第五层机构设置") = r("第五层机构设置")
    nr("第六层机构设置") = r("第六层机构设置")
    r.checked = False
    xz.checked = False
    Dim tr As WinForm.TreeView = Forms("安全信息化系统主窗口").Controls("TreeView4")
    tr.BuildTree("年度安全目标", "企业名称|部门设置|岗位设置|第四层机构设置|第五层机构设置|第六层机构设置")
    tr.ExpandAll()
Next
MessageBox.Show("已向《年度安全目标》表中成功添加了公司组织机构!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)

自己写了几次没成功,请帮忙改一下,谢谢。

--  作者:有点蓝
--  发布时间:2019/1/15 21:31:00
--  
For Each r As Row In Tables("快速添加组织机构_table1").GetCheckedRows
    Dim nr As DataRow = DataTables("年度安全目标").Find("列7=\'" & r("列7") & "\' and 列8=\'" & r("列8") & "\' ") \'其它列自行参考补充完整
    If nr Is Nothing Then
        nr  = DataTables("年度安全目标").AddNew
        nr("企业名称") = r("企业名称")
        nr("部门设置") = r("部门设置")
        nr("岗位设置") = r("岗位设置")
        nr("第四层机构设置") = r("第四层机构设置")
        nr("第五层机构设置") = r("第五层机构设置")
        nr("第六层机构设置") = r("第六层机构设置")
    End If
    r.checked = False
Next
Dim xz As winform.checkbox = e.Form.controls("checkbox1")
xz.checked = False
Dim tr As WinForm.TreeView = Forms("安全信息化系统主窗口").Controls("TreeView4")
tr.BuildTree("年度安全目标", "企业名称|部门设置|岗位设置|第四层机构设置|第五层机构设置|第六层机构设置")
tr.ExpandAll()
MessageBox.Show("已向《年度安全目标》表中成功添加了公司组织机构!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)

--  作者:deliangzhaoe
--  发布时间:2019/1/16 8:17:00
--  
不行啊,有重复的照样能添加进去。改了一下代码,也不行。
For Each r As Row In Tables("快速添加组织机构_table1").GetCheckedRows
    Dim nr As DataRow = DataTables("年度安全目标").Find("企业名称=\'" & r("企业名称") & "\' and 年度 = \'" & Date.Today.year & "\' and 部门设置 = \'" & r("部门设置") & "\' and 岗位设置 = \'" & r("岗位设置") & "\'")
    Dim xz As winform.checkbox = e.Form.controls("checkbox1")
    If nr IsNot Nothing Then
        MessageBox.Show("本年度您可能已添加过本公司的组织机构,禁止重复添加!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Else
        nr = DataTables("年度安全目标").addnew
        nr("企业名称") = r("企业名称")
        nr("部门设置") = r("部门设置")
        nr("岗位设置") = r("岗位设置")
        nr("第四层机构设置") = r("第四层机构设置")
        nr("第五层机构设置") = r("第五层机构设置")
        nr("第六层机构设置") = r("第六层机构设置")
        r.checked = False
        xz.checked = False
        Dim tr As WinForm.TreeView = Forms("安全信息化系统主窗口").Controls("TreeView4")
        tr.BuildTree("年度安全目标", "企业名称|部门设置|岗位设置|第四层机构设置|第五层机构设置|第六层机构设置")
        tr.ExpandAll()
        MessageBox.Show("已向《公司年度安全目标》表中成功添加了公司组织机构!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    End If
Next

老师,再帮忙看一下吧。谢谢
另外,如果选中行的部门设置或岗位设置是空值,查找年度安全目标表中的部门设置或岗位设置是空值的行,有影响吗?
[此贴子已经被作者于2019/1/16 8:20:08编辑过]

--  作者:有点甜
--  发布时间:2019/1/16 8:53:00
--  

代码要改成这样

 

 

Dim filter As String = "年度 = \'" & Date.Today.year & "\'"

If r("企业名称") = Nothing Then
   
    filter &= " and 企业名称 is null"
   
Else
   
    filter &= " and 企业名称 = \'" & r("企业名称") & "\'"
   
End If

If r("部门") = Nothing Then
   
    filter &= " and 部门 is null"
   
else
   
    filter &= " and 部门 = \'" & r("部门") & "\'"
   
End If

If r("岗位设置") = Nothing Then
   
    filter &= " and 岗位设置 is null"
   
Else
   
    filter &= " and 岗位设置 = \'" & r("岗位设置") & "\'"
   
End If

msgbox(filter)

Dim nr As DataRow = DataTables("年度安全目标").Find(filter)


--  作者:deliangzhaoe
--  发布时间:2019/1/16 9:16:00
--  
可以了,谢谢蓝老师,甜老师