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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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