CreateEventProc Method

Microsoft Access Visual Basic

expression.CreateEventProc(EventName, ObjectName)

expression    Required. An expression that returns one of the objects in the Applies To list.

EventName   Required String. A string expression that evaluates to the name of an event.

ObjectName   Required String. An object that has the event specified by the eventname    argument. It may be a Form, Report, or Control object, a form or report section, or a class module.

Remarks

The CreateEventProc method creates a code stub for an event procedure for the specified object. For example, you can use this method to create a Click event procedure for a command button on a form. Microsoft Access creates the Click event procedure in the module associated with the form that contains the command button.

Once you've created the event procedure code stub by using the CreateEventProc method, you can add lines of code to the procedure by using other methods of the Module object. For example, you can use the InsertLines method to insert a line of code.

Example

The following example creates a new form, adds a command button, and creates a Click event procedure for the command button:

Function ClickEventProc() As Boolean
    Dim frm As Form, ctl As Control, mdl As Module
    Dim lngReturn As Long

    On Error GoTo Error_ClickEventProc
    ' Create new form.
    Set frm = CreateForm
    ' Create command button on form.
    Set ctl = CreateControl(frm.Name, acCommandButton, , , , _
         1000, 1000)
    ctl.Caption = "Click here"
    ' Return reference to form module.
    Set mdl = frm.Module
    ' Add event procedure.
    lngReturn = mdl.CreateEventProc("Click", ctl.Name)
    ' Insert text into body of procedure.
    mdl.InsertLines lngReturn + 1, vbTab & "MsgBox ""Way cool!"""
    ClickEventProc = True

Exit_ClickEventProc:
    Exit Function

Error_ClickEventProc:
    MsgBox Err & " :" & Err.Description
    ClickEventProc = False
    Resume Exit_ClickEventProc
End Function