Hi all How to disable Close button in VBA UserForm thanks in adva nce kumaran v

change form property borderstyle as none

or you can careate as small form and plase on the position of close button using code you have to use api for show this for always on top

Hi !

Nice suggests. Thanks.

Regards
Pedaammulu
VB, SQL Server, Accounting

— AV-Guy via vb-vba-l

Here’s an example that uses QueryClose as AV-Guy suggested.

Unfortunately you don’t seem to be able to actually disable the button like you can in VB (and I had hoped we could here).

Rod
Rod Stephens, Author of “Visual Basic 2005 Programmer’s Reference”

Sign up for the free VB Helper Newsletters at VB Helper: Newsletter

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

Kumaran,

Assuming the name of the Close button is “cmdClose”, you can use
“cmdClose.Enabled = False”.

regards
Ian


You can use this code to disable it, just set “Cancel = True” into the form’s QueryClose sub routine.

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

Hope this helps,
Jim

How to disable Close button in VBA UserForm

This example shows how to do it in VB.

(When I have a chance, I’ll try it in VBA, too.)

Rod

Rod Stephens, Author of “Visual Basic 2005 Programmer’s Reference”

Sign up for the free VB Helper Newsletters at VB Helper: Newsletter