RaiseEvent Statement

Named Arguments

No

Syntax

RaiseEvent eventName [arglist]

eventName

Use: Required

Data Type: String

The name of the event.

arglist

Use: Optional

Data Type: Any (defined by the Event statement)

A comma-delimited list of variables.

Description

Generates a predefined custom event within any procedure of an object module.

Rules at a Glance

  • eventName must already be defined in the Declarations section of the module using the Event statement.

  • arglist must match the number and data type of parameters defined in the Event statement.

  • The RaiseEvent and Event statements can be used only in object modules—i.e., in form and class modules—and not in code modules.

Example

The following snippet demonstrates how you can use an event to communicate a status message back to the client application, and at the same time use a ByRef argument to trap a user response in the client application. This gets around the fact that events can't return values. To take advantage of this functionality, the client must declare a reference to this class using the WithEvents keyword.

Public Event Status(Message As String, _
                    ByRef Cancel As Boolean)

Private Function UpdateRecords(iVal As Integer) as Boolean
    Dim blnCancel As Boolean
    ...
    If iVal > 1000 Then
        RaiseEvent Status "Is value too high?", blnCancel
        If blnCancel Then
            Exit Function
        End If
    End If
    ...
End Function

Programming Tips and Gotchas

  • To allow the client application to handle the event being fired, the object variable must be declared ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.