--- title: "Execute, Requery, and Clear Methods Example (VBScript) | Microsoft Docs" ms.prod: sql ms.prod_service: connectivity ms.technology: connectivity ms.custom: "" ms.date: "01/19/2017" ms.reviewer: "" ms.topic: conceptual dev_langs: - "VB" helpviewer_keywords: - "Execute method [ADO], VBScript example" - "Clear method [ADO], VBScript example" - "Requery method [ADO], VBScript example" ms.assetid: 3a7bbf07-2fca-4892-95f4-eec93f2d5e91 author: MightyPen ms.author: genemi --- # Execute, Requery, and Clear Methods Example (VBScript) This example demonstrates the **Execute** method when run from both a [Command](../../../ado/reference/ado-api/command-object-ado.md) object and a [Connection](../../../ado/reference/ado-api/connection-object-ado.md) object. It also uses the [Requery](../../../ado/reference/ado-api/requery-method.md) method to retrieve current data in a [recordset](../../../ado/reference/ado-api/recordset-object-ado.md), and the [Clear](../../../ado/reference/ado-api/clear-method-ado.md) method to clear the contents of the [Errors](../../../ado/reference/ado-api/errors-collection-ado.md) collection. The ExecuteCommand and PrintOutput procedures are required for this procedure to run. Use the following example in an Active Server Page (ASP). To view this fully functional example, you must either have the data source AdvWorks.mdb (installed with the SDK samples) located at C:\Program Files\Microsoft Platform SDK\Samples\DataAccess\Rds\RDSTest\advworks.mdb or edit the path in the example code to reflect the actual location of this file. This is a Microsoft Access database file. Use **Find** to locate the file Adovbs.inc and place it in the directory you plan to use. Cut and paste the following code into Notepad or another text editor, and save it as **ExecuteVBS.asp**. You can view the result in any client browser. ``` <%@ Language=VBScript %> <% ' use this meta tag instead of ADOVBS.inc%> ADO Execute Method

ADO Execute Method


Recordset Retrieved Using Connection Object

<% ' connection, command and recordset variables Dim Cnxn, strCnxn Dim rsCustomers, strSQLCustomers Dim Cmd Dim rsProducts, strSQLProducts ' create and open connection Set Cnxn = Server.CreateObject("ADODB.Connection") strCnxn="Provider='sqloledb';Data Source=" & _ Request.ServerVariables("SERVER_NAME") & ";" & _ "Integrated Security='SSPI';Initial Catalog='Northwind';" Cnxn.Open strCnxn ' create and open recordset Set rsCustomers = Server.CreateObject("ADODB.Recordset") strSQLCustomers = "Customers" rsCustomers.Open strSQLCustomers, Cnxn, adOpenKeyset, adLockOptimistic, adCmdTable '1st Recordset using Connection - Execute Set rsCustomers = Cnxn.Execute(strSQLCustomers) Set Cmd = Server.CreateObject("ADODB.Command") Cmd.ActiveConnection = Cnxn strSQLProducts = "SELECT * From Products" Cmd.CommandText = strSQLProducts '2nd Recordset Cmd - execute Set rsProducts = Cmd.Execute %> <% Do While Not rsCustomers.EOF %> <% rsCustomers.MoveNext Loop %>
Company Name Contact Name City
<%= rsCustomers("CompanyName")%> <%= rsCustomers("ContactName") %> <%= rsCustomers("City")%>

Recordset Retrieved Using Command Object

<% Do Until rsProducts.EOF %> <% rsProducts.MoveNext Loop ' clean up If rsCustomers.State = adStateOpen then rsCustomers.Close End If If rsProducts.State = adStateOpen then rsProducts.Close End If If Cnxn.State = adStateOpen then Cnxn.Close End If Set Cmd = Nothing Set rsCustomers = Nothing Set rsProducts = Nothing Set Cnxn = Nothing %>
Product Name Unit Price
<%= rsProducts("ProductName")%> <%= rsProducts("UnitPrice")%>
``` ## See Also [Clear Method (ADO)](../../../ado/reference/ado-api/clear-method-ado.md) [Command Object (ADO)](../../../ado/reference/ado-api/command-object-ado.md) [Connection Object (ADO)](../../../ado/reference/ado-api/connection-object-ado.md) [Error Object](../../../ado/reference/ado-api/error-object.md) [Errors Collection (ADO)](../../../ado/reference/ado-api/errors-collection-ado.md) [Execute Method (ADO Command)](../../../ado/reference/ado-api/execute-method-ado-command.md) [Execute Method (ADO Connection)](../../../ado/reference/ado-api/execute-method-ado-connection.md) [Recordset Object (ADO)](../../../ado/reference/ado-api/recordset-object-ado.md) [Requery Method](../../../ado/reference/ado-api/requery-method.md)