CurrentDb.RecordsAffected returns 0. Why?

孤街醉人 提交于 2020-01-02 01:30:11

问题


If I use RecordsAffected with CurrentDb.Execute, it always returns 0. If I first make a instance of a Database object it works properly. Why?

Like this:

Dim Db As Database
Set Db = CurrentDb

Db.Execute "DELETE * FROM [Samples] WHERE Sample=5"
If Db.RecordsAffected = 0 Then
  MsgBox "Error"
End If

Instead of:

CurrentDb.Execute "DELETE * FROM [Samples] WHERE Sample=5"
If CurrentDb.RecordsAffected = 0 Then
  MsgBox "Error"
End If

I'm using Access 2007 and the Microsoft Office 12.0 Access database engine Objects Library.


回答1:


Each time you use CurrentDB, it is a new instance.




回答2:


Use With. Change your code to:

Dim Db As Database
Dim recordAffect = Integer
Set Db = CurrentDb
With Db
  .Execute "DELETE * FROM [Samples] WHERE Sample=5"
  recordAffect = .RecordsAffected
  'If Db.RecordsAffected = 0 Then
  If (recordAffect = 0)  Then
     MsgBox "Error"
  End If
End With


来源:https://stackoverflow.com/questions/3004153/currentdb-recordsaffected-returns-0-why

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