I have some standard code I typically use in most forms. It just makes my “Save” and “Undo” buttons visible.
On some occasions, I’ve noticed that my buttons didn’t become visible, and actually the OnDirty event was never firing. I narrowed it down to it being when I was adding a new record vs. modifying an existing record. But this was not a routine occurance; it didn’t happen with 99% of my forms, but every once in a while I had a form were it was a problem. I was always able to create a work-around, but it just annoyed me.
I finally figured out the cause, and it may be something that others know about, but it was new to me.
In the few times I had this problem, I also had code in my Before Insert event. Not just any code, but code that set the value of a field in the record! Why this should be a problem seems logical to me now, but first I had to understand 2 concepts:
#1: Changing the value of a field in code will make the record dirty (if it isn’t already), but will not cause the OnDirty event to fire.
#2: The Before Insert event fires before the On Dirty event.
When I hit a keystroke on a “Clean” form, Access ran my Before Insert code. By changing a field in Before Insert, I made the form Dirty. But then when it came time to evaluate whether/not to fire the On Dirty event because of the keystroke, apparently Access said “The form was already dirty, so your keystroke didn’t dirty it! Therefore I won’t fire the On Dirty Event!”.