VB6 COM Callbacks

Created 08 January 2001 00:00, updated 06 June 2017 20:14
COM components using asynchronous callbacks must provide an interface through which the component will make the callback. The client program must then create an object using the interface and pass it into a method of the component. When the component completes its processing, it calls a method of the object that was passed in to perform the notification.

1. A notification interface for validating an employee number (called EG.Notify) :

  Option Explicit
  Public Sub ValidateReturned(validated as Boolean)
  End Sub
2. The component actually performing the validation (called EG.Employee):
  Option Explicit
  Public objNotify As EG.Notify

  Public Sub ValidateEmployee(empID As Long, objClient As EG.Notify)
    Set objNotify = objClient
    Call DoValidation
  End Sub

  Private Sub DoValidation()
    ' do validation of employee here
    ' finished - call the client
    objNotify.ValidateReturned True
  End Sub
3. class module to implement the callback (called EmpNotifier):
  Option Explicit
  Implements EG.Notify

  Private Sub Notify_ValidateReturned(validated as Boolean)
    ' the process is complete
    If NOT validated Then
      MsgBox "The employee number could not be successfully validated."
    End If
  End Sub
4. Using the interface requires creating two objects - one which actual does the validation and one to handle the notification of the validation:
  Option Explicit
  Private objEmployee As New EG.Employee
  Private objNotify As New EmpNotifier

  Private Sub Form_Load()
    objEmployee.ValidateEmployee 123456, objNotify
  End Sub