The Label Control
Practical Learning: Introducing Windows Controls
Creating a Label
To visually add a label to a form or report, display the form or report in Design View. In the Controls section of the Design tab of the Ribbon, click Label and click the form or report. You must also type the title of the label (if you click the Label and click the form or report but you click somewhere else, the label would disappear.
To programmatically create a label, call the CreateControl() function and pass the ControlType argument as acLabel. The first argument is the name of the form or report on which the label will be positioned. Here is an example:
Private Sub cmdCreateControl_Click() Dim ctlFirstName As Control Set ctlFirstName = CreateControl("Exercise", _ AcControlType.acLabel) Set ctlFirstName = Nothing End Sub
Of course, the third argument is the section of the form or report where the label will be positioned. You can pass the fourth argument as the name of the form or report on which the label will be positioned. That is, the first and the fourth arguments can be the same. Here is an example:
Private Sub cmdCreateControl_Click() Dim ctlFirstName As Control Set ctlFirstName = CreateControl("Exercise", _ AcControlType.acLabel, _ AcSection.acDetail, _ "Exercise") Set ctlFirstName = Nothing End Sub
Characteristics of a Label
The Caption of a Label
A label cannot be used directly for data entry. That is, you cannot link a label to a column of a table. A label is only used to display static text. If you are programmatically creating the label, pass the fifth argument as an empty string.
Probably the most important and the most obvious characteristic of a label is the text it displays. The text is the label's Caption property. If a label already has a caption, there are various ways you can edit it. For example, you can click it and click again to put it into edit mode, then edit its string. You can also double-click it to access its Properties window and change its Caption property. You can also programmatically edit a label by assigning a string to this property. Here is an example:
Private Sub cmdToday_Click() lblToday.Caption = Date End Sub
Practical Learning: Introducing Labels
The Size of a Label
To specify the size of a label, after clicking it on the form or report, drag one of the dots around it. If you want to label to fit its caption, double-click one of its borders.
If you are programmatically creating the label, to specify its size, pass a sixth argument as its left position and/or a seventh argument as its top position. Here are examples:
Private Sub cmdCreateControl_Click() Dim ctlFirstName As Control Set ctlFirstName = CreateControl("Fundamentals", _ AcControlType.acLabel, _ AcSection.acDetail, _ "Exercise", _ "", _ 320, 260, 1200, 260) ctlFirstName.Caption = "First Name:" Set ctlFirstName = Nothing End Sub
Practical Learning: Using the Characteristics of a Label
The Line Control
As its name indicates, the line is a static control that represents a horizontal, diagonal, or vertical line. To add a control to a form or report in Design View, in the Controls section of the Ribbon, click the Line and click the form or report.
Characteristics of a Line
To let you enhance the appearance of a line, it is equipped with such properties as the Border Color, the Border Width, and the Border Style. You can access them from either the Property Sheet or the Shape Outline button in the Format tab of the Ribbon.
As a classic static control, the line doesn't have events.
Practical Learning: Introducing the Line
The rectangle control is a static quadrilateral object you can use in your application for its aesthetic appearance. To add a rectangle to a form or report in Design View, in the Controls section of the Ribbon, click the Rectangle and click the form or report.
Characteristics of a Rectangle
The rectangle uses the same characteristics as the linie for its borders. These include the Border Color, the Border Width, and the Border Style. Unlike a line, a rectangle has a body. This makes it possible to paint the rectangle with a background color.
Unlike the line, the rectangle control has events. It supports the click (On Click and On Dbl Click) and the mouse events (On Mouse Move, On Mouse Down), and On Mouse Up). This means that you can use the rectangle control as a basic button.
The Group Box
A group box is a rectangular control whose primary purpose is to organize the layout of other controls positioned on a form or report. At least that's the way the group box works in the Win32 library. In Microsoft Access, the group box goes beyond that static definition.
To visually create a group box, in the Controls section of the Ribbon, click the Option Group control and click the form or report in Design View. If a wizard starts, since you only want a group box, click Cancel.
Characteristics of a Group Box
A group box appeas like a rectangle with a visible border. As done for any control, you can paint a group box with a background color of your choice. You can almost enhance its appearance with the Special Effect property.
To indicate its purpose, a group box is equipped with a label, usually positioned on its top border. The group box acts as the parent of its label. As a result, if you move the group box, it moves with the label. Still, you can set some of their characteristics (Back Style, Back Color, Border Style, Border Color, Special Effect, etc) independently.
A text box is a Windows control used to get or display text. The text box may be accompanied by a label that indicates its purpose.
A text box is managed by a class named TextBox.
Creating a Text Box
To visually add a text box to a form or report, from the Controls section of the Ribbon, click the Text Box control and click a section of the form or report. Unless you have a good alternate reason, most of your text boxes will be placed in the Detail section. Some text boxes used in expressions can be placed in another section. By default, placing a text box on the form or report also adds a corresponding label to its left.
To programmatically create a text box, call the CreateControl() method of the Application class and pass the ControlType argument as acTextBox. Here is an example:
Private Sub cmdCreateControl_Click() Dim txtFirstName As Control Set txtFirstName = CreateControl("Exercise", _ AcControlType.acTextBox, _ AcSection.acDetail) Set txtFirstName = Nothing End Sub
Practical Learning: Adding a Text Box to a Form
The Characteristics of a Text Box
The Control Source of a Text Box
In a database, a text box is typically used to display the value of a field of a table. To make this possible, after specifying the Record Source of its form or report and after visually adding a text box, set its Control Source to the desired field of the table. If you are programmatically creating the text box, to specify the field it must link to, pass the name of the table as the fourth argument and pass the name of the field as the fifth argument. This can be done as follows:
Private Sub cmdCreateControl_Click() Dim txtFirstName As Control Set txtFirstName = CreateControl("Exercise", _ AcControlType.acTextBox, _ AcSection.acDetail, _ "Students", "FirstName", 1600, 260, 1760, 260) Set txtFirstName = Nothing End Sub
The Text of a Text Box
To programmatically specify or change the text property of an object, if the property is called Text, it is considered the default property of the control. That's the case for the text box. This means that, to specify the value of that property, simply type the name of the control and assign the desired string. Here is an example:
Private Sub cmdSpecifyFullName_Click() txtDescriptione = "Universal Subscription" End Sub
During design, the text you provide to the control is static. If you want the text to change the text in response to something, you must write code. Here is an example:
Private Sub cmdCurrentDate_Click() Caption = Date End Sub
With this example, every time the button is clicked, the form would display today's date.
If you are creating a text box that will not link to a field of a table, you can pass the fourth argument as either the same as the first argument or an empty string, and then pass the fifth argument also as an empty string.
A Read-Only Text Box
To make a text box read-only, that is, if you don't want the user to enter text in an edit box, there are various options. If you change the Enabled property from Yes to No or from True to False, the text box would have a gray background and cannot receive focus. If you set the Locked property from No or False to Yes or True, the control would appear with a normal (white) background.
The Special Effect of a Text Box
The Special Effect property of the text box is expanded as compared to that available on a label. Besides the ability to raise or sink a text box, you can give it a thick, etched, or shadow border.
Topic Applied: Applying a Special Effect to a Text Box