Microsoft KB Archive/172082

= How To Handle RDO 2.0 Asynchronous Processing WithEvents =

Article ID: 172082

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q172082



SUMMARY
RDO 2.0 gives you the ability to handle asynchronous processing more elegantly than RDO 1.0. Because Visual Basic allows objects to be dimensioned WithEvents, you can use the events of the object as if it were a visual control, such as a textbox, rather than constantly referring to properties such as StillExecuting.



MORE INFORMATION
This project is an example of dimensioning object's WithEvents using the events of RDO 2.0's rdoConnection object. Be sure to change the connect string and query string to correctly connect to and query your database.

The sample establishes an asynchronous connection to the database and runs an asynchronous query. When the asynchronous calls are made, the CommandButton on the form is disabled. The cbton is then re-enabled in the rdoConnection's QueryComplete and Connect event. This code is more elegant than using a timer to monitor the StillExecuting property repeatedly.

In the code below, note that the rdoConnection object variable MyConn is treated almost like a visual control. From the code window, the MyConn object variable is available from the dropdown list of available objects.

Step-by-Step Example
 Begin by opening a new project and adding a reference to the Microsoft Remote Data Object 2.0. To add a reference, select References from the Project menu. Place a CommandButton on the form and set its Name property to cmdOpenResultset.  Add the following code to the form. Be sure to modify the gstrConnect constant accordingly to make sure that RDO will connect to your database: Const gstrConnect As String = "DRIVER={SQL Server};" & _ "SERVER=YourServer;" & _ "DATABASE=pubs;" & _ "UID= ;" & _ "PWD= ;" Const gstrSQL As String = "SELECT * FROM Authors"

Dim WithEvents MyConn As rdoConnection Dim MyRS As rdoResultset

Private Sub Form_Load Set MyConn = New rdoConnection MyConn.Connect = gstrConnect MyConn.CursorDriver = rdUseOdbc MyConn.EstablishConnection rdDriverNoPrompt, _ False, _ rdAsyncEnable cmdOpenResultset.Enabled = False Caption = "Opening Connection..." End Sub

Private Sub cmdOpenResultset_Click Set MyRS = MyConn.OpenResultset(gstrSQL, _                                         rdOpenStatic, _                                          rdConcurReadOnly, _                                          rdExecDirect + rdAsyncEnable) cmdOpenResultset.Enabled = False Caption = "Running Query..." End Sub

Private Sub MyConn_Connect(ByVal ErrorOccurred As Boolean) If ErrorOccurred Then Caption = "Error Occurred!" Else Caption = "Connection Established!" cmdOpenResultset.Enabled = True End If     End Sub

Private Sub MyConn_QueryComplete(ByVal Query As RDO.rdoQuery, _                                      ByVal ErrorOccurred As Boolean) IF ErrorOccured Then Caption = "Error Occurred!" Else Caption = "Query Complete!" cmdOpenResultset.Enabled = True End IF     End Sub

 Run the project.

