none
Determining which control caused beforeupdate to fire RRS feed

  • Question

  • I have an unbound form with various textboxes that use the beforeupdate event for validation. Within the beforeupdate I have a custom message displayed to the user about how they need to fix their data, then I fire off an undo and their changes are undone while the focus remains on the same control. I also have a Cancel button on the form that if pressed I want to undo any changes on any of the controls and simply close  the form without showing the custom message about their invalid data. Here is my code and one line of pseudo code for what I am intending to do.

    Private Sub EMAIL_BeforeUpdate(Cancel as integer)

    If check_for_valid_email(Me.Email) = False then
       If screen.nextcontrol.name <> "cmdCancelButton" then   'Here's the sudo code line
          Msgbox("You have entered an invalid email address. Email addresses can only contain letters, numbers...")
       End if
       Me.EMAIL.Undo
    End If

    End Sub

    Thursday, May 4, 2017 3:26 PM

Answers

  • You would have to undertake the validation in the form's BeforeUpdate event procedure by stepping through the relevant controls, rather than in each individual control's BeforeUpdate event procedure.

    If you undo the edits with your cancel button, the form's BeforeUpdate event procedure will not be executed, so even if the user has violated any validation criteria, they will not be presented with any of the custom messages.

    Edit:  Just noticed that your form is unbound, so it's not a matter of undoing it in the same way as you would with a bound form.  Instead you'd assign Nulls to the controls.

    However, you cannot anticipate which control will receive focus when a value is inserted in a control  You'd still need to undertake the validation at form level.  With a bound form this would normally be done in whatever event procedure, e.g. a command button's Click event procedure, carries out whatever the purpose of the dialogue form is, by making the relevant code conditional on the validation criteria for all controls having been met.


    Ken Sheridan, Stafford, England


    • Edited by Ken Sheridan Thursday, May 4, 2017 6:02 PM
    • Marked as answer by ITLOUIS Thursday, May 4, 2017 6:28 PM
    Thursday, May 4, 2017 5:52 PM

All replies

  • Hi,

    Not sure I follow. The BeforeUpdate shouldn't fire if the user clicks on your cancel button if you use Me.Undo on it.

    Thursday, May 4, 2017 4:39 PM
  • You would have to undertake the validation in the form's BeforeUpdate event procedure by stepping through the relevant controls, rather than in each individual control's BeforeUpdate event procedure.

    If you undo the edits with your cancel button, the form's BeforeUpdate event procedure will not be executed, so even if the user has violated any validation criteria, they will not be presented with any of the custom messages.

    Edit:  Just noticed that your form is unbound, so it's not a matter of undoing it in the same way as you would with a bound form.  Instead you'd assign Nulls to the controls.

    However, you cannot anticipate which control will receive focus when a value is inserted in a control  You'd still need to undertake the validation at form level.  With a bound form this would normally be done in whatever event procedure, e.g. a command button's Click event procedure, carries out whatever the purpose of the dialogue form is, by making the relevant code conditional on the validation criteria for all controls having been met.


    Ken Sheridan, Stafford, England


    • Edited by Ken Sheridan Thursday, May 4, 2017 6:02 PM
    • Marked as answer by ITLOUIS Thursday, May 4, 2017 6:28 PM
    Thursday, May 4, 2017 5:52 PM
  • The BeforeUpdate on the textbox always fires as soon as the focus tries to move to the cancel button just pushed. In the end I'd like the validation to fire for each textbox and prevent bad data unless the cancel button was pushed and invalid data doesn't matter since it is not saved to the table.
    Thursday, May 4, 2017 5:55 PM
  • My editing of my previous reply crossed with your last reply.  The bottom line is that the validation will have to be done at form level to prevent the messages being shown.

    Ken Sheridan, Stafford, England

    Thursday, May 4, 2017 6:08 PM
  • That's what I was afraid of. I'd prefer the textbox be validated as the user leaves the textbox instead of when trying to save the entire form. I appreciate the help. Thanks.

     
    Thursday, May 4, 2017 6:28 PM