Writing a Procedure that Closes the User Form

After displaying the form, the user may want to cancel the form by pressing Esc or clicking the Cancel button. To remove the form from the screen, prepare a simple procedure that uses the Hide method.

1. Double-click the Cancel button and enter the following cmdCancel_Click procedure:

Private Sub cmdCancel_Click()

Me.Hide End Sub

The Hide method hides the object but does not remove it from memory. This way, your VBA procedure can use the form's objects and properties behind the scenes while the form isn't visible to the user.

Use the Unload method to remove the form from the screen and release memory resources:

Unload Me

When the form is unloaded, all memory associated with it is reclaimed. The user can't interact with the form, and the form's objects can't be accessed by your VBA procedure until the form is placed in the memory again by using the Load statement.

0 0

Post a comment