Illegal Window Closing

The userforms created in the VBE are not that different than normal windows: They also include the X close button in the upper right-hand corner. Though using the button is not wrong, it can cause problems, depending on the objective of the userform. In cases like this, you may want to control what happens if the user presses the button. Use the QueryClose event of the userform to find out what method is used to close the form and code an appropriate action:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = vbFormControlMenu Then

MsgBox "Please use the OK or Cancel buttons to close the form", vbCritical Cancel = True End If End Sub

After you know the method the user used to try and close the form, you can create a MsgBox similar to Figure 9.15, to warn the user that the method was illegal.

Figure 9.15

Control what happens when the user clicks the X button.

Figure 9.15

Control what happens when the user clicks the X button.

The QueryClose event can be triggered in three other ways:

■ vbFormCode—The Unload statement was used.

■ vbAppWindows—Windows shuts down.

■ vbAppTaskManager—The application was shut down by the Task Manager.

0 0

Post a comment