Results 1 to 11 of 11
  1. #1
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317

    Cancel update of field if button clicked

    Please try the following in the attached database:

    1. Type some text in Field1.
    2. Click on the button.
    3. Click on the button again.

    You'll see that the text remains in Field1 until step 3. How is it possible to undo the entry in Field1 at step 2? I'm thinking of something like detecting that the button has been clicked before Field1 is updated, but I've no idea how to do it.
    Attached Files Attached Files

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    Did you try the undo button...ctl-z ?

  3. #3
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    That certainly works, but I need an Undo button on my form (the one in my actual database).

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    Code:
    Option Compare DatabaseOption Explicit
    Private Sub Field1_AfterUpdate()
        MsgBox "Field1 afterupdate fired"
        Field1.Enabled = False
        Field2.Enabled = True
    End Sub
    Private Sub Form_Current()
        Field1.Enabled = True
        Field2.Enabled = False
    End Sub
    Private Sub Form_Undo(Cancel As Integer)
        Field1.Enabled = True
        Field2.Enabled = False
    End Sub
    Private Sub UndoEntries_Click()
        MsgBox "UndoEntries click"
        Me.Undo
    End Sub
    I've diagnosed the problem but not the solution:
    Add the two msgbox lines and you will see the problem.
    If the cursor remains in Field1 when the UndoEntries button is clicked, the Click event does not fire, the Field1_afterupdate does!!
    The second time the button is clicked, it fires as expected.

    However, if you tab out of field1 before clicking the button, it also fires as expected.

    Anyone have any insight as to why the click event is ignored?

  5. #5
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    I came to that conclusion myself a couple of hours ago, but your explanation is far tidier than the one I would have given.

    I've also noticed that nothing seems to have the focus the first time you click on the button (I tried various things to determine this).

  6. #6
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    A "Click" event, is a combination of a "Mouse_Down" + "Mouse_Up" events. In the current case, between those events, occurs the "After_Update" event of "Field1" (when it's value has change) that gives the focus to the "Field 2" and interrupts the "Click" event of the button.

  7. #7
    Remster is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2010
    Posts
    317
    So do you think the solution is to place the code in the Mouse_Up event? I'm on a tablet right now, so I can't test this.

  8. #8
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by Remster View Post
    So do you think the solution is to place the code in the Mouse_Up event? I'm on a tablet right now, so I can't test this.
    I have to apologize.
    In actual fact, the After_Update occurs before the Mouse_Down as well. So, you can click the button only when the Field1 has lost the focus.
    With the code below I think that works fine.
    Code:
    Private Sub Field1_Change()
        Me.Field2.Enabled = True
    End Sub
    
    Private Sub Field1_GotFocus()
        Me.Field2.Enabled = False
    End Sub
    
    Private Sub Field2_GotFocus()
        Me.Field1.Enabled = False
    End Sub
    
    Private Sub Form_Current()
        With Me.Field1
            .Enabled = True
            .SetFocus
        End With
    End Sub
    
    Private Sub Form_Undo(Cancel As Integer)
        With Me.Field1
            .Enabled = True
            .SetFocus
        End With
    End Sub
    
    Private Sub UndoEntries_Click()
        Me.Undo
    End Sub
    Regards,
    John

  9. #9
    Remster is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2010
    Posts
    317
    Thanks, I'll give this a go on Monday, when I'm back at work.

  10. #10
    Remster is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2010
    Posts
    317
    I've just tried it on my desktop PC at home, and it works excellently. GotFocus turns out to be better than AfterUpdate anyway, since it allows the user to click instead of tab into Field2.

    Just one query. In my original, I disabled Field2 in Form_Current, whereas you've done it in Field1_GotFocus. Is there any particular reason for this?

  11. #11
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by Remster View Post
    I've just tried it on my desktop PC at home, and it works excellently. GotFocus turns out to be better than AfterUpdate anyway, since it allows the user to click instead of tab into Field2.

    Just one query. In my original, I disabled Field2 in Form_Current, whereas you've done it in Field1_GotFocus. Is there any particular reason for this?
    There is no any particular reason for this. Simply, it's no need to disable Field2 in Form_Current. That is doing in Field1_GotFocus.
    As in real world, in the world of code you have to be laconic. ;-)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Cancel Button
    By UT227 in forum Programming
    Replies: 5
    Last Post: 02-23-2018, 09:17 AM
  2. Cancel button
    By sprtrmp in forum Programming
    Replies: 7
    Last Post: 10-29-2015, 11:50 AM
  3. Replies: 2
    Last Post: 01-11-2015, 11:45 PM
  4. Replies: 5
    Last Post: 05-27-2014, 02:44 PM
  5. Replies: 7
    Last Post: 11-30-2013, 12:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums