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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
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
 

cs1ctp

Board Regular
Joined
Oct 7, 2005
Messages
122
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
 

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
Sorry,
I didn't read the whole thing. I thought you were talking about a workbook. Give me a few minutes.

Cal
 

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196

ADVERTISEMENT

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
 

charllie

Well-known Member
Joined
Apr 6, 2005
Messages
983
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.
 

cs1ctp

Board Regular
Joined
Oct 7, 2005
Messages
122

ADVERTISEMENT

Excellent, that works fine ... I will have a play around to insert the conditions to close the UserForm.

Thanks for your help
 

andyh69uk

New Member
Joined
Nov 23, 2005
Messages
31
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
 

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,998
Messages
5,834,805
Members
430,322
Latest member
excelnoobnoob

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
Top