Help with the X on a form in VBA

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Is there a way to write code to the X on a userform located in the upper right hand corner of the form. I have a few textboxes on my form that alerts the user with a message box if they did not enter a value in the textbox on exit. The problem that I am having is if the user opens the form by accident and they want to close out of it by clicking the X, without entering any values in the text boxes, the message box still displays. So what I need is the form to close without displaying any message boxes. Thank you.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Code:
Private Sub cmbPrdCde_Exit(ByVal Cancel As MSForms.ReturnBoolean)Dim rtn_ans2 As String
    
    If cmbPrdCde.Value = "" Then
        rtn_ans2 = MsgBox("Please select a product.", vbOKOnly, "Select a Product")
    Select Case rtn_ans2
    Case 1
            Cancel = True
    End Select
    
    End If
End Sub
 
Upvote 0
Is that a command button Exit event? And are you getting THAT message when you try to close the userform without values in TBs?
 
Upvote 0
I'm sorry. I'm sure if what you are referring to. The "X" I'm referring to is the white X with a red background on the form or the white X is also on a webpage. The code mentioned above displays even when the user clicks on that "X". I hope that clarifies things.
 
Upvote 0
Instead of vbOKOnly use the vbYesNo and ask the user "Are you sure you want to close without bla bla bla"
 
Upvote 0
Yes, the regular close button. And I understand now what that code is for and why it runs. Instead of "cmbPrdCde_Exit(ByVal Cancel As MSForms.ReturnBoolean)" maybe try "cmbPrdCde_Change()"

I think it's firing because you have the focus set to the combobox and when you close the form (or try to) it..."exits" the combobox. You could also try to set the focus to something else when the userform initializes unless you just have to have it on the combobox.
 
Upvote 0
I would remove that code from the combobox and use this one in the close event of the userform

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 And cmbPrdCde.Value = "" Then
    If MsgBox("Are you sure you want to exit without selecting a product?", vbYesNo, "Select Product?") = vbNo Then
    Cancel = 1
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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