CurrentDb.RecordsAffected returns 0. Why?
Asked Answered
A

2

9

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.

Aulos answered 9/6, 2010 at 8:30 Comment(0)
C
17

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

Copley answered 9/6, 2010 at 9:24 Comment(0)
L
4

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
Linger answered 29/11, 2015 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.