禁止重复记录上传
我用MSHFLEXGRID批量输入资料,最后提交,上传到SQL scandata表中, 并且该记录在SCANDATA中要不存在,且记录不重复.
代码如下:
可是我传了,还可以再上传 ,麻烦帮看看,谢谢
Dim js As Integer
Dim rs1 As New Recordset
Dim rs2 As New Recordset
Dim rs4 As New Recordset
Dim Djls As Integer
If js = MS1.Rows - 2 Then
MsgBox "没有要保存的数据!", , "提示窗口"
Exit Sub
End If
rs1.Open "select * from scandata", cnn, 1, 3
rs2.Open "select * from scandata where barcode='" + MS1.TextMatrix(i, 1) + "' and stationname='" + Combo1 + "'", cnn, adOpenDynamic, adLockReadOnly
For i = 1 To MS1.Rows - 2
If MS1.TextMatrix(i, 1) <> "" And MS1.TextMatrix(i, 2) <> "" And MS1.TextMatrix(i, 4) <> "" Then
If rs2.EOF Then
'添加新记录到"入库从表"中
rs1.AddNew
If MS1.TextMatrix(i, 1) <> "" Then rs1.Fields("barcode") = Trim(MS1.TextMatrix(i, 1))
If MS1.TextMatrix(i, 2) <> "" Then rs1.Fields("gch") = MS1.TextMatrix(i, 2)
rs1.Fields("gcm") = MS1.TextMatrix(i, 3)
If MS1.TextMatrix(i, 4) <> "" Then rs1.Fields("tdh") = MS1.TextMatrix(i, 4)
If MS1.TextMatrix(i, 5) <> "" Then rs1.Fields("bh") = MS1.TextMatrix(i, 5)
rs1.Fields("cc") = MS1.TextMatrix(i, 6)
rs1.Fields("ys") = MS1.TextMatrix(i, 7)
rs1.Fields("bzl") = MS1.TextMatrix(i, 8)
rs1.Fields("zc") = MS1.TextMatrix(i, 9)
rs1.Fields("sl") = MS1.TextMatrix(i, 10)
rs1.Fields("stationname") = Trim(Combo1)
rs1.Fields("scanop") = Trim(Label4)
rs1.Fields("scandate") = Date
rs1.Fields("scantime") = Time
rs1.Update
Djls = Djls + 1
End If
End If
Next i
rs1.Close
rs2.Close
MsgBox "共有『" + CStr(Djls) + "』记录数被导入" mshflexgrid 数据
[解决办法]
这两句颠倒一下:
For i = 1 To MS1.Rows - 2
rs2.Open "select * from scandata where barcode='" + MS1.TextMatrix(i, 1) + "' and stationname='" + Combo1 + "'", cnn, adOpenDynamic, adLockReadOnly