Delete 5 Records but RecordsAffected Property is 0

老子叫甜甜 提交于 2020-01-14 14:17:08

问题


Here is a code snippet:

Private Sub frmSearch_UnMarkAll()
Const sqlD As String = "DELETE * FROM PickList WHERE TableName = ""CARS"" AND KeyNo IN (" & _
"SELECT RecNo FROM Cars "
If frmSearch.WhereSql <> vbNullString Then
    CurrentDb.Execute sqlD & frmSearch.WhereSql & ")"
End If
Stop
End Sub

This function is called by clicking a command button on the frmSearch form. The button raises an event that is handled by the above code. WhereSql is the where clause from the listbox displayed on the find screen. It has a value like "WHERE Cars.RecNo > 1441" When the code reached the Stop statement, I checked the RecordsAffected property by using the immediate window and typing:

? Currentdb.RecordsAffected 
 0 is the result.

I checked the PickList table and the appropriate items were deleted.

My question: Is this a bug? Or, is it normal that deleting a record does not affect it?

I have gotten around the problem. I removed the SequenceNo field from the PickList table. I created and index on TableName and KeyNo in the PickList table and specified Unique as yes. Now I just

INSERT INTO PickList (TableName,LastChangedDate,KeyNo)
Values(tablename,now(),recno from the tablenamed)

If the INSERT fails due to error 3022, it is an attempt at duplicate entry which I ignore in this example. Everything else functions the way I wanted to function in the first place. The RecordsAffected property is always zero when I test it in my code or view by printing in the debugger. That is still perplexing because if you run action queries from access interactively, all that information is presented. If you run other relational DBMS's (the ones that are standard) there is always feedback when you use the query engine. I find it hard to believe that others are not failing if this is a bug in access. I am inclined to think I screwed up somehow. Hopefully, I am not the lone pioneer. Thanks, in advance for looking at this.


回答1:


See the answer here for explanation of what is going wrong. In effect you are not referring to the same object when you obtain the RecordsAffected property. You need to set explicit reference and use in both cases. Something like:

dim db as dao.database
set db=currentdb
db.execute "some SQL statement", dbfailonerror
debug.print db.recordsaffected


来源:https://stackoverflow.com/questions/36610395/delete-5-records-but-recordsaffected-property-is-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!