Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Userform "X" close button not working second round

  1. #1
    New Member
    Join Date
    Jun 2012
    Posts
    47
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Userform "X" close button not working second round

    I have 2 userforms name "Home" and "Permanent".

    Home has several command buttons on it and one of them is set to close home and open permanent as follows.
    Code:
    Unload Home
    Permanent.Show
    The "Permanent" userform has several controls on it that are editable by the user however, I do not want to put a close button on the userform. The user will have to use the "X" to close the form. I have a userform terminate procedure to run when the user clicks the "X" close button to reopen the Home userform as follows.

    Code:
    Private Sub UserForm_Terminate()
    HOME.Show
    End Sub
    At this point - the user once again has the option to click the command button that opens "Permanent" - this is fine however, if the user does indeed open "Permanent" a second time, the "X" close button on the "Permanent" userform no longer works. I assumed this is because at no point was the "Permanent" userform actually unloading so I adjusted the terminate procedure to the following.

    Code:
    Private Sub UserForm_Terminate()
    Unload Permanent
    HOME.Show
    End Sub
    Even with this modification, if the "Permanent" userform gets opened a second time.... the "X" close button no longer works.

    In the end - what I am looking for is a way to do this sequence without somehow disabling the "X" button as I seem to be doing. The "X" button is the only way for the user to exit out of the "Permanent" userform so it needs to work.

    Thanks in advance for taking the time to help.

    Cheers!

  2. #2
    Board Regular NdNoviceHlp's Avatar
    Join Date
    Nov 2002
    Location
    Manitoba Canada
    Posts
    2,285
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Userform "X" close button not working second round

    Maybe use the UserForm_QueryClose event instead of the terminate event. U can also hide the form instead of unloading it. HTH. Dave

  3. #3
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    22,648
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Userform "X" close button not working second round

    I agree with the hide suggestion.

    The first code snipet in the OP would read

    Code:
    ' somewhere in Home
    
    Home.Hide
    Permanent.Show
    Home.Show
    Depending on how much information you want the re-shown Home form to retain, you might have to move some code from Home's initialize event to its activate event. Or write a whole ResetMyValues sub and call it both from Home's initialize and after Pemanent has been dismiseds

    Code:
    ' somewhere in Home
    
    Home.Hide
    Permanent.Show
    
    Call ResetMyValues
    Home.Show
    
    Private Sub ResetMyValues
        With Me
            .TextBox1.Text = "hello"
            .TextBox2.Text = vbNullString
            .ComboBox1.List = Array("one", "two", "three")
            ' etc
        End With
    End Sub
    Last edited by mikerickson; Feb 20th, 2016 at 11:29 AM.

  4. #4
    New Member
    Join Date
    Jun 2012
    Posts
    47
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Userform "X" close button not working second round

    Quote Originally Posted by NdNoviceHlp View Post
    Maybe use the UserForm_QueryClose event instead of the terminate event. U can also hide the form instead of unloading it. HTH. Dave
    Thanks for the response. I have attempted to use the QueryClose event to set the cancel value to 0 to ensure that the user has the option to close the form. Unfortunately, on the second load of the Permanent userform, the close button in the title bar still does not work.

  5. #5
    New Member
    Join Date
    Jun 2012
    Posts
    47
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Userform "X" close button not working second round

    Quote Originally Posted by mikerickson View Post
    I agree with the hide suggestion.

    The first code snipet in the OP would read

    Code:
    ' somewhere in Home
    
    Home.Hide
    Permanent.Show
    Home.Show
    Depending on how much information you want the re-shown Home form to retain, you might have to move some code from Home's initialize event to its activate event. Or write a whole ResetMyValues sub and call it both from Home's initialize and after Pemanent has been dismiseds

    Code:
    ' somewhere in Home
    
    Home.Hide
    Permanent.Show
    
    Call ResetMyValues
    Home.Show
    
    Private Sub ResetMyValues
        With Me
            .TextBox1.Text = "hello"
            .TextBox2.Text = vbNullString
            .ComboBox1.List = Array("one", "two", "three")
            ' etc
        End With
    End Sub
    Thanks for your thoughts. I have attempted to hide the home userform and then show again. My Home userform is a series of several command buttons so there is no information on it that needs to be saved. When the command button to open the Permanent userform is clicked, I don't even really have to discard the Home userform as it naturally hides behind the Permanent userform; so I have attempted to completely remove this step. Once the Permanent userform is closed for the first time, if the user opens it again via clicking the command button on the home userform, it doesn't matter what form ive used to discard the home userform, the close button in the Permanent userform no longer works on the second go-round. The only way that I can get it to work is to have the home userform close at the same time which I do not want to do. Something with closing the Home userform actually resets it. If the Home userform is re-opened, then the Permanent userform reopened - all works but this is extra steps that I don't want the end user to have to do.

  6. #6
    New Member
    Join Date
    Jun 2012
    Posts
    47
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Userform "X" close button not working second round

    I should add, that even if I use code to close the home userform, close the Permanent userform and then show the Home userform again in the Terminate event, if the user immedietly opens the Permanent userform again, the close button will not work. The Home userform has to be manually closed. Something about that actually resets whatever is happening.

  7. #7
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    22,648
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Userform "X" close button not working second round

    I can't replicate your problem.
    I created a user form with one button and this code
    Code:
    ' in userform1
    
    Private Sub CommandButton1_Click()
        UserForm2.Show
    End Sub
    And the corner X button of Userform2 worked fine every time.

    What is your Permanent user form doing, what code do you have in its module. And why is the corner X the only way you have to close it?

  8. #8
    Board Regular Jaafar Tribak's Avatar
    Join Date
    Dec 2002
    Location
    Larache--Morocco
    Posts
    7,398
    Post Thanks / Like
    Mentioned
    40 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Userform "X" close button not working second round

    I have had this problem before and it is due to the code in the userform ... I can't remember how I solved it
    Can you show us a simplified version of the code that you have ?
    Office/Excel 2010 64Bits -- Win10 64Bits

    Common sense is not so common.


    http://photo-larache.blogspot.com/

  9. #9
    Board Regular
    Join Date
    Jul 2014
    Location
    Anywhere
    Posts
    450
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Userform "X" close button not working second round

    I know this topic is old, but I've been through the same problem.
    Userform2 button "X" works only at the first show/hide round.
    I found elsewhere the following solution that worked for me.
    The solution was a indirect reference to the userform2 in the button in userform1

    Code:
    Private Sub CommandButton1_Click()
    
    Dim MyForm  As Userform2
    Me.Hide
    Set MyForm = New Userform2
    MyForm.Show
    Set MyForm = Nothing
    
    End Sub
    In the queryclose userform2 I use this:

    Code:
    Private Sub UserForm_QueryClose(cancel As Integer, CloseMode As Integer)
    ...
    Unload Userform1
    Me.Hide
    Call Reopen_Userform1
    
    end sub
    Thanks all
    Last edited by eduzs; Feb 22nd, 2018 at 06:35 PM.
    Using Excel/Word 2010.
    Backup your original file before doing any modification.

  10. #10
    New Member
    Join Date
    Jan 2018
    Posts
    8
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Userform "X" close button not working second round

    Quote Originally Posted by eduzs View Post
    I know this topic is old, but I've been through the same problem.
    Userform2 button "X" works only at the first show/hide round.
    I found elsewhere the following solution that worked for me.
    The solution was a indirect reference to the userform2 in the button in userform1
    Appreciate the post eduzs!

    That got me out of the jam I'm in, almost. I say almost as my userform was totally responsive the second time around. Changing the userform_terminate event to userform_QueryClose has given the userform all functionality back EXCEPT for the **** close button!

    The terminate function does something to the userform from which it doesn't recover. A simple test case:

    In Userform1:
    Code:
    Private Sub CommandButton1_Click()
    UserForm1.Hide
    UserForm2.Show
    End Sub
    In Userform2:
    Code:
    Private Sub UserForm_terminate()
    UserForm2.Hide
    UserForm1.Show
    End Sub
    In Module:
    Code:
    Sub letstrythis()
    UserForm1.Show
    End Sub
    Run this and sure enough, the second time around, the close cross doesn't respond.

    I'm sort of somewhere between beginner and intermediate with vba, so if any one has an idea, please post it up.

    Thanks
    Chris

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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