+ Reply to Thread
Results 1 to 4 of 4

Stop exit event of textbox firing when click a command button to close a userform

Hybrid View

chrisjames25 Stop exit event of textbox... 10-18-2013, 07:03 AM
6StringJazzer Re: Stop exit event of... 10-18-2013, 09:20 AM
Tsjallie Re: Stop exit event of... 10-18-2013, 09:25 AM
Izandol Re: Stop exit event of... 10-18-2013, 09:27 AM
  1. #1
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Stop exit event of textbox firing when click a command button to close a userform

    HI I have a userform with a textbox that will not let you exit it if it is empty. This works great until I want to exit the userform and click a command button to leave and it asks me to enter a batch no.

    I found a old post suggesting the following
    Dim bflag As Boolean
    
    
    
    Private Sub supplierpricetxt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    If bflag = True Then Exit Sub
      
      
        If supplierpricetxt = "" Then
        MsgBox "Please enter a Batch No."
        
        Cancel = True
        Exit Sub
        End If
    End Sub
    
    
    Private Sub cmdClearAndExit_Click()
    bflag = True
    Unload Me
    End Sub
    However this does not seem to work for me even though the logic seems fine. Am I missing something or is there alternative ways around this problem.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,785

    Re: Stop exit event of textbox firing when click a command button to close a userform

    The problem is that when you try to click the button, the Exit event on the textbox takes precedence and you never get the button Click event. So the textbox complains that it's still blank.

    I can think of very few cases where you want to use an Exit event to force the user to provide a value. I would redesign the user interface so that you are not checking the content of the textbox until you try to exit the form. Then check for an empty textbox at that point.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Stop exit event of textbox firing when click a command button to close a userform

    I guess this could solve your problem:
    Dim bflag As Boolean
    Private Sub cmdClearAndExit_Click()
        If bflag Then
            Unload Me
        End If
    End Sub
    Private Sub supplierpricetxt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        If Not bflag Then
            bflag = False
            If supplierpricetxt = "" Then
                Ans = MsgBox("No Batch No entered." & vbCrLf & "Click Cancel to return to BatchNo" & vbCrLf & "Click OK to exit without BatchNo", vbOKCancel)
                Select Case Ans
                    Case 2 'Cancel clicked
                        Cancel = True
                    Case 1 'OK clicked
                        bflag = True
                        cmdClearAndExit_Click
                End Select
            End If
        End If
    End Sub
    And yes, you shouldn't use the exit-event for validation purposes. Consider the before_update or after_update events.
    Last edited by Tsjallie; 10-18-2013 at 09:28 AM.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Stop exit event of textbox firing when click a command button to close a userform

    Did you set the TakeFocusOnClick property of the button to False?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Userform multipage control - exit event not firing or event order
    By jane serky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2013, 10:23 AM
  2. Code a Command Button on UserForm not to close upon click - Excel VBA
    By eemiller1997 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-14-2012, 02:20 PM
  3. [SOLVED] Is it possible to have a command button click event autofill a textbox within a userform?
    By Orestees in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2012, 02:14 PM
  4. Userform - Stop Command button working if a textbox is blank?
    By revenge4ash89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-10-2012, 05:52 AM
  5. Userform textbox event that fires after I exit the textbox
    By jerseyguy1996 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-04-2010, 08:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1