Using information from a User Form

The VBE provides a name for each control you add to a UserForm. The control's name corresponds to its Name property. Use this name to refer to a particular control in your code. For example, if you add a CheckBox control to a UserForm named UserForm1, the CheckBox control is named CheckBox1 by default. The following statement makes this control appear with a check mark:

UserForml.CheckBoxl.Value = True

Most of the time, you write the code for a UserForm in the UserForm's code module. If that's the case, you can omit the UserForm object qualifier, and write the statement like this:

CheckBoxl.Value = True

Your VBA code can also check various properties of the controls and take appropriate actions. The following statement executes a macro named PrintReport if the check box (named CheckBoxl) is checked:

If CheckBox1.Value = True Then Call PrintReport I discuss this topic in detail in Chapter 17.

I recommend that you change the default name the VBE has given to your controls to something more meaningful. You might consider naming the check box described above "cbxPrintReport." Note that I precede the name with a three-letter prefix, indicating the type of control. It is a matter of taste whether you think doing so is a good practice or not.

0 0

Post a comment