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!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Maybe use the UserForm_QueryClose event instead of the terminate event. U can also hide the form instead of unloading it. HTH. Dave
 
Upvote 0
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:
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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?
 
Upvote 0
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 ?
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top