command buttons not working

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Code:
Private Sub cmbSDPFLine_Exit(ByVal Cancel As MSForms.ReturnBoolean)    Dim rtn_ans1 As String


    If cmbSDPFLine.Value = "" Then
        rtn_ans1 = MsgBox("Please select a production line.", vbOKOnly, "Please Select Production Line")
        Select Case rtn_ans1
            Case 1
                Cancel = True
                cmbSDPFLine.SetFocus
         End Select
    End If
End Sub
What I am trying to achieve with the above code is when the user exits the required text box above it will return back to that text box until a selection is made. Which it does. I also have a command button labeled cancel.
Code:
Private Sub cmdbtnCancel_Click()    Unload Me
End Sub

The problem that I am having is if the user wants to cancel out of the form without making a selection, and clicks the Cancel Button, the above message box keeps displaying until the user makes a selection and then clicks the cancel button. Also if the user clicks on the white "X" with the red background on the form the message box also displays but once the user acknowledges the OK button the form closes. So what I like is once the user clicks on the white X or the clicks the cancel button I need the form to close without displaying the message box. How can I achieve this? Thank You.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You need to stop the code in the Exit event of cmbDSPFLine running when the form is being closed.

To do that you can use a boolean variable to 'disable' events.

Let's call that variable frmEnableEvents and declare it at the top of the userform module.

Code:
Private frmEnableEvents As Boolean

In the form's Initialize event we set it's value to True so that all events are enabled.
Code:
Private Sub UserForm_Initialize()

    Me.frmEnableEvents = True

    ' other initialize code

End Sub
Now to stop the code in Exit event of cmbDSPFLine running we can change your posted code like this.
Code:
Private Sub cmbSDPFLine_Exit(ByVal Cancel As MSForms.ReturnBoolean)    Dim rtn_ans1 As String

    If Me.frmEnableEvents = False Then Exit Sub

    If cmbSDPFLine.Value = "" Then
        rtn_ans1 = MsgBox("Please select a production line.", vbOKOnly, "Please Select Production Line")
        Select Case rtn_ans1
            Case 1
                Cancel = True
                cmbSDPFLine.SetFocus
         End Select
    End If

End Sub

' ... other code

Private Sub cmdbtnCancel_Click()
    Me.frmEnableEvents = False
    Unload Me
    Me.frmEnableEvents = True
End Sub

If there are any other subs that you have code you don't want executed when the form closes just add this at the start.
Code:
If Me.frmEnableEvents = False Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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