Event Order Reference that includes VBA exceptions or differences (1 Viewer)

JMongi

Active member
Local time
Today, 18:04
Joined
Jan 6, 2021
Messages
802
I have the following page bookmarked:


Is there a similar type of reference which incorporates the exceptions or differences when data is modified via VBA? (Edited for typos and clarity).
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:04
Joined
Feb 28, 2001
Messages
27,223
Unless you do something that affects the state of a form (like opening it or closing it or saving the record or triggering Maximize, Minimize, Restore), there are very few code-initiated steps that will trigger events. Single-control events typically don't fire based on VBA actions.
 

JMongi

Active member
Local time
Today, 18:04
Joined
Jan 6, 2021
Messages
802
@The_Doc_Man - Quite true! Which is why I would like a nice reference that explicitly spells that out for me. :D

Microsoft and I don't always agree on what makes "sense". That's fine. It's their ball afterall. I just am looking for a nice copy of the rules of the game.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:04
Joined
Feb 28, 2001
Messages
27,223
Hmmmm..... a sentence with "Microsoft" and "makes sense" in it.... I'll have to ponder that one.
;)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:04
Joined
Feb 19, 2002
Messages
43,352
Control level events do not fire when you use VBA to modify data in the control. That is only ONE of the reasons that I don't use Control level events to validate data. I always use the Form's BeforeUpdate event which ALWAYS fires REGARDLESS of what dirties a record or what causes the record to be saved. If I decide that I need immediate validation because I am going to prevent the record from being saved if a particular field is invalid, then I use a procedure that I call from both the Control's BeforeUpdate event and the Form's BeforeUpdate event to avoid having duplicate code that can get out of sync.

Here's a link to a video I made with @Uncle Gizmo Turns out that one of the best parts of the video is the form that stays open in the background and displays the events as they run in real time. I've been working with Access for many years and even I was surprised by some things I learned doing this video. At some point, and I'm not sure when, I will either release the sample database for free or for a small charge. To use it, you would import a couple of objects into your app and then add one line of code to each event in your form that you want to see fire. You can even leave the code in place because there is a flag that you can set to turn off the logging.

 

JMongi

Active member
Local time
Today, 18:04
Joined
Jan 6, 2021
Messages
802
Control level events do not fire when you use VBA to modify data in the control.
@Pat Hartman - Is that universally true? Meaning there is precisely 0 form control event procedures that can be triggered via VBA data change?

Edit: Just to head things off at the pass, I am NOT meaning you can't use various tricks and work arounds to get Access to do what you want. I am focused very specifically on the idea of programmatically setting data in a form control i.e. FormControl.Value = Newdata.

P.S. The video was nice. Thanks!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:04
Joined
Feb 28, 2001
Messages
27,223
There is nothing to stop you from doing that kind of value assignment. It just won't have any immediate side-effects due to events. Don't forget that using VBA you can do things to every data-centric control on a form. If you fired an event for each control change "behind the scenes" you would never get anything done. There is also this minor technical concept - that the only place where you can do a VBA change is already IN an event, and events cannot interrupt other events (with the sole exception of an "error event" - let's not go too deep on that one for now). Remember that for Access code, you DO NOT create a MAIN program, because MS Access IS the main program, whether we are talking about the full-out GUI and developer context or the run-time-only context. The only thing you CAN create is an event routine.

Stated another way, you don't need another event routine because you already are running one. Maybe that will help you see the logic of it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:04
Joined
Feb 19, 2002
Messages
43,352
No, NONE of the control events run when you set the value using VBA.
What does it matter if you are using the form's BeforeUpdate event to do your validation? What EXACTLY are you trying to accomplish?
Here's a picture of the events for the control in the form. I don't log the mouse move since that is just too annoying for words. I did check it though and it of course doesn't run either.
Here's the code in the button click event. This sample was intended to show WHY you cannot rely on the control's BeforeUpdate event for validation. Even if YOU never write code to modify the control in VBA, some successor might. Don't give them a landmine to fall on.

Code:
Private Sub cmdTestVBA_Click()
    'sample that shows that BeforeUpdate event is not raised when control is dirtied via code.

        Me.Governor = "6-cchar"
        MsgBox "Governor set to '6-char' in LostFocus event of Population control", vbOKOnly

End Sub
Here's the code in the other "Governor" control events.
Code:
Private Sub Governor_Change()
    Call LogEvent(Me, "Governor_Change")
End Sub

Private Sub Governor_Click()
    Call LogEvent(Me, "Governor_Click")
End Sub

Private Sub Governor_DblClick(Cancel As Integer)
    Call LogEvent(Me, "Governor_DblClick")
End Sub

Private Sub Governor_Dirty(Cancel As Integer)
    Call LogEvent(Me, "Governor_Dirty")
End Sub

Private Sub Governor_Enter()
    Call LogEvent(Me, "Governor_Enter")
End Sub

Private Sub Governor_Exit(Cancel As Integer)
    Call LogEvent(Me, "Governor_Exit")
End Sub

Private Sub Governor_GotFocus()
    Call LogEvent(Me, "Governor_GotFocus")
End Sub

Private Sub Governor_KeyDown(KeyCode As Integer, Shift As Integer)
    Call LogEvent(Me, "Governor_KeyDown")
End Sub

Private Sub Governor_KeyPress(KeyAscii As Integer)
    Call LogEvent(Me, "Governor_KeyPress")
End Sub

Private Sub Governor_KeyUp(KeyCode As Integer, Shift As Integer)
    Call LogEvent(Me, "Governor_KeyUp")
End Sub

Private Sub Governor_LostFocus()
    Call LogEvent(Me, "Governor_LostFocus")
End Sub

Private Sub Governor_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Call LogEvent(Me, "Governor_MouseDown")
End Sub

Private Sub Governor_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Call LogEvent(Me, "Governor_MouseUp")
End Sub

Private Sub Governor_Undo(Cancel As Integer)
    Call LogEvent(Me, "Governor_Undo")
End Sub

xxVBACode.JPG
 

JMongi

Active member
Local time
Today, 18:04
Joined
Jan 6, 2021
Messages
802
No, NONE of the control events run when you set the value using VBA.
@Pat Hartman - Thank you. It seemed like it should be universal, I just couldn't find any MS documentation stating such. Also, I'm not trying to accomplish anything specific at the moment. Just looking for official reference material similar to the link in the first post.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:04
Joined
Feb 19, 2002
Messages
43,352
Sometimes, MS doesn't bother with what doesn't happen although I have see a reference that control events are not triggered by code. Here's one in the control's BeforeUpdate event.
BeforeUpdate Event - Microsoft Access Visual Basic Documentation
This one led me to add another option to the form to reference the .Text property.
TextBox.Change event (Access) | Microsoft Learn

This does seem to cause the change event to fire but it is throwing other errors. It isn't a method I would ever use so I'm not going to investigate further at this time. If someone can come up with a need that couldn't be solved by using the Form's BeforeUpdate event for validation, I'll look into it after Christmas.
 

Martyh

Registered User.
Local time
Today, 18:04
Joined
May 2, 2000
Messages
196
Hi,
This question fits with what you are talking about Form to Form:
I've been looking for an event that gets fired when one closes the first form and then exits to the second form. Unfortunately, when I switch between the two open forms, the Deactivate event does NOT occurs for the first form, nor the Activate event for the second form. This is because the PopUp property is set to Yes for both forms and it will remain that way!

So back to my original question: Is there any event that gets fired? What is that event? I have been trying to find one that fits with no luck!

Marty
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:04
Joined
Feb 19, 2002
Messages
43,352
@Martyh Download the database at the link I posted earlier.
Add two test forms to the database.
Add the log code to the form level events in the two forms. You can start by copying the sample form which has all the logging set up. Unless you are testing your own code, you don't need much in any of the forms maybe a control or a button if you want to review control events also.
Open the logging form.
Open the first of your forms. All the events that fire will display in the logging form.
Open the second of your forms. All the events that fire will display in the logging form.
 

moke123

AWF VIP
Local time
Today, 18:04
Joined
Jan 11, 2013
Messages
3,927
Look into WithEvents.

here's a basic example. Open form2 and type something then close form2.

In form1 there is a custom event which fires when form2 is closed and copies the text from form2 to form1
 

Attachments

  • custom Event.accdb
    460 KB · Views: 88

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:04
Joined
Sep 12, 2006
Messages
15,660
I don't know is this has been explicitly mentioned, but changing data in a table using VBA is akin to modifying data directly at the table level. You can do pretty well whatever you want whether or not it makes sense, as long as it meets data validation rules.

Changing the value displayed in a text box on a form by using VBA is slightly different, as now you would have a dirty form, which I expect @Pat Hartman meant when she said how important the forms beforeupdate event is for data control.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:04
Joined
Feb 19, 2002
Messages
43,352
@Martyh If you don't understand the existing event model, I would strongly advise against trying to create your own custom event. Once you play with the logging app enough to understand how form events actually work, you may decide that you really need your own custom event in which case, go for it. But don't lose sight of the fact that MS didn't build the hook for the event you think you need because they didn't think it was necessary or because they didn't have the information to implement it correctly. It is unlikely they just "missed" creating it at the beginning and no update in the past 30 years added it either.

I've been looking for an event that gets fired when one closes the first form and then exits to the second form.
Every developer has come to this conclusion at some point in time, including myself. There are always ways to handle this transition. What action do you need to take at this point?

Let me mention Coupling and Cohesion at this point. The two topics seem to be lost to the dust of time because every new developer thinks their new idea is better. But if you understand these two concepts you will improve your skill level dramatically and avoid creating certain kinds of bugs. Have you ever wondered about why some Windows changes break Access? It is because some sloppy code used an object incorrectly. The object was changed and now the incorrect/unpublished method is not available.

Coupling is the links between objects. They should be as few as possible and immutable. They form a contract that cannot be broken because if the object changes and needs different inputs/outputs, it breaks every object that depends on it.

Cohesion is the binding within an object. You probably don't want the same object to paint the screen and print to paper. Each procedure should perform a coherent process. When there are alternative paths, you should consider creating separate "master" objects that each call common as well as custom procedures so you can minimize further changes down the road as well as avoiding accidentally breaking a calling procedure.

No event is dependent on another event which is what you are trying to make happen. All events are called separately by the form's class module mainline. Sometimes multiple events are run due to a single action such as Delete, BeforeDeleteConfirm, and AfterDeleteConfirm but they are not dependant upon the previous event. The first two have the the Cancel option. if either is cancelled, the following events don't get called. The last event does not have a Cancel option and so it can't be cancelled. The delete is complete and cannot be undone when this event runs.
 
Last edited:

moke123

AWF VIP
Local time
Today, 18:04
Joined
Jan 11, 2013
Messages
3,927
You may not even need an event, per se, if switching between pop-up forms. Opening the 2nd form in dialog mode will pause the code in the 1st Pop-Up. We just need to know more about what your trying to accomplish.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:04
Joined
Feb 19, 2002
Messages
43,352
Also, I will remind you that you cannot technically create an event. The class module written by Microsoft runs the form. You didn't write that code, you can't see that code, you can't change that code. It makes the form work the way Microsoft thinks the form should work. it provides hooks for us to add our code at certain points. they did not provide this particular hook, therefore, you cannot add it. Your code will always have to run from one of the existing event "hooks". So, unless you have the exact same process for multiple form transitions, creating a class to simulate an event is pointless. Custom code goes into one of the existing event procedures.
 

Users who are viewing this thread

Top Bottom