



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Executes the specified query, SQL statement, stored procedure, or provider-specific text. Syntax For a non–row-returning command string: connection .Execute CommandText , RecordsAffected , Options For a row-returning command string: Set recordset = connection .Execute ( CommandText , RecordsAffected , Options ) Return Value Returns a Recordset object reference. Parameters CommandText A String value that contains the SQL statement, table name, stored procedure, a URL, or provider-specific text to execute. RecordsAffected Optional. A Long variable to which the provider returns the number of records that the operation affected. Options Optional. A Long value that indicates how the provider should evaluate the CommandText argument. Can be one or more CommandTypeEnum or ExecuteOptionEnum values. Remarks Using the Execute method on a Connection object executes whatever query you pass to the method in the CommandText argument on the specified connection. If the CommandText argument specifies a row-returning query, any results that the execution generates are stored in a new Recordset object. If the command is not a row-returning query, the provider returns a closed Recordset object. The returned Recordset object is always a read-only, forward-only cursor. If you need a Recordset object with more functionality, first create a Recordset object with the desired property settings, then use the Recordset object's Open method to execute the query and return the desired cursor type.
The contents of the CommandText argument are specific to the provider and can be standard SQL syntax or any special command format that the provider supports. An ExecuteComplete event will be issued when this operation concludes.
This example demonstrates the Execute method when run from both a Command object and a Connection object. It also uses the Requery method to retrieve current data in a Recordset, and the Clear method to clear the contents of the Errors collection. (The Errors collection is accessed via the Connection object of the ActiveConnection property of the Recordset.) The ExecuteCommand and PrintOutput procedures are required for this procedure to run. Public Sub ExecuteX() Dim strSQLChange As String Dim strSQLRestore As String Dim strCnn As String Dim cnn1 As ADODB.Connection Dim cmdChange As ADODB.Command Dim rstTitles As ADODB.Recordset Dim errLoop As ADODB.Error ' Define two SQL statements to execute as command text. strSQLChange = "UPDATE Titles SET Type = " & _ "'self_help' WHERE Type = 'psychology'" strSQLRestore = "UPDATE Titles SET Type = " & _ "'psychology' WHERE Type = 'self_help'" ' Open connection. strCnn = "Provider=sqloledb;" & _ "Data Source=srv;Initial Catalog=Pubs;User Id=sa;Password=; " Set cnn1 = New ADODB.Connection cnn1.Open strCnn ' Create command object. Set cmdChange = New ADODB.Command Set cmdChange.ActiveConnection = cnn cmdChange.CommandText = strSQLChange ' Open titles table. Set rstTitles = New ADODB.Recordset rstTitles.Open "titles", cnn1, , , adCmdTable ' Print report of original data. Debug.Print _ "Data in Titles table before executing the query" PrintOutput rstTitles ' Clear extraneous errors from the Errors collection. cnn1.Errors. Clear
' Notify user of any errors that result from ' executing the query. If rstTemp.ActiveConnection.Errors.Count > 0 Then For Each errLoop In Errors MsgBox "Error number: " & errLoop.Number & vbCr & _ errLoop.Description Next errLoop End If Resume Next End Sub Public Sub PrintOutput(rstTemp As ADODB.Recordset) ' Enumerate Recordset. Do While Not rstTemp.EOF Debug.Print " " & rstTemp!Title & _ ", " & rstTemp!Type rstTemp.MoveNext Loop End Sub
These transaction methods manage transaction processing within a Connection object as follows: BeginTrans — Begins a new transaction. CommitTrans — Saves any changes and ends the current transaction. It may also start a new transaction. RollbackTrans — Cancels any changes made during the current transaction and ends the transaction. It may also start a new transaction. Syntax level = object .BeginTrans() object .BeginTrans object .CommitTrans object .RollbackTrans Return Value BeginTrans can be called as a function that returns a Long variable indicating the nesting level of the transaction. Parameters object A Connection object. Connection Use these methods with a Connection object when you want to save or cancel a series of changes made to the source data as a single unit. For example, to transfer money between accounts, you subtract an amount from one and add the same amount to the other. If either update fails, the accounts no longer balance. Making these changes within an open transaction ensures that either all or none of the changes go through. Note Not all providers support transactions. Verify that the provider-defined property " Transaction DDL " appears in the Connection object's Properties collection, indicating that the provider supports transactions. If the provider does not support transactions, calling one of these methods will return an error.