CurrentDb.RecordsAffected returns 0. Why?

If I use RecordAffected with CurrentDb.Execute, it always returns 0. If I first create an instance of the database object, it works correctly. 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:

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

I am using Access 2007 and the Microsoft Office 12.0 Access database object library.

+6
ms-access ms-access-2007 dao
source share
2 answers

Every time you use CurrentDB, this is a new instance.

+12
source share

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 
+1
source share

All Articles