clicking on button in form from subform

Akaban

New member
Local time
Today, 14:47
Joined
Sep 4, 2022
Messages
2
Hi -- I arrived at this thread because I am trying to resolve the "need to click the parent form button twice" problem myself. What I have discovered is

1. When the subform has the focus and you click a button on the parent form -- let's say your own "Undo" button -- the subform's After_Update method fires.
2. From that subform code, if you look at me.parent.activecontrol.name, you get the name of the subform, not the name of the button that was clicked.
3. I have not been able to find any event code in the parent form that executes as a result of clicking the button in the parent while the subform has the focus. Nor have I been able to find a way to even determine that the Parent's UNDO button was clicked.

Like Wysy, I am left for the time being with having to click UNDO twice if the focus was on the subform when UNDO was clicked. I will find a solution, live with two clicks, or implement an UNDO on the child which, in my app, I would want to also undo the parent. Implementing UNDO on the subform with a scope of action outside the subform seems tacky so I likely won't do that.

Bottom line: I'll find the solution or, since the app is for me, I could, if necessary live with double clicks of the UNDO button. I'd rather not though. :)

Update: No sooner did I post the above than I discovered that in the parent form, the subform control's <ctrlName>_Exit method fires. You still don't know what was clicked and the activecontrol remains the subform. But I was wrong to say no code in the parent executes. Now to continue to try and figure out how the code can know UNDO was clicked.

Mod Edit: This topic was split from below original thread to avoid any confusion.
 
Last edited by a moderator:
@Akaban Welcome to the forum. I'm going to answer your question BUT, in the future please do NOT hijack an existing thread and add your question to it. If you think there is relevant info in an old thread then please post a link to it but ALWAYS start a new thread for YOUR question. It just gets too confusing for people to figure out whose question they are addressing. I'm only answering this question here because the old thread had only 5 posts so people won't get lost.

To answer your question, you need to understand how Access forms work so bear with me. Access takes personal responsibility to ensure your data is safe. That means that when you are using a bound form, not only do YOU get to say when the data should be saved but Access also makes this decision on your behalf whenever it thinks you "forgot" to save. This happens under many situations but the one you are encountering is when you move focus from a subform to a main form, Access always saves the subform record Or when you move focus from a main form to a subform, Access always saves the main form record. The side effect of that is that you CANNOT put buttons that affect subform records on the main form and expect them to work correctly.

1. Not only does the subform's After update event fire but its BeforeUpdate event also fires because the LAST event that fires BEFORE data gets saved REGARDLESS of what/who prompted the save is the form's BeforeUpdate event. That is why this event is the event where you should put the majority of your validation logic. The form's AfterUpdate event then fires AFTER the dirty record has been saved. So, by the time you click the undo button on the main form, it is way too late. The horses are out of the barn and saved snuggly in your table so there is no way you can undo the update.
3. That is because there is NONE for the reasons I explained above.

If you want to add an undo button for the subform, it MUST be on the subform itself. It CANNOT work correctly from any other place.

Please don't take the following as a suggestion because I am NOT suggesting that you do this and in fact I would NEVER do it because it just annoys the users and trains them to click OK on all your messages without actually reading them.

In the BeforeUpdate event of the subform, AFTER you have validated the data and NOT found any errors, you can give the user the option of undoing the update.
Code:
Select Case Msgbox("Please press Yes to save; No to return to editing; or Cancel to discard changes", vbYesNoCancel)
    Case vbNo
        Cancel = True
        Exit Sub
    Case vbCancel
        Cancel = True
        Me.Undo
        Exit Sub
End Select

What you actually need to think about is why you want to give the user the option to cancel? Is it because you didn't do any validation and you are expecting the user to verify his work before pressing Yes to save?
 
I just did a quick test and wasn't able to duplicate this problem. As you can see in the image below, the subform is currently being edited. When I click on the "Click Me" button at this stage (while the subform is still in Edit mode), the button's Click event fires. I don't have to click a second time.

1662417233670.png
 
@theDBguy Did you also notice that clicking on the button on the main form saved the subform record?
 
You say "of course" but for some reason the poster thinks that if he presses the "undo" button twice, the subform record is undone and that is simply not happening because it has already been saved. Of course we don't know what code is behind the "undo" button. What we do know is that it is not Me.Undo:)
 
You say "of course" but for some reason the poster thinks that if he presses the "undo" button twice, the subform record is undone and that is simply not happening because it has already been saved. Of course we don't know what code is behind the "undo" button. What we do know is that it is not Me.Undo:)
Hi @Pat Hartman The "undo" button in question was actually just an example from @Akaban, which is not the OP, by the way (@Wysy was).

The original topic of this thread, from what I can see, is that clicking on a button on the main form (it doesn't matter what it does, I just made mine a simple MsgBox call) while the subform is the active control, necessitates a second click, because the OP and Akaban seem to experience a "no action" when they only click on that button once.

So, it seems the topic got sidetracked, somehow. You can do me a favor and either confirm or repudiate my experiment by trying it out yourself and let us know what behavior you got.

Cheers!
 
I was responding specifically to @Akaban 's post where he observed the subform's AfterUpdate event running which of course indicated that the subform record had been saved and therefore was NOT available for "undo" and I also told him to NOT hijack threads in the future because of the confusion it causes. And here we are "confused: :):)

I did add a new button to a main form to see if the behavior had changed. It only took one click to activate the button. I'm pretty sure that there was something else going on in the original post that caused the problem since I've never encountered an issue having to click more than once on a main form button to activate it.
 
I was responding specifically to @Akaban 's post where he observed the subform's AfterUpdate event running which of course indicated that the subform record had been saved and therefore was NOT available for "undo" and I also told him to NOT hijack threads in the future because of the confusion it causes. And here we are "confused: :):)

I did add a new button to a main form to see if the behavior had changed. It only took one click to activate the button. I'm pretty sure that there was something else going on in the original post that caused the problem since I've never encountered an issue having to click more than once on a main form button to activate it.
Hi Pat. Thanks for the update and clarification. I don't like it either when I get confused. :)
 
It takes so little these days:eek:. I would have split off the question but I couldn't remember how and I didn't want to mess it up. I thought there was a split option but I couldn't find it. All I saw was a move and I didn't want to move the whole thread since it was in the correct forum.
 
It takes so little these days:eek:. I would have split off the question but I couldn't remember how and I didn't want to mess it up. I thought there was a split option but I couldn't find it. All I saw was a move and I didn't want to move the whole thread since it was in the correct forum.
I think splitting was a good idea. Done! (It took a few steps...)
 

Users who are viewing this thread

Back
Top Bottom