PDA

View Full Version : Solved: Disable the "X" button on a form.



sandam
07-18-2005, 12:59 AM
I hate to repeat questions but I searched and couldn't find the answer even though I'm sure I saw a post that showed how ....grumblegrumbleshouldacopyandpastewhenihadthechancegrumblegrumble...
:banghead:

ANyway, how do you disable the built in "X" button at the top of a userform in VBA?

TIA
Andrew;?

doctordoggie
07-18-2005, 04:58 AM
I've just had a go at this one - try putting this code in with the userform code (You need to change the 'UserForm_QueryClose' bit to reflect the name of the form).

This code will always cancel a close, but will leave unload and hide functions alone. I think the idea is that you pop up a message box to ask if it's ok to close the form. If Cancel is set to anything other than 0 then the close will be cancelled. This code just cancels regardless.

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = 1
End Sub

It seems to work anyway.

Doggie

Steiner
07-18-2005, 06:30 AM
You should check the closemode to allow the user to close your form using a button you provide (which calls Unload Me which is CloseMode 1):


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then Cancel = True
End Sub


Daniel

Jacob Hilderbrand
07-18-2005, 09:08 AM
Here is an example you can download: Disable X (http://www.vbaexpress.com/kb/getarticle.php?kb_id=164).

MOS MASTER
07-18-2005, 09:47 AM
I've just had a go at this one - try putting this code in with the userform code (You need to change the 'UserForm_QueryClose' bit to reflect the name of the form).
Doggie

Hi and welcome to VBAX! :hi:

FYI the UserForm_QueryClose code does not have to reflect the name of the form.

All UserForm Events start with "UserForm" so the syntax is always:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

End Sub


HTH, :whistle:

sandam
07-19-2005, 12:56 AM
Sweet, thanks guys. Knew it was here somewhere. I've found my users click thourgh too quickly in the macros no matter how attention grabbing the forms are. This will force them to use my command buttons. This one is offically solved :)

Johnpants
11-08-2005, 09:52 AM
I know this is solved so I hope it is ok to add a small post at the end. If I wanted my workbook to close when I clicked on the CommandButton1 what would I need to enter in there? It is a login userform so i don't want people to just click the X and still have access, but would like a 'Cancel' button that when clicked closes the workbook...


Thanks, and sorry if I shouldn't have posted here..

sandam
11-08-2005, 09:55 AM
try


Application.ActiveWorkBook.Close False


And if this thread suits your need then its ok to post here.

Johnpants
11-08-2005, 10:00 AM
Thank you Sandam, works perfect and just what I was looking for, appreciate it.

sheeeng
11-09-2005, 08:22 AM
Thanks for all the information provided. :friends:
Looks like we have more than an alternative to choose from to disable the 'X' button.

Marcster
11-09-2005, 08:58 AM
How do disable the x in a userform by using API:
(The x will be greyed out)

http://word.mvps.org/FAQs/Userforms/DisableClose.htm

Marcster.