ADO Event Instantiation by Language

See Also   

Each programming language creates instances of ADO events differently. All the examples that follow create a ConnectComplete event handler.

Visual Basic

Dim WithEvent connEvent as Connection
Dim conn as New Connection
set connEvent = conn      'Turn on event support.
conn.Open(...)
...
set connEvent = Nothing   'Turn off event support.
...
Private Sub connEvent_ConnectComplete(ByVal err as ADODB.Error, adStatus as ADODB.EventStatus, ByVal pConnectionas ADODB.Connection)
'Check the error object only if adStatus equals adStatusErrorsOccurred.
...
End Sub

Visual C++

This is a schematic description of how to instantiate ADO events in VC++.

Create classes derived from the ConnectionEventsVt and RecordsetEventsVt interfaces found in file adoint.h.

class CConnEvent : public ConnectionEventsVt
{
   public:
   STDMETHODIMP InfoMessage( 
            ADOError *pError,
            EventStatusEnum *adStatus,
           _ADOConnection *pConnection);
...
}

class CRstEvent : public RecordsetEventsVt 
{
    public:
       STDMETHODIMP WillChangeField( 
             LONG cFields,
             VARIANT Fields,
             EventStatusEnum *adStatus,
             _ADORecordset *pRecordset);
...
}

Implement each of the event handler methods in both classes. It's sufficient that each method merely return an HRESULT of S_OK. However, when you make it known that your event handlers are available, they will be called continuously by default. Instead, you may want to request no further notification after the first time by setting adStatus to adStatusUnwantedEvent.

STDMETHODIMP CConnEvent::ConnectComplete(
             ADOError *pError,
             EventStatusEnum *adStatus,
             _ADOConnection *pConnection) 
      {
      *adStatus = adStatusUnwantedEvent;
      return S_OK;
      }

The event classes inherit from IUnknown, so you must also implement the QueryInterface, AddRef and Release methods. Also implement class constructors and destructors. Choose the VC++ tools with which you are most comfortable that will simplify this part of the task.

Make it known that your event handlers are available by issuing QueryInterface on the Recordset and Connection objects for the IConnectionPointContainer and IConnectionPoint interfaces. Then issue IConnectionPoint::Advise for each class.

For example, assume you're in a Boolean function that returns True if it successfully informs a Recordset object that you have event handlers available.

HRESULT   hr;
DWORD      dwEvtClass;
IConnectionPointContainer   *pCPC = NULL;
IConnectionPoint            *pCP = NULL;
CRstEvent                  *pRStEvent = NULL;
...
_RecordsetPtr    pRs();
pRs.CreateInstance(__uuidof(Recordset));
pRStEvent = New CRstEvent();
if (pRStEvent == NULL) return FALSE;
...
hr = pRs->QueryInterface(IID_IConnectionPointContainer, &pCPC);
if (FAILED(hr)) return FALSE;
hr = pCPC->FindConnectionPoint(IID_ADORecordsetEvents, &pCP);
pCPC->Release();   // Always Release now, even before checking.
if (FAILED(hr)) return FALSE;
hr = pCP->Advise(pRstEvent, &dwEvtClass);   //Turn on event support.
pCP->Release();
if (FAILED(hr)) return FALSE;
...
return TRUE;
...

At this point, events for the RecordsetEvent family are turned on and your methods will be called as Recordset events occur.

Later, when you want to make your event handlers unavailable, get the connection point again and issue the IConnectionPoint::UnAdvise method.

...
hr = pCP->UnAdvise(dwEvtClass);   //Turn off event support.
pCP->Release();
if (FAILED(hr)) return FALSE;
...

Of course, you must release interfaces and destroy class objects as appropriate .

Visual J++

import wfc.data.*;
public class MyClass
{
ConnectionEventHandler handler = 
   new ConnectionEventHandler(this,"onConnectComplete");

public void onConnectComplete(Object sender,ConnectionEvent e)
{
   if (e.adStatus == AdoEnums.EventStatus.ERRORSOCCURRED) 
      System.out.println("Connection failed");
   else
      System.out.println("Connection completed");
   return;
}

void main( void )
{
   Connection conn = new Connection();

   conn.addOnConnectComplete(handler);      // Turn on event support.
   conn.open("DSN=pubs");
   conn.close();
   conn.removeOnConnectComplete(handler);   // Turn off event support.
}
}

VBScript

VBScript does not support events.