我正在尝试删除ARP中的所有多条记录,但我无法弄清楚为什么这不起作用。当我在ACCESS中运行时,我收到错误3075:
“查询中的日期语法错误'ARPs.ARPNR ='CLU7BG'AND ARPs.Änderungsdatum< #29.07.2016 12:21:42'“
感谢您提供的任何帮助。
Dim strSQL As String
Dim allMult As DAO.Recordset
strSQL = "SELECT ARPs.ARPNR, ARPs.Änderungsdatum FROM ARPs WHERE ARPs.BTNR IN (SELECT BTNR FROM ARPs GROUP BY BTNR HAVING COUNT(*) > 1);"
Set allMult = CurrentDb.OpenRecordset(strSQL)
With allMult
'Check that the Recordset isn't empty
If Not .BOF And Not .EOF Then
.MoveLast
.MoveFirst
'Loop until we reach the end of the Recordset
While (Not allMult.EOF)
'Delete all except the newest multiple records
strSQL = "DELETE * FROM ARPs WHERE ARPs.ARPNR = '" & allMult.Fields("ARPNR") & "' AND ARPs.Änderungsdatum < #" & allMult.Fields("Änderungsdatum") & "# ;"
CurrentDb.Execute strSQL, dbFailOnError
.MoveNext
Wend
End If
'close the Recordset
.Close
End With
'release memory
Set allMult = Nothing
答案 0 :(得分:2)
日期值需要格式正确的字符串表达式:
strSQL = "DELETE * FROM ARPs WHERE ARPs.ARPNR = '" & allMult.Fields("ARPNR") & "' AND ARPs.Änderungsdatum < #" & Format(allMult.Fields("Änderungsdatum").Value, "yyyy\/mm\/dd") & "# ;"
答案 1 :(得分:0)
假设您的allMult.Fields(“Änderungsdatum”)是一个Date,那么您可以使用QueryDef来传递参数。在这种情况下,您无需担心格式化。
With CurrentDb.CreateQueryDef("", _
"DELETE FROM ARPs WHERE ARPNR = prm0 AND Änderungsdatum < prm1")
.Parameters("prm0") = allMult.Fields("ARPNR")
.Parameters("prm1") = allMult.Fields("Änderungsdatum")
.Execute
.Close
End With
使用参数查询是一个很好的习惯。在SQL字符串中包含字符串以供执行通常是不好的做法。谷歌SQL注入找出原因。