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!
 
Hi all

Ok, I got this working with the close button now closing the form the second time and beyond. Good job eduzs!

However, I'm stuck with referring to the userform from outside of the userform. I used to be able to refer back to it using the userform's name, however it seems that link is now broken. Something to do with the
Code:
Set MyForm = New Userform2
i'm guessing.

I've tried MyForm but that's not much chop either.

Train of code:

DATA_DUMP_PAGE UserForm
Code:
Private Sub DELETE_BUTTON_Click()

Application.ScreenUpdating = False


Dim MyForm As ADD_DEL_MAT_SHEET
Me.Hide
Set MyForm = New ADD_DEL_MAT_SHEET
MyForm.Show
Set MyForm = Nothing


End Sub

ADD_DEL_MAT_SHEET UserForm
Code:
Private Sub Delete_Material_Button_Click()

Application.ScreenUpdating = False


TRANSACTION_TEXT_BOX.Visible = True
Add_Material_Button.Visible = False
Delete_Single_Material_Questions


End Sub

Delete-Single_Material-Question()
Code:
Sub Delete_Single_Material_Questions()    
        Answer = MsgBox("YOU ARE ABOUT TO DELETE A MATERIAL LINE. ARE YOU SURE?", vbYesNo + vbQuestion + vbMsgBoxSetForeground, "TIME TO TAKE OUT THE TRASH?")
        
        If Answer = vbYes Then
            
            ADD_DEL_MAT_SHEET.TRANSACTION_TEXT_BOX.Visible = False
            ADD_DEL_MAT_SHEET.PHOTO_BLANK_TEXT.Visible = False
            ADD_DEL_MAT_SHEET.PHOTO_BLANK.Visible = False
            Application.ScreenUpdating = True

When the ADD_DEL_MAT_SHEET userform initially displays it has these three things set as Visible = True - and they are. Run the macros thru to what you see here and nothing changes, however the macro continues on, it doesn't error out there.

Eventually it does error out when it comes to:
Code:
ADD_DEL_MAT_SHEET.Hide

It gives error:
Run-time error '402':
Must close or hide topmost modal first

Any ideas anyone?

Cheers
Chris
 
Last edited:
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Ok, i figured it out.

To refer back to the userform you need to create a public sub that contains the instructions within the userform, and refer to that public sub name from outside of the userform to execute those instructions.

Hope that makes sense?

Cheers
Chris
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
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