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
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

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
974
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
 

Watch MrExcel Video

Forum statistics

Threads
1,118,448
Messages
5,572,174
Members
412,447
Latest member
immy
Top