Closing a Userform

cs1ctp

Board Regular
Joined
Oct 7, 2005
Messages
122
Hi,

Is there anyway to disable the close button in the top right hand corner of a UserForm? I am preparing a password dialog, but at the moment, the user can simply bypass the password entry by simply closing the dialog box. Security becomes void!!

Thanks

cHrIs
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
cs,
It's as simple as this to disable the close event. The difficult part is setting up conditions so that once you requirements are met, the user can close the workbook. Otherwise, they will never be able to close it.

Code:
Sub workbook_BeforeClose(Cancel As Boolean)
Cancel = True
End Sub

HTH
Cal
 
Upvote 0
Ok tried that, but not sure it is what I require!!

It is not the workbook that I am trying not to close, it is a UserForm. I tried attaching the code you specified to the UserForm, but I am still able to close the UserForm using the Close button.

Code:
Sub Logon_BeforeClose(Cancel As Boolean)
Cancel = True
End Sub

Where Logon is the name of the UserForm I wish to restrict from closing.

I am probably missing something simple here :oops: , but please advise.

Many Thanks,

Chris
 
Upvote 0
Sorry,
I didn't read the whole thing. I thought you were talking about a workbook. Give me a few minutes.

Cal
 
Upvote 0
That kinda sux's.
You can't seem to do it.
Here's my suggestion.
Code:
Private Sub UserForm_Terminate()
dim oldvalue as string

oldvalue =textbox1.value 
UserForm1.Show
textbox1.value = oldvalue

End Sub

You will need to store the values of all entries, reload the form, and then put the values back in again. You will also need to put a condition in for when it's OK to close the form.

HTH
Cal
 
Upvote 0
Hi Chris,

I always use this code for my userforms, i have a few workbooks with several in and it works everytime.


Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    'DeActivates the x button on the UserForms
    If CloseMode = 0 Then Cancel = True
End Sub

Hope this helps.
 
Upvote 0
Excellent, that works fine ... I will have a play around to insert the conditions to close the UserForm.

Thanks for your help
 
Upvote 0
This must be the most asked question ever, Microsoft should make this an option for userforms. You need to add the following code to your userform:-

Code:
Private Sub UserForm_QueryClose(cancel As Integer, closemode As Integer)
    If closemode = vbFormControlMenu Then cancel = True
End Sub

That will stop the 'x' from doing anything you can add a msgbox to that procedure informing the user or not.

Andy
 
Upvote 0
charllie,
That's pretty weird, I wonder why they called it queryclose instead of formclose? I don't work with forms a whole lot, so this is good to know.

Cal
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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