Microsoft KB Archive/142925

= How To Cancel an Asynchronous Query--RDO =

Article ID: 142925

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Enterprise Edition

-



This article was previously published under Q142925



SUMMARY
After executing an asynchronous query with the OpenResultset method, program control continues to the next line of code while the query is executing.

Later in your code, you can decide to cancel the asynchronous query by using the Cancel method of the rdoResultset.



Sample Program
This example assumes that you already have an ODBC database server available and that you have an SQL query that runs long enough to make a cancel operation practical. This example uses a "DSN-less" ODBC connection, so you do not need to set up a DSN with the ODBC Admin utility.

 Start a new project in Visual Basic. Form1 is created by default. Add two command buttons to Form1. Change the caption property of Command1 to Begin and the caption property of Commmand2 to Cancel.  Paste the following code into the General Declarations section of form1: Option Explicit Dim cn As rdoConnection Dim en As rdoEnvironment Dim rs As rdoResultset Private Sub Form_Load Command1.Enabled = True Command2.Enabled = False 'Establish connection. Set en = rdoEngine.rdoEnvironments(0) en.CursorDriver = rdUseOdbc 'This should be modified to connect to your database. Dim cnStr As String cnStr = "driver={SQL Server};server=mysvr;" & _ "database=pubs;uid=myuid;pwd=mypwd" Set cn = en.OpenConnection(dsname:="", Prompt:=rdDriverNoPrompt, _          Connect:=cnStr) End Sub

Private Sub Command1_Click Dim sql As String 'Change this to an SQL statement that takes at least a few 'seconds to finish, such as a Cartesian join or a text 'column search, i.e. Where textcolumn Like '*find this text*'. sql = "Select title From Titles" Command1.Enabled = False Command2.Enabled = True Set rs = cn.OpenResultset(Name:=sql, Type:=rdOpenStatic, _         Option:=rdAsyncEnable) While rs.StillExecuting DoEvents ' Allow user to click on Cancel command button. Wend Command1.Enabled = True Command2.Enabled = False End Sub

Private Sub Command2_Click rs.Cancel End Sub

  Note that you need to change your DRIVER, SERVER, DATABASE, UID, and PWD in the OpenConnection function contained in the Form_Load event. You also need to modify the SQL statement contained in the Command1_Click event to match your own SQL data source and to be complex enough to make this example a practical use of the cancel operation. A good example is a Cartesian join or a text column search: Select * from mytable Where textcolumn Like '*find this text*'

 Start the program or press the F5 key. You can then click the Begin Command button to start the query and the Cancel Command button to cancel the currently running query while it is in operation.</ol>

<div class="references_section">