CurrentDb.RecordsAffected返回0.为什么?

时间:2010-06-09 08:30:14

标签: ms-access ms-access-2007 dao

如果我将RecordsAffected与CurrentDb.Execute一起使用,它总是返回0.如果我首先创建一个Database对象的实例,它就能正常工作。为什么呢?

像这样:

Dim Db As Database
Set Db = CurrentDb

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

而不是:

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

我正在使用Access 2007和Microsoft Office 12.0 Access数据库引擎对象库。

2 个答案:

答案 0 :(得分:15)

每次使用CurrentDB时,它都是一个新实例。

答案 1 :(得分:3)

使用With。将您的代码更改为:

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