Microsoft Access Database Development With VBA

Windows Controls: The Label

   

Description

A label is a static control that displays fixed text to the user. The user cannot change the text of a label but can only read it. A label can be used by itself to display text. In many other cases, a label is used to display text about another control next to it.

To 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 Label and click the form or report. You must also type the title of the label (if you don't and click somewhere else, the label would disappear. The label control is an object of type Label.

To programmatically create a label, call the CreateConotrol() function and pass the ControlType 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 argument 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

Properties 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 that cannot be changed. As a consequence, if you are visually creating a label, you cannot specify its Control Source in the Properties window. 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. If you click the Label on the Ribbon and click on the form or report, you must first define its caption. If a label already has a caption, there are various ways you can edit it. For example, 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.

If you are programmatically creating the label, to specify its caption, access the control's Caption property and assign the desired string to it. Here is an example:

Private Sub cmdCreateControl_Click()
    Dim ctlFirstName As Control
    
    Set ctlFirstName = CreateControl("Exercise", _
                                     AcControlType.acLabel, _
                                     AcSection.acDetail, _
                                     "Exercise")
    
    ctlFirstName.Caption = "First Name:"

    Set ctlFirstName = Nothing
End Sub

The appearance of a label is visibly controlled by its font characteristics. The Font name, also called its face, is the name of the font as defined by the operating system. There are various ways you can define the font characteristics of a control like the label. After selecting it while the form or report is in Design View, on the Ribbon, you can click Home or Design, then use the buttons in the Font section to specify the font characteristics. You can also access the Properties window of the label and modify the desired font properties.

The position of a label is controlled by its Top and Left properties. The Top property defines the measure from the top left corner of the section where the label is positioned, to the top left corner of the label itself. There are two main ways you can set the position of a label. On the Properties window, you can change the values of the Top and Left properties. On the other hand, you can place your mouse on the top left corner of the label until the mouse pointer turns into a pointing finger. Then click and drag in the desired direction.

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)
    
    ctlFirstName.Caption = "First Name:"

    Set ctlFirstName = Nothing
End Sub

The size of a label is controlled by its Width and Height properties. Although the dimensions are closely related to the font characteristics, they can be independently defined. There are two main ways you can resize a label, which is equivalent to changing its dimensions. To set your own dimensions, in the Format tab of the Properties window of the label, change the values of the Width and Height properties. Unless you plan to show the background color of a label, probably the best way to resize a label is to make it adjust to the dimensions depending on the font size and the total characters width. To do this, position the mouse on one of the label's handle and double-click. The label's width and height would be set to accommodate its caption.

If you are programmatically creating the label, to specify its size, pass an 8th argument as its width and/or a ninth argument as its height. 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
 
 
     
 

Home Copyright © 2012 FunctionX, Inc. Home