Page 282 - Excel Progamming Weekend Crash Course
P. 282
n540629 ch20.qxd 9/2/03 9:35 AM Page 257
Session 20 — Controls for User Forms 257
1. Add a new user form to a project.
2. Change its Name property to CommandButtonDemo and its Caption property to
CommandButton Demo.
3. Add a CommandButton to the form. Change its Name property to cmdOK, its
Caption property to OK, and its Default property to True.
4. Add a second CommandButton to the form. Change its Name property to
cmdCancel, its Caption property to Cancel, and its Cancel property to True.
5. Open the code-editing window for the form, and select (General) from the list at
the top left of the window.
6. Enter this single line of code: Public Cancelled As Boolean.
7. Add Click event procedures for each CommandButton, and add the code shown in
Listing 20-3.
Listing 20-3 Code for the two CommandButton controls’ Click event procedures
Private Sub cmdCancel_Click()
Cancelled = True
Me.Hide
End Sub
Private Sub cmdOK_Click()
Cancelled = False
Me.Hide
End Sub
Now that the form is complete, create the procedure that displays the form. In a code
module in your project, add a new procedure named TestCommandButtonDemo and insert
the code shown in Listing 20-4.
Listing 20-4 Code to run the CommandButton demo form
Public Sub TestCommandButtonDemo()
Dim frm As New CommandButtonDemo
frm.Show
If frm.Cancelled = True Then
MsgBox “You closed the form with the Cancel button”
Else
MsgBox “You closed the form with the OK button”
End If
End Sub