Userform problems disabling X close button

gooniegirl180

Board Regular
Joined
Aug 13, 2003
Messages
152
I'm a struggling and inexperienced VBA writer. I can't get me head around what I'm doing wrong here. I am attempting to effectively disable the "X" close button on a user form. I have the following code:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = True
Unload ClaimHeader1
MsgBox "Invalid Action", vbOKOnly
ClaimHeader1.Show
End If
End Sub

If a user clicks on the "X" close button, the "Invalid Action" message comes up correctly, however, the userform doesn't unload and is still on the screen. When the user presses on the "OK" button of the messagebox, I get the error "Form already displayed: can't be shown modally", and the debugger highlights the "ClaimHeader1.Show" command as being the error.

I tried including the command "ClaimHeader1.Hide" after the Unload command, and that worked, but only for the first press of the "X" button - if the user presses the "X" close button a second time, nothing at all happens. I have a "Cancel & Exit" button on the form, and if that is pressed at this point then the ClaimHeader1 userform stays displayed despite the code continuing to execute.

What am I missing?

Thanks,
Goonie
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,
try this update to your code & see if does what you want

QueryClose Event

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = CloseMode = 0
    If Cancel Then MsgBox "Invalid Action" & Chr(10) & "Please Use Cancel Button", 48, "Invalid"
End Sub

Your Cancel Button

VBA Code:
Private Sub cmdCancel_Click()
    Unload Me
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top