Diable close button

C

Cor van der Bliek

A close button is automatically inserted on a UserForm. How can I prevent
this. I want to control the closing of the form only through the use of OK or
Cancel.
 
S

Suzanne S. Barnhill

What do you mean by "automatically"? I wasn't aware that anything appeared
on a UserForm unless you put it there.
 
C

Cor van der Bliek

A UserForm gets a Close button (the normal X on the right top) by default.
Pushing this button closed the form without programmable interaction.
That is what I want to prevent. I want to always be able to control the way
the form is closed.
 
C

Cor van der Bliek

Since it can't be removed, let's give it something useful to do:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
MsgBox "Use the OK or Cancel button to close this form"
End Sub

Thanks.
 
S

Suzanne S. Barnhill

Ah, I wasn't thinking of the X but rather an actual button. Thanks for the
clarification (and see Jay's post for an actual answer).
 
J

Jay Freedman

Hi Cor,

Well, not quite. That would display the message whenever the user closes the
userform, regardless of how they did it. I'd find that annoying if I
actually had clicked OK or Cancel. What you want is this:

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Use the OK or Cancel button to close this form"
End If
End Sub

An alternative would be to assume that the X button should be the equivalent
of the Cancel button, and just do it:

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
btnCancel_Click
End If
End Sub

If you've renamed the Cancel button to something other than btnCancel, alter
this as necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top