如果我将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数据库引擎对象库。
答案 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