Diable close button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
What do you mean by "automatically"? I wasn't aware that anything appeared
on a UserForm unless you put it there.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
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.
 
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.
 
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).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
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.
 
Back
Top