My question is how are control source property and afterupdate event related to each other in terms of order of occurence?Which one occurs first? I want to have a msgbox asking user to confirm if he would like to save the data he just entered in the textbox.The textbox has control source prpoerty set to some field.If the user cancels the action the text he entered should not get saved. What if I put the msgbox in the OnChange event of the textbox, wouldn't it be too early to prompt the user to confirm data entry? Note:the control source is a field of type date.
1 Answers
ControlSource isn't really directly related to the Change, BeforeUpdate or AfterUpdate events.
The event you need to use is the BeforeUpdate event.
Private Sub txtFirstName_BeforeUpdate(Cancel as Integer)
If MsgBox("Are you sure you want to save this data?", vbYesNo, "Um...") = vbNo Then
Cancel = True
End If
End Sub
I want to add that in most situations it's better to use the Form's BeforeUpdate event to validate things rather than doing it on the control's BeforeUpdate event, simply because it tends to be more user friendly. The Form's BeforeUpdate event works the same way as the code I posted above does.
Also, using MsgBox during validation routines is the way most people do it but it really isn't considered a good design practice. It would be better to have a textbox or label that you can use to output validation messages and then maybe play a sound as well so that the user knows that the data they entered isn't valid.