So no, bad data wouldn't get saved anyway because you couldn't close the form while it still held unsaved data.
The lost focus event does not prevent you from closing the form if it is dirty and therefore saving the bad data. It simply prevents you from leaving the control if you code it that way. Please prove this to yourself if you don't believe me. I told you how. If your application didn't save bad data, it was because you coded your way around it because you had to because your validation code was in the wrong event. I'm using wrong as a fact here, not an opinion. Validation code in the Lost Focus event runs whether the record was updated or not - that in itself is a poor idea. Then, since you don't have a built in method in that event to tell Access to NOT save, you have to work around it. You need to code yourself out of the bag. Of course you can do it. I guess you didn't appreciate my little story from #21 about the 5,000!!!!!! completely useless lines of validation code. That developer to this day probably doesn't know that the BeforeUpdate (control and form) are the events that MS intended for us to use for validation when it created the Event procedure "hooks" where we could hang our code.
The Form's BeforeUpdate event is like the flapper on a funnel. If the flapper is open, the record gets saved. If the flapper is closed, it doesn't. PERIOD. Kinda' like your "old programmer rules". It's a binary rule and the Form's BeforeUpdate event can NEVER, EVER be bypassed. If you have code in it, it is always the last event that runs BEFORE the record gets saved. That means if the user closes the form with the record dirty, the BeforeUpdate event runs. If the user scrolls to a new record and the current record is dirty, the BeforeUpdate event runs. If the user presses your save button to initiate a save, the BeforeUpdate event runs. Therefore, in that event, you have complete control over whether or not a record gets saved. You don't need code in multiple other events where if you miss an event, your bad data slips through no matter how many error messages you raise. Error messages don't stop the bad data from being saved. There are valid reasons for wanting the validation code closer to the error. Hence, the control level BeforeUpdate event. You wanted to use fancy highlighting. OK, that is certainly useful to help the user to focus on the problem. When you built the application you didn't know how the Access event model was designed to be used. OK, you picked the LostFocus event. It didn't work but you persisted and finally put enough code in other events so that you made it work. Go back and look at that app and see how you could have simplified the process by using the two BeforeUpdate events instead of the ones you chose. Count the events you needed code in to prevent the bad data from being saved.
You are really digging in your heels to justify NOT using the events that MS intended we use for validation. I don't understand why. If I look back at some of my earlier applications, I cringe at the poor choices I made before I understood more about the event model. But I would never justify them. It feels like you think you are being personally attacked. Far from it. I am simply trying to add a little clarity to the Form's event model. Each event has an intended purpose. Some events can be used interchangeably on a limited level but there is always one "better" choice.
So step back a little, Pat, and allow for different viewpoints caused by different conditions. You know that every shop is the same and every shop is different. Vive le difference!
I am not arguing against visual clues or because I have a different opinion, I am simply pointing out that you can get the same effect by using the control's BeforeUpdate event. That lets you "skin the cat" in one step rather than five or however many you needed to code to actually trap the errors and prevent Access from saving the bad data.
Doc, keep in mind that beginners are reading this discussion. I am trying to keep THEM from making a poor choice when placing their validation code. They latch on to the worst solution as frequently as the best. Some advice is not simply a different opinion. Some advice is based on an actual better method. You are a competent programmer. You can code your way out of a poor choice.
When you use the correct event for your validation, all it takes to prevent bad data from being saved is:
Cancel = True
When you use the control's BeforeUpdate event, that line of code also prevents you from leaving the control.
You might find playing with my sample database at this link enlightening. I did when I built it. Discovered some very interesting things about events that I never realized before
I just added #2 --- that link is further down. It is much shorter than #1. I also added a copy of the database I used for the video. Enjoy:) I created a database to help people to understand how and why to use the Form's BeforeUpdate event to validate data. @Uncle Gizmo and I made a...
www.access-programmers.co.uk