Sunday, December 12, 2010

DAO RecordsAffected property

RecordsAffected property returns the number of records affected by the most recently invoked Execute method. Can be useful when you need to update a record in a table, and if record not exists – add it. Normally this is solved by opening recordset and using NoMatch property to test if record exists. Using RecordsAffected property you can do this in 3 lines:

Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Execute "UPDATE Sales SET Amount=Amount + " & _
varAmount & " Where SalesDate=" & varDate, dbFailOnError
If dbs.RecordsAffected = 0 Then
dbs.Execute "INSERT INTO Sales (Amount, SalesDate) Values(" _ & varAmount & ", " & varDate & ")", dbFailOnError
End If

No comments:

Post a Comment