2010-06-09 22 views
6

Jeśli używam RecordsAffected z CurrentDb.Execute, zawsze zwraca on wartość 0. Jeśli po raz pierwszy wykonam wystąpienie obiektu bazy danych, działa on poprawnie. Czemu?CurrentDb.RecordsAffected zwraca 0. Dlaczego?

Jak to:

Dim Db As Database 
Set Db = CurrentDb 

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

Zamiast:

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

Używam Access 2007, a silnik bazy danych 12.0 Dostęp Microsoft Office Objects Library.

Odpowiedz

12

Przy każdym użyciu CurrentDB jest to nowa instancja.

1

Użyj With. Zmień swój kod na:

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 
Powiązane problemy