Microsoft Access Database Development With VBA

Windows Controls: The Text Box

   

Description

A text box is a Windows control used to get or display text. At its most regular use, a text box serves as a place to fill out and provide information. You can also use it only to display text without allowing the user to change its content.

Like most other controls, the role of an edit box is not obvious at first glance; that is why it should be accompanied by a label that defines its purpose. From the user's standpoint, a text box is named after the label closer to it. Such a label is usually on the left or the top side of the corresponding edit box.

Creating a Text Box

To add a text box to your project, from the Controls section of the Ribbon, you can click the Text Box control Text Box 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 CreateConotrol() function and pass the ControlType as acTextBox. The first argument is the name of the form or report on which the text box will be positioned. The third argument specifies the section where to position the text box. 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

Properties of a Text Box

As mentioned already, a text box can be used to display text, just like a normal control in a Microsoft Windows application. 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 could be done as follows:

Private Sub cmdCreateControl_Click()
    Dim txtFirstName As Control
    
    Set txtFirstName = CreateControl("Exercise", _
                                     AcControlType.acTextBox, _
                                     AcSection.acDetail, _
                                     "Students", "FirstName")

    Set txtFirstName = Nothing
End Sub

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.

Like every control on a form or report, the dimensions of the text box are controlled by its Width and Height properties. You can change these in the Properties window of the text box. You can also specify them if you are programmatically creating the control. Here are examples:

Private Sub cmdCreateControl_Click()
    Dim txtFirstName As Control
    
    Set txtFirstName = CreateControl("Exercise", _
                                     AcControlType.acTextBox, _
                                     AcSection.acDetail, _
                                     "Exercise", _
                                     "", 1600, 260)

    Set txtFirstName = Nothing
End Sub

The position of a text box is controlled by its Top and Left properties. You can change them in the Properties window of the text box. You can also specify them when programmatically creating the control. Here are examples:

Private Sub cmdCreateControl_Click()
    Dim txtFirstName As Control
    
    Set txtFirstName = CreateControl("Fundamentals", _
                                     AcControlType.acTextBox, _
                                     AcSection.acDetail, _
                                     "Fundamentals", _
                                     "", 1600, 260, 1760, 260)

    Set txtFirstName = Nothing
End Sub

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 alternatives. If you change the Enabled property from Yes to No, the text box would have a gray background and cannot receive focus. If you set the Locked property from No to Yes, the control would appear with a normal (white) background.

The Special Effects 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.

 
 
     
 

Home Copyright © 2012 FunctionX, Inc. Home