Userform "X" close button not working second round

Dustan

New Member
Joined
Jun 19, 2012
Messages
47
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!
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,315
Maybe use the UserForm_QueryClose event instead of the terminate event. U can also hide the form instead of unloading it. HTH. Dave
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,692
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:

Dustan

New Member
Joined
Jun 19, 2012
Messages
47
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.
 

Dustan

New Member
Joined
Jun 19, 2012
Messages
47
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.
 

Dustan

New Member
Joined
Jun 19, 2012
Messages
47
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.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,692
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?
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,420
Office Version
2016
Platform
Windows
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 ?
 

eduzs

Active Member
Joined
Jul 6, 2014
Messages
450
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:

Raptor6L

New Member
Joined
Jan 18, 2018
Messages
8
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
 

Forum statistics

Threads
1,078,226
Messages
5,338,958
Members
399,272
Latest member
jakepenner

Some videos you may like

This Week's Hot Topics

Top