I assume you want to disable the red X but use another button on the form to close it.
The UserForm_QueryClose, Cancel = True solution doesn’t work so great since the close event gets cancelled even if executed from code using “Unload Me”
You could create a boolean variable like “NoClose” and set it’s value to True. When the user clicks the red X and triggers the QueryClose event, this code would execute as…
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = NoClose
End Sub
You could then use a dedicated command button on the form to function as…
Sub cmdCloseButtn()
NoClose = False
'Do some special stuff here…
Unload Me
End Sub
Now the Cancel will be set to False when the form tries to close and will function as usual.
Another method is to use the CloseMode to determine how the form was closed. This info describes the integer values passed to QueryClose event.
0 = The user chose the Close command from the Control menu on the form
1 = The Unload statement is invoked from code
2 = The current Microsoft Windows operating environment session is ending
3 = The Microsoft Windows Task Manager is closing the application
4 = An MDI child form is closing because the MDI form is closing
5 = A form is closing because its owner is closing
This is the best way to determine exactly why the form is closing and make decisions for your error handlers.
Using a basic example like…
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then Cancel = True
End Sub
This would only allow the form to close if Unload Me was invoked through code and all other attempts would be ignored.
Sorry this was so long but hopefully it was helpfull.
AV-Guy