Connection.Execute Method (DAO)

Office 2013 and later

Last modified: March 09, 2015

Applies to: Access 2013 | Office 2013

Runs an action query or executes an SQL statement on the specified object.

expression .Execute(Query, Options)

expression A variable that represents a Connection object.

Parameters

Name

Required/Optional

Data Type

Description

Query

Required

String

A String that is an SQL statement or the Name property value of a QueryDef object.

Options

Optional

Variant

A constant or combination of constants that determines the data integrity characteristics of the query, as specified in Settings.

You can use the following RecordsetOptionEnumconstants for options.

Constant

Description

dbDenyWrite

Denies write permission to other users (Microsoft Access workspaces only).

dbInconsistent

(Default) Executes inconsistent updates (Microsoft Access workspaces only).

dbConsistent

Executes consistent updates (Microsoft Access workspaces only).

dbSQLPassThrough

Executes an SQL pass-through query. Setting this option passes the SQL statement to an ODBC database for processing (Microsoft Access workspaces only).

dbFailOnError

Rolls back updates if an error occurs (Microsoft Access workspaces only).

dbSeeChanges

Generates a run-time error if another user is changing data you are editing (Microsoft Access workspaces only).

dbRunAsync

Executes the query asynchronously (ODBCDirect Connection and QueryDef objects only).

dbExecDirect

Executes the statement without first calling SQLPrepare ODBC API function (ODBCDirect Connection and QueryDef objects only).

Note Note

ODBCDirect workspaces are not supported in Microsoft Access 2013. Use ADO if you want to access external data sources without using the Microsoft Access database engine.

Note Note

The constants dbConsistent and dbInconsistent are mutually exclusive. You can use one or the other, but not both in a given instance of OpenRecordset. Using both dbConsistent and dbInconsistent causes an error.

The Execute method is valid only for action queries. If you use Execute with another type of query, an error occurs. Because an action query doesn't return any records, Execute doesn't return a Recordset. (Executing an SQL pass-through query in an ODBCDirect workspace will not return an error if a Recordset isn't returned.)

Use the RecordsAffected property of the Connection, Database, or QueryDef object to determine the number of records affected by the most recent Execute method. For example, RecordsAffected contains the number of records deleted, updated, or inserted when executing an action query. When you use the Execute method to run a query, the RecordsAffected property of the QueryDef object is set to the number of records affected.

In a Microsoft Access workspace, if you provide a syntactically correct SQL statement and have the appropriate permissions, the Execute method won't fail — even if not a single row can be modified or deleted. Therefore, always use the dbFailOnError option when using the Execute method to run an update or delete query. This option generates a run-time error and rolls back all successful changes if any of the records affected are locked and can't be updated or deleted.

In earlier versions of the Microsoft Jet database engine, SQL statements were automatically embedded in implicit transactions. If part of a statement executed with dbFailOnError failed, the entire statement would be rolled back. To improve performance, these implicit transactions were removed starting with version 3.5. If you are updating older DAO code, be sure to consider using explicit transactions around Execute statements.

For best performance in a Microsoft Access workspace, especially in a multiuser environment, nest the Execute method inside a transaction. Use the BeginTrans method on the current Workspace object, then use the Execute method, and complete the transaction by using the CommitTrans method on the Workspace. This saves changes on disk and frees any locks placed while the query is running.

Show: