gpt4 book ai didi

mysql - MS Access VBA 代码未删除所有记录

转载 作者:行者123 更新时间:2023-11-29 12:02:49 27 4
gpt4 key购买 nike

我试图删除一个表中出现在另一个表中的所有记录,但它似乎只删除了部分记录。

Private Sub removeDuplicates()

Dim resultSet1 As DAO.Recordset

Set resultSet1 = CurrentDb.OpenRecordset("remove")
resultSet1.MoveFirst

Do Until resultSet1.EOF

Dim sql As String
sql = "Delete * from [Copy Of remove] Where"

If Not IsNull(resultSet1.Fields(0)) And (resultSet1.Fields(0) <> "") Then
sql = sql & " PHN = """ & resultSet1.Fields("PHN") & """"
End If

If Not IsNull(resultSet1.Fields(1)) And (resultSet1.Fields(1) <> "") Then
sql = sql & " and Year = " & resultSet1.Fields(1)
End If

If Not IsNull(resultSet1.Fields(2)) And (resultSet1.Fields(2) <> "") Then
sql = sql & " and [Date of Referral to Thoracics] = " & resultSet1.Fields(2)
End If

If Not IsNull(resultSet1.Fields(3)) And (resultSet1.Fields(3) <> "") Then
sql = sql & " and [Date of Thoracics Consult] = " & resultSet1.Fields(3)
End If

If Not IsNull(resultSet1.Fields(4)) And (resultSet1.Fields(4) <> "") Then
sql = sql & " and [Date Thoracic Surgery Booked] = " & resultSet1.Fields(4)
End If

If Not IsNull(resultSet1.Fields(5)) And (resultSet1.Fields(5) <> "") Then
sql = sql & " and [Date of Thoracic Surgery] = " & resultSet1.Fields(5)
End If

If Not IsNull(resultSet1.Fields(6)) And (resultSet1.Fields(6) <> "") Then
sql = sql & " and [Study Group] = """ & resultSet1.Fields(6) & """"
End If

If Not IsNull(resultSet1.Fields(7)) And (resultSet1.Fields(7) <> "") Then
sql = sql & " and [Access Method] = """ & resultSet1.Fields(7) & """"
End If

If Not IsNull(resultSet1.Fields(8)) And (resultSet1.Fields(8) <> "") Then
sql = sql & " and Procedure = """ & resultSet1.Fields(8) & """"
End If

If Not IsNull(resultSet1.Fields(9)) And (resultSet1.Fields(9) <> "") Then
sql = sql & " and Site = """ & resultSet1.Fields(9) & """"
End If

If Not IsNull(resultSet1.Fields(10)) And (resultSet1.Fields(10) <> "") Then
sql = sql & " and [Procedure 2] = """ & resultSet1.Fields(10) & """"
End If

If Not IsNull(resultSet1.Fields(11)) And (resultSet1.Fields(11) <> "") Then
sql = sql & " and [Site 2] = """ & resultSet1.Fields(11) & """"
End If

If Not IsNull(resultSet1.Fields(12)) And (resultSet1.Fields(12) <> "") Then
sql = sql & " and [Primary site] = """ & resultSet1.Fields(12) & """"
End If

If Not IsNull(resultSet1.Fields(13)) And (resultSet1.Fields(13) <> "") Then
sql = sql & " and Grade = """ & resultSet1.Fields(13) & """"
End If

If Not IsNull(resultSet1.Fields(14)) And (resultSet1.Fields(14) <> "") Then
sql = sql & " and [T Stage] = """ & resultSet1.Fields(14) & """"
End If

If Not IsNull(resultSet1.Fields(15)) And (resultSet1.Fields(15) <> "") Then
sql = sql & " and [N Stage] = """ & resultSet1.Fields(15) & """"
End If

If Not IsNull(resultSet1.Fields(16)) And (resultSet1.Fields(16) <> "") Then
sql = sql & " and [M Stage] = """ & resultSet1.Fields(16) & """"
End If

If Not IsNull(resultSet1.Fields(17)) And (resultSet1.Fields(17) <> "") Then
sql = sql & " and [Same Staging?] = """ & resultSet1.Fields(17) & """"
End If

CurrentDb.Execute sql

resultSet1.MoveNext
Loop

resultSet1.Close
End Sub

这是我正在使用的代码,为了测试它是否有效,我一直在使用表删除删除副本,但只有135个中的大约20个即使一个记录是另一个记录的副本,记录也会被删除。

此外,我在创建删除查询时匹配所有字段,我感觉这就是问题的根源。

附注选项显式和选项比较数据库在此子上方声明

最佳答案

您尚未引用日期值,因此您正在进行除法运算:

DELETE ... WHERE [Date...] = 6/5/2013
which becomes
DELETE ... WHERE [Date...] = 0.0005961etc...

尝试

DELETE ... WHERE [Date...] = #6/5/2013#
^--------^

相反。

关于mysql - MS Access VBA 代码未删除所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com