Need help understanding event options (1 Viewer)

cjrhoads

Registered User.
Local time
Today, 09:25
Joined
Nov 27, 2013
Messages
14
Hi.
I'm trying to figure out the best place/way to put certain subroutines to update certain fields in my form. I have tried to find a good explanation of the different options (On Click, Before Update, After Update, On Exit, ...etc.) but I haven't found one that describes the nuances well enough for me to understand them.
The problem I'm running into is that the code runs fine when the field is updated manually - i.e. the person types in the field and then tabs to another field. I've put the code into the After Update.

For example - I want the Nickname to update after the Firstname is typed in. In the After Update I have the following code:
If IsNull(Me.Nickname) Then
Me.Nickname = Me.Firstname
End If

The problem is that I also have a parsing routine. When the parsing routine updates the name, the copy-to-nickname code doesn't run. I could copy the Firstname code into the parsing routine, but we are not just talking a single update - I've got dozens of these. I expected that they would run whether the field was updated manually or through another subroutine, and that's not happening. Should I be using On Change instead of After Update?

In other cases I want the code to run when the user tabs out of a field even if they didn't change anything. Would On Exit work, or does On Exit refer to exiting out of the entire form instead of just that one field?

Any help that you can give would be most appreciated.

Thanks
CJ Rhoads
 
Last edited:

MarkK

bit cruncher
Local time
Today, 06:25
Joined
Mar 17, 2004
Messages
8,199
Hi there. Use subroutines. Write a subroutine that handles the logic for creating a nickname from a firstname. Then trigger that functionality from wherever you need to, so if external consumers need that functionality, make it public . . .
Code:
Public Function GetNicknameFromFirstname(Firstname As String) as String
   GetNickNameFromFirstname = [COLOR="Green"]'Your logic here[/COLOR]
End Function
What other things need to be able to happen easily? Maybe the form itself will need to save the nickname without parameters . . .
Code:
Private Sub CreateAndSaveNewNickname()
   Me.Nickname = Me.GetNicknameFromFirstname(Me.Firstname)
End Sub
. . . and now in Form_AfterUpdate you can do . . .
Code:
Private Sub Form_AfterUpdate()
   CreateAndSaveNewNickname
End Sub
. . . but the principle is that you distinguish your event handlers from the functionality of your objects. A form, and other objects, sometimes raise events that you can use to trigger your custom processing, but it's commonly very handy to put your custom processors in subroutine of their own, so that they can be triggered independently of the functioning of the form.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:25
Joined
Feb 19, 2002
Messages
43,774
In other cases I want the code to run when the user tabs out of a field even if they didn't change anything. Would On Exit work, or does On Exit refer to exiting out of the entire form instead of just that one field?
The OnExit event exists for each control but ONLY if the control was ever entered. The Exit event wouldn't run if the user never tabbed/clicked into the control so it has limited usefulness.

The Form's BeforeUpdate event is probably the most important event for a form. Think of it as the funnel through which ALL updates must pass. It is the LAST event that runs before Access saves a record and it runs regardless of what triggered the save. Final edits are done here as are any edit that involves checking for empty fields or comparing the value of one field to another. Cancel the update if any edit fails to prevent bad data from being saved.
 

cjrhoads

Registered User.
Local time
Today, 09:25
Joined
Nov 27, 2013
Messages
14
Pat - that is the best explanation I have seen on BeforeUpdate for the form, which I didn't really understand. I'm definitely going to use that information for some of my requirements - though it wouldn't help for the name parsing and updating thing, which needs to be done immediately so the person can change the Nickname to something else if it shouldn't be the firstname.
And on-exit might work; generally after parsing the full name the user tabs through the different parts, so if the code were to run on-exit, that would work better than after-update. The problem with after-update is that just going into the field doesn't launch the code - the person has to change something to make it work.
I'm still confused, however, about the difference between On Change and AfterUpdate on a control. Can someone explain that?

And Lagbolt - the concept of separating the event handlers from the functionality is helpful too, though I'm not sure I'm quite up to creating functions, and don't even know where to begin with that. The truth is I haven't done any real programming for nearly 20 years, and I'm more than a little rusty (and I was never that great at it to begin with). I'm really trying to just get little bits of code to run along the lines of "replace this with that" inside the form.

Thanks - I'll try the on-exit unless someone tells me that on-change will launch the code even if updated programmatically.

Peace
CJ Rhoads
 

MarkK

bit cruncher
Local time
Today, 06:25
Joined
Mar 17, 2004
Messages
8,199
The change event fires for every keystroke in the control, and only the Text property is modified. AfterUpdate fires when the update is complete and the Value and OldValue properties of the control are modified.
 

Users who are viewing this thread

Top Bottom