Home > Enterprise >  DoCmd.CancelEvent / Cancel = True Usage
DoCmd.CancelEvent / Cancel = True Usage

Time:09-08

Can either of DoCmd.CancelEvent or Cancel = True be run with effect from a procedure called by an event procedure whose event can be cancelled? E.g.,

Private Sub mCbo_DblClick(Cancel As Integer)
    DoThis
End Sub

Private Sub DoThis()
    Foo
    { Cancel = True | DoCmd.CancelEvent }
    Bar
End Sub

I.e., will the Cancel = True / DoCmd.CancelEvent statement in DoThis() cancel the mCbo.DblClick event or is the statement effective only in the event procedure itself? The former would seem to be in scope in a broad sense, at least, but this isn't clear.

Secondarily, wherever the statement must appear, will subsequent code execute? I.e., will DoThis() execute Bar or will the statement effectively operate as Exit Sub, immediately ceasing any further execution?

This would seem straightforward but an hour or two Googling around hasn't turned up anything on point. Many thanks for any constructive thoughts.

CodePudding user response:

You can easily do this by passing the cancel parameter by reference:

Private Sub mCbo_DblClick(Cancel As Integer)
    DoThis Cancel
End Sub

Private Sub DoThis(ByRef Cancel As Integer)
    Foo
    Cancel = True
    Bar
End Sub

And, of course, Bar is still ran. You could use Exit Sub if you want to prevent it from running. The Cancel parameter is just an integer that's used after all event handlers have ran to determine if the default behaviour should be cancelled. Setting it in a procedure doesn't do anything immediate.

  • Related