18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating UserForms 41<br />

FIGURE 41.11<br />

Displaying the UserForm.<br />

Creating event-handler procedures<br />

This section explains how to create two event-handler procedures: one to handle the Click event for<br />

the CancelButton CommandButton and the other to handle the Click event for the OKButton<br />

CommandButton. Event handlers for the OptionButton controls are not necessary. The VBA code can<br />

determine which of the three OptionButton controls is selected.<br />

Event-handler procedures are stored in the UserForm code module. To create the procedure to handle the<br />

Click event for the CancelButton, follow these steps:<br />

1. Activate the UserForm1 form by double-clicking its name in the Project window.<br />

2. Double-click the CancelButton control. The VB Editor activates the code module for the<br />

UserForm and inserts an empty procedure.<br />

3. Insert the following statement before the End Sub statement:<br />

Unload UserForm1<br />

That’s all there is to it. The following is a listing of the entire procedure that’s attached to the Click event<br />

for the CancelButton:<br />

Private Sub CancelButton_Click()<br />

Unload UserForm1<br />

End Sub<br />

This procedure is executed when the CancelButton is clicked. It consists of a single statement that unloads<br />

the UserForm1 form.<br />

729

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!