Runing Access Queries

My favorite method for executing SQL, be it saved queries or raw SQL, is the Execute method of the Database object. You can execute a statement with a single line, like this:

<br />
  CurrentDb.Execute &#8220;UPDATE titles SET price = price * 1.10&#8243;<br />

In this case, CurrentDB references the currently open database. Alternatively, you can create and load a Database object variable, which will allow you to accomplish more with this method. After setting the variable, you can execute a query, supply additional options and even read the number of rows affected. While this takes a few more lines, it is by no means verbose:

<br />
  Dim dbs As DAO.Database, sql as String, iCount as Integer<br />
  Set dbs = CurrentDb<br />
  sql = &#8220;DELETE * FROM stores WHERE state=&#8217;WY&#8217;&#8221;<br />
  &#8216;(not that there&#8217;s anything wrong with Wyoming!)<br />
  dbs.Execute sql, dbFailOnError<br />
  iCount = dbs.RecordsAffected <br />

  • Share/Bookmark

Leave a Comment