Using Beforeupdate Cancel

jaydwest

JayW
Local time
Today, 02:10
Joined
Apr 22, 2003
Messages
340
I would like to call a control's BeforeUpdate Event from a button. Here's my problem, if the BeforeUpdate event is cancelled, I would like to bypass the rest of the code on the button, if not I would like to run the rest of the code.

The BeforeUpdate is not a function so it doesn't return a value and I can't seem to find an access variable (e.g. acCancel) that is set that I can get my grubby paws on.

I can create a global or form cancel variable and set it in the BeforeUpdate event. Does anyone have a more elegant solution?

Thanks for your thoughts.
 
If I’m reading this correctly (probably not) I think you are going to have some trouble achieving this the way you envisage.

There are two different things, an event and the code that it calls. Simply calling the BeforeUpdate code does not trigger the event, nor should it. Consider the normal course of events. An event happens that calls some code; now if the calling of the code causes the event then the event would call the code again which…you’d probably get an out of stack space error message.

Simply calling the BeforeUpdate code does not trigger the event and therefore the event has not occurred and so there is no event to cancel.

On the other hand, if you are not expecting to cancel the event, that did not happen, but simply want to reuse the same BeforeUpdate code then the following should work: -

Code:
Private Sub cmdMyButton_Click()
    Dim intDummyCancel As Integer

    txtMyTextBox_BeforeUpdate intDummyCancel
    
    If Not intDummyCancel Then
        MsgBox "Running some code."
    End If

End Sub


Private Sub txtMyTextBox_BeforeUpdate(Cancel As Integer)

    Cancel = True

End Sub

Hope that helps.

Regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom