Page 271 - Excel Progamming Weekend Crash Course
P. 271

n540629 ch19.qxd  9/2/03  9:35 AM  Page 246




                246                                                         Saturday Evening


               Displaying, Using, and Hiding Forms

               The sequence of steps involved in using a form in your application is summarized here.
               These steps assume that the form design has been completed (or at least has progressed
               enough to permit testing). This code assumes that the Name property of the user form is
               MyUserForm.
                 1. Create an instance of the user form, at the same time declaring a variable that ref-
                    erences the form.
                     Dim frm As New MyUserForm
                 2. If necessary, use the form reference to set values of properties for the form and its
                    controls.
                     frm.Backcolor = RGB(210, 210, 210)
                     frm.TextBox1.Value = “Some text”

                 3. Display the form to the user.
                     frm.Show
                 4. At this point the user interacts with the form by entering data, selecting options,
                    and performing other actions for which the form was designed.
                 5. When finished, the user takes some action to close the form, typically by clicking
                    a button on the form. Code in the form executes the Hide method to hide the
                    form.
                     Me.Hide
                 6. Code in your program can now retrieve information from the form’s controls as
                    needed.
                     Response = frm.TextBox1.Value

                 7. Unless you want to use the form again, destroy it by setting its reference to
                    Nothing in order to free up the memory that the form used.
                     Set frm = Nothing


                          In code that is part of a form, the Me keyword is used to refer to the form
                          (see step 5 above). Because the form reference is implicit within the form’s
                   Tip    code, you can also use property and method names without the Me qualifier.
                          Thus, the code in step 5 above could simply say Hide.

                          Be aware that only the line of code in step 5 above is part of the form. All
                          the remaining code is in your program, which is part of a VBA module.
                   Note
   266   267   268   269   270   271   272   273   274   275   276