Microsoft Office Tutorials and References
In Depth Information
Modal Versus Modeless
UserForms have a QueryClose event that can help you control such situations. In Figure 20-2, a
Message Box appears if the “X” Close button was clicked in an attempt to close the UserForm
without selecting a name from the drop-down list. The code associated with that follows Figure 20-2.
figurE 20-2
Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
‘Prevents use of the Close button
‘if a name has not been selected.
If CloseMode = vbFormControlMenu Then
If Len(ComboBox1.Value) = 0 Then
Cancel = True
MsgBox _
“You must select a name to continue.”, _
vbExclamation, _
“Name is required”
‘Set Focus to the ComboBox for the user.
ComboBox1.SetFocus
End If
End If
End Sub
Keep in mind that you’ll want to monitor the input requirement through the other controls on the
UserForm as well. The following example is associated with the Continue button:
Private Sub cmdContinue_Click()
If Len(ComboBox1.Value) = 0 Then
MsgBox _
“You must select a name to continue.”, _
vbExclamation, _
“Name is required”
‘Set Focus to the ComboBox for the user.
ComboBox1.SetFocus
Exit Sub
Else
Unload Me
End If
End Sub
Search JabSto ::




Custom Search