Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

How do you use .RecordsAffected

 Dim db As Database
  Set db = CurrentDb
   Dim queryDef As DAO.queryDef
   Set queryDef = db.CreateQueryDef("", SqlStr)
   debug.print queryDef.RecordsAffected -->gives zero  every time
  db.Execute (SqlStr), dbFailOnError --> the dbFailOnError seems to make the execute statement more picky about what it will let execute
  DaoExecuteSql = db.RecordsAffected --> gives back the number of records affected correctly

WTF is going on here?

1 Solution
Hmm. RecordsAffected work with action queries. For example:

    With CurrentDb
        .Execute "DELETE * FROM tblClients"
        MsgBox "You deleted " & .RecordsAffected & " Clients!"
    End With

In Conjuction with .BeginTrans and .CommitTrans / .RollBack, this allows messages in the form:

    12 records will be updated.
    Press [OK] to confirm or [Esc] to cancel

It also allow to decide wheter a refresh is needed:

    If .RecordsAffected Then Me.subForm.Requery

Finally, if you use dbFailOnError, it will not even attempt to run the query if there is any error. In the Delete example above, only clients who never placed an order would be deleted because of relational integrity. This is basically what you get when you run an action query from the database window...

Hope this sheds some light :)

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now