Pages

Sunday, December 18, 2011

Understanding The Label Control On UserForms In Excel

By Brenda Barker


If you are booked in to attend a basic Excel VBA training course, be sure to check whether it contains coverage of UserForms. This simple facility allows you to create pop-up forms which can be used for outputting and inputting some information required by you Excel application in a very user-friendly way. User forms can contain a variety of controls with which the user can interact.

By far the most frequently used control is also one of the most basic: the Label control. This allows you to display text anywhere on the form and, typically, each input control has a Label control next to it which clarifies its purpose. The caption property of the Label control determines the text which it displays. Thus, if we were creating a form which allowed users to enter expense claims, we might have Label controls with captions like "Name:", "Department:", "Amount", and so forth.

Ensuring that each of your controls has a descriptive name, rather than accepting default names such as "Label1", is good practice. Although you can set the properties of your controls at design time using the Properties Window, you can also manipulate controls programmatically.

In the interests of clarity, it is useful if each control has a name which relates to its function. It is also useful to have the name of each control start with a short prefix denoting its type. This prefix should apply to all objects in your project, including variable names. The statement "strName = frmExpenses.txtName.Value" makes much more sense to someone reading the code than "Variable1.UserForm1.TextBox3.Value".

One of the most common reasons for referring to Label controls in your code is to communicate dynamically with the user. This is done by setting the Caption or Visible property of Label controls programmatically. Thus, let's say you had a series of radio buttons allowing the user to specify the type of expense they were claiming for, your final button might be "Other". If the user chooses "Other", you might then want a Label control bearing the caption "Please Specify", and which was hidden at design time, to suddenly become visible. This can be achieved simply by using a VBA statement like "frmExpenses.lblSpecify.Visible = True".




About the Author:



No comments:

Post a Comment