以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  导入excel表 匹配字段后 且选择了是否唯一为是的接收字段后 怎么写判断 对应的接收字段值如果已经有了来源字段的内容时,跳过该行数据,避免重复值录入  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=173211)

--  作者:cnsjroom
--  发布时间:2021/11/19 0:59:00
--  导入excel表 匹配字段后 且选择了是否唯一为是的接收字段后 怎么写判断 对应的接收字段值如果已经有了来源字段的内容时,跳过该行数据,避免重复值录入
导入excel表 匹配字段后  且选择了是否唯一为是的接收字段后  怎么写判断  对应的接收字段值如果已经有了来源字段的内容时,跳过该行数据,避免重复值录入图片点击可在新窗口打开查看

假定我的接收字段里面有一个身份证号  乡镇  单位列  想新增一个禁止重复身份证号 乡镇  单位的判断,实现当导入的数据如果表里已经有了一个身份证号或者其他设定了 是否唯一=是  的值之后,不在输入这个身份证号对应及其对应的整行数据,下述代码该怎么补充呢?
Dim r As Row
Dim i,j As Integer
Dim t1,t2 As Table
Dim str1,str2 As String
Dim str As String
t1 = Tables(Vars("btname11"))
t2 = e.form.Controls("Table1").Table
str1 = e.form.Controls("TextBox1").value
str2 = e.form.Controls("ComboBox1").value
Dim prb As WinForm.ProgressBar = e.Form.Controls("ProgressBar1")
If str1 = "" OrElse str2 = "" Then
    Return
End If
Dim Book As New XLS.Book(str1)
Dim Sheet As XLS.Sheet = Book.Sheets(str2)
If e.Form.Controls("CheckBox1").checked = False Then
    For Each r In t2.Rows
        If r.IsNull("来源字段") OrElse r.IsNull("接收字段") Then
            MessageBox.Show("字段匹配未完成!")
            Return
        End If
    Next
    prb.Visible = True
    prb.Maximum = Sheet.Rows.Count - 1
    For i = 1 To Sheet.Rows.Count -1
        r = t1.AddNew()
        For j = 0 To t2.Rows.count - 1
            \'r(t2.Rows(j)(1)) = Sheet(i,j).Value
            Dim ss As String  =Sheet(i,t2.Rows(j)("来源列数")).Value


            r(t2.Rows(j)("接收字段")) = ss.Replace(" ", "").Trim
        Next
        prb.Value = i
    Next
Else
    For j = 0 To Sheet.Cols.count - 1
        If t1.cols.Contains(Sheet(0,j).value) Then
            If str = "" Then
                str = Sheet(0,j).value
                str = str.Replace(" ", "").Trim
            Else
                str = str & "," & Sheet(0,j).value
                str = str.Replace(" ", "").Trim
            End If
        End If
    Next
    If str = "" Then
        MessageBox.Show("对不起,没有匹配字段!")
        Return
    End If
    If MessageBox.Show("是否只导入匹配字段?","询问",MessageBoxButtons.YesNo,MessageBoxIcon.Question) = DialogResult.Yes Then
        prb.Visible = True
        prb.Maximum = Sheet.Rows.Count - 1
        For i = 1 To Sheet.Rows.Count -1
            r = t1.AddNew()
            For j = 0 To str.split(",").Length - 1
                Dim sss As String =Sheet(i,j).Value
                r(str.split(",")(j)) = sss.Replace(" ", "").Trim
            Next
            prb.Value = i
        Next
    Else
        Return
    End If
End If
t1.DataTable.save
MessageBox.Show("数据导入完毕!")
\'e.Form.close

--  作者:有点蓝
--  发布时间:2021/11/19 8:22:00
--  
使用find查询:http://www.foxtable.com/webhelp/topics/2334.htm,参考第三段代码的判断方法
--  作者:cnsjroom
--  发布时间:2021/11/19 9:06:00
--  回复:(有点蓝)使用find查询:http://www.foxtable....
    For i = 1 To Sheet.Rows.Count -1
            For j = 0 To t2.Rows.count - 1
                Dim ss As String  =Sheet(i,t2.Rows(j)("来源列数")).Value
                Dim dr As DataRow = DataTables(Vars("btname11")).Find(r(t2.Rows(j)("接收字段")) = ss.Replace(" ", "").Trim)
                If dr Is Nothing Then \'如果不存在同值的数据
                r = t1.AddNew()

                r(t2.Rows(j)("接收字段")) = ss.Replace(" ", "").Trim
                End If
            Next   
        prb.Value = i
    Next

运行提示  列“姓名”不属于表 导入Excel_Table1。 

如果改成如下:【红色部分代码该怎么写呢?又要适应匹配的来源字段和接收字段,又要匹配是否唯一值等于是的条件】
    For i = 1 To Sheet.Rows.Count -1
        
        Dim dr As DataRow = DataTables(Vars("btname11")).Find(“”)
        If dr Is Nothing Then \'如果不存在同编号的订单
            r = t1.AddNew()
        Else
            MessageBox.Show("存在相同内容,即将跳过")
        End If
        For j = 0 To t2.Rows.count - 1
            Dim ss As String  =Sheet(i,t2.Rows(j)("来源列数")).Value
            r(t2.Rows(j)("接收字段")) = ss.Replace(" ", "").Trim
        Next
        prb.Value = i
    Next

--  作者:有点蓝
--  发布时间:2021/11/19 9:23:00
--  
http://www.foxtable.com/webhelp/topics/1284.htm

DataTables(Vars("btname11")).Find(r(t2.Rows(j)("接收字段")) & " = \'" & ss.Replace(" ", "").Trim & "\'")

--  作者:cnsjroom
--  发布时间:2021/11/19 9:46:00
--  回复:(有点蓝)http://www.foxtable.com/webhelp/to...
 For i = 1 To Sheet.Rows.Count -1
        
        DataTables(Vars("btname11")).Find(r(t2.Rows(j)("接收字段")) & " = \'" & ss.Replace(" ", "").Trim & "\'")   
是不是顺序不对  提示没有SS  此时t2表也还没有进行判断
        If dr Is Nothing Then \'如果不存在同编号的订单
            r = t1.AddNew()
        Else
            MessageBox.Show("存在相同内容,即将跳过")
        End If
        For j = 0 To t2.Rows.count - 1
            Dim ss As String  =Sheet(i,t2.Rows(j)("来源列数")).Value
            r(t2.Rows(j)("接收字段")) = ss.Replace(" ", "").Trim
        Next
        prb.Value = i
    Next


如果采用一楼的第一个代码的顺序    还是一样提示  某某不存在这个t2表中

--  作者:有点蓝
--  发布时间:2021/11/19 9:56:00
--  
For j = 0 To t2.Rows.count - 1
dim dr = DataTables(Vars("btname11")).Find(r(t2.Rows(j)("接收字段")) & " = \'" & ss.Replace(" ", "").Trim & "\'")   

觉得还是花点时间重新看看编程基础吧

--  作者:cnsjroom
--  发布时间:2021/11/19 10:18:00
--  回复:(有点蓝)For j = 0 To t2.Rows.count - 1dim ...
For i = 1 To Sheet.Rows.Count -1
        For j = 0 To t2.Rows.count - 1
            Dim ss As String  =Sheet(i,t2.Rows(j)("来源列数")).Value
            Dim dr As DataRow = DataTables(Vars("btname11")).Find(r(t2.Rows(j)("接收字段")) & " = \'" & ss.Replace(" ", "").Trim & "\'")
            If dr Is Nothing Then \'如果不存在同值的数据
                r = t1.AddNew()
                
                r(t2.Rows(j)("接收字段")) = ss.Replace(" ", "").Trim
            Else
                MessageBox.Show("存在相同数据,不予新建,即将跳过")
            End If
        Next
        prb.Value = i
    Next

图片点击可在新窗口打开查看

--  作者:有点蓝
--  发布时间:2021/11/19 10:21:00
--  
提示很明显,张冠李戴,表B的列名用到表A了
--  作者:cnsjroom
--  发布时间:2021/11/19 10:48:00
--  回复:(有点蓝)提示很明显,张冠李戴,表B的列名用到...
嗯  刚刚已经试过了楼上的操作    
是从DataTables(Vars("btname11"))表中查找T2表接收字段中如果有来源列数对应的值的  且是否唯一等于是 时 就跳过   反之是否唯一不等于是就新建
还得麻烦老师指导下
For i = 1 To Sheet.Rows.Count -1               
        Dim dr As DataRow = DataTables(Vars("btname11")).Find(“”)   \'在这里怎么写find条件
        If dr Is Nothing Then \'如果不存在同编号的订单
            r = t1.AddNew()
            For j = 0 To t2.Rows.count - 1
                  Dim ss As String  =Sheet(i,t2.Rows(j)("来源列数")).Value
                  r(t2.Rows(j)("接收字段")) = ss.Replace(" ", "").Trim
             Next
        Else
            MessageBox.Show("存在相同内容,即将跳过")
        End If
        prb.Value = i
 Next

--  作者:有点蓝
--  发布时间:2021/11/19 10:51:00
--  
类似:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=173142&replyID=161611&skin=1