Home

Simple Windows Controls

The Label Control

Introduction

A label is a static control that displays fixed text to the user. The user cannot change the text of a label but can read it. To support labels, Microsoft Access VBA provides a class named Label.

Practical LearningPractical Learning: Introducing Windows Controls

  1. Start Microsoft Access
  2. Click Blank Desktop Database
  3. Set the file name to FunDS1
  4. Click Create
  5. On the Ribbon, click File and click Options
  6. In the left list of the Access Options dialog box, click Current Database
  7. In the main list, click Overlapping Windows
  8. Click OK on the dialog box
  9. Click OK on the message box
  10. On the Ribbon, click File and click Close
  11. In the list of files, click FunDS1
  12. On the Ribbon, click Create and, in the Forms section, click Form Design
  13. In the Property Sheet, click the All tab and change the following characteristics:
    Caption: Fun Department Store
    Auto Center: Yes
    Record Selectors: No
    Navigation Buttons: No
    Min Max Buttons: Min Enabled
  14. Set the Back Color of the Detail section to
    Red: 240
    Green: 235
    Blue: 225
  15. Save the form as Department Store

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 Label and click the form or report. You must also type the title of the label (if you click the Label 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 LearningPractical Learning: Introducing Labels

  1. On the Ribbon, click the Label Label
  2. Click the form (no precision requied)
  3. Type Tables and press Enter
  4. While the label is still selected on the form, on the Ribbon, click the Format tab
  5. In the Font section of the Ribbon, click the arrow of the Font combo box and select Bodoni MT Black (if you don't have that font, select Garamond or Times New Roman)
  6. Still in the Font section of the Ribbon, click the arrow of the Font Size button and select 20
  7. Still in the Font section of the Ribbon, click the arrow of the Font Color button and select Black, Text 1
  8. Save the Department Store form

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 LearningPractical Learning: Using the Characteristics of a Label

  1. While the label is still selected, to resize it, double-click one of its borders
  2. Save the Department Store form
  3. On the Ribbon, click File and click New
  4. Click Blank Desktop Database
  5. Set the file name to Whirl Water Shine1
  6. Click Create
  7. On the default table, double-click ID, press F2 and press Home
  8. Type WaterMeter to get WaterMeterID
  9. Right-click the tab of the table and click Design View
  10. In the text box, type WaterMeters and click OK
  11. Complete the table as follows:
    Field Name Field Size Caption
    MeterID   Meter ID
    MeterNumber 20 Meter #
    Make 40  
    Model 40  
    MeterSize 20 Meter Size
  12. Save and close the table
  13. On the Ribbon, click Create and, in the Tables section, click Table
  14. On the table, double-click ID, press F2 and press Home
  15. Type Customer to get CustomerID, and press Enter
  16. Right-click the tab of the table and click Design View
  17. In the Table Name text box, type Customers and press Enter
  18. Complete the table as follows:
    Field Name Field Size Caption
    CustomerID   Customer ID
    AccountNumber 20 Account #
    MeterNumber 20 Meter #
    FirstName 20 First Name
    LastName 20 Last Name
    Address 100  
    City 40  
    County 40  
    State 2  
    ZIPCode 20 ZIP Code
  19. Save and close the table
  20. On the Ribbon, click File and click Options
  21. In the left list of the Access Options dialog box, click Current Database
  22. In the main list, click Overlapping Windows and click OK on the dialog box
  23. Click OK on the message box
  24. On the Ribbon, click File and click Close
  25. In the list of files, click Whirl Water Shine1
  26. On the Ribbon, click Create and, in the Forms section, click Form Design
  27. In the Property Sheet, click the All tab and change the following characteristics:
    Caption: Whirl Water Shine - Bill Preparation
    Auto Center: Yes
    Record Selectors: No
    Navigation Buttons: No
    Dividing Lines: Yes
    Min Max Buttons: Min Enabled
  28. Right-click the body of the form and click Form Header/Footer
  29. Set the Back Color of the Form Header to white
  30. Set the Back Color of the Detail section to Background Form
  31. Set the Back Color of the Form Footer sections to black
  32. Save the form as Bill Preparation
  33. In the Controls section of the Ribbon, click the Label and click the Form Header section of the form
  34. Type Whirl Water Shine - Bill Preparation and press Enter
  35. While the label is still selected, in the All tab of the Property Sheet, change the following characteristics:
    Width: 5.20
    Height: .425
    Top: .0417
    Left: 1.75
    Font Name: Bodoni MT (if you don't have that font, use Garamond)
    Font Size: 24
    Font Weight: Bold
    Fore Yellow: #2F3699
  36. In the Controls section of the Ribbon, click the Label and click the Detail section of the form
  37. Type Customer Account Information and press Enter
  38. While the label is still selected, in the All tab of the Property Sheet, change the following characteristics:
    Width: 7.35
    Height: .2
    Top: .125
    Left: .165
    Back Color: Background 1, Darker 15%
    Font Name: Colonna MT (if you don't have that font, use Times New Roman)
    Font Size: 14
    Fore Yellow: black
  39. Save the form
  40. On the Ribbon, click File and click Open
  41. In the list of files, click FunDS1
  42. In the Navigation Pane, right-click the Department Store form and click Design View

The Line Control

Introduction

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 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 LearningPractical Learning: Introducing the Line

  1. In the Controls section of the Ribbon, click the Line
  2. Click the form
  3. Save the Department Store form

The Rectangle

Introduction

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

Introduction

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 Option Group 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.

 
 
 

Introduction to Text Boxes

Overview

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 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 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 LearningPractical Learning: Adding a Text Box to a Form

  1. On the Ribbon, click File and click Open
  2. In the list of files, click Whirl Water Shine1 used earlier
  3. In the Navigation Pane, right-click the Bill Preparation form and click Design View
  4. In the Controls section of the Ribbon, click the Text Box Text Box and click the Detail section of the form
  5. While the text box is still selected on the form, in the All table of the Property Sheet, click Name and type txtAccountNumber

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.

ApplicationTopic Applied: Applying a Special Effect to a Text Box

  1. While the text box is still selected on the form, in the All tab of the Property Sheet, click Special Effect, then click the arrow of its combo box and select Shadowed
  2. On the Ribbon, click the Format tab
  3. In the Font section of the Ribbon, click the arrow of the Font Name combo box and select Bodoni MT (if you don't have that font, select Times New Roman)
  4. Complete the design of the form as follows:

    Whirl Water Shine - Bill Preparation

    Control Caption Name Other Properties
    Label Label  Whirl Water Shine - Bill Preparation    
    Label Label Customer Account Information    
    Text Box Text Box Account #: txtAccountNumber  
    Text Box Text Box Customer Name: txtFirstName  
    Text Box Text Box   txtLastName  
    Text Box Text Box Address: txtAddress  
    Text Box Text Box   txtCity  
    Text Box Text Box   txtCounty  
    Text Box Text Box   txtState  
    Text Box Text Box   txtZIPCode  
    Line Line      
    Label Label Meter Information    
    Text Box Text Box Meter Details: txtMeterInformation  
    Text Box Text Box Service From (Date): txtServiceFromDate Format: Fixed
    Text Box Text Box Service To: txtServiceToDate Format: Fixed
    Text Box Text Box Number of Days: txtNumberOfDays Format: Fixed
    Decimal Places: 0
    Text Box Text Box Meter Reading Start: txtMeterReadingStart Format: Fixed
    Text Box Text Box Reading End: txtMeterReadingEnd Format: Fixed
    Text Box Text Box Total HCF: txtTotalHCF Format: Fixed
    Text Box Text Box Total Gallons: txtTotalGallons Format: Fixed
    Decimal Places: 0
    Text Box Text Box 1st 15 HCF at $3.6121: txtFirst15HCF Format: Fixed
    Text Box Text Box Next 10 HCF at $3.9180: txtNext10HCF Format: Fixed
    Text Box Text Box Remaining HCF at $ 4.2763: txtRemainingHCF Format: Fixed
    Text Box Text Box Sewer Charge: txtSewerCharge Format: Fixed
    Text Box Text Box Storm Charge: txtStormCharge Format: Fixed
    Text Box Text Box Water Usage Charge: txtWaterUsageCharge Format: Fixed
    Label Label Bill Values    
    Text Box Text Box County Taxes: txtCountyTaxes Format: Fixed
    Text Box Text Box State Taxes: txtStateTaxes Format: Fixed
    Line Line      
    Text Box Text Box Amount Due: txtAmountDue Format: Fixed
    Text Box Text Box Late Payment Amt: txtLatePaymentAmount Format: Fixed
  5. On the form, click the txtMeterReadingEnd text box
  6. In the Property Sheet, double-click On Lost Focus, then click its ellipsis button Browse
  7. Implement the event as follows:
    Private Sub txtMeterReadingEnd_LostFocus()
        Dim waterUsageCharge As Double
        Dim totalHCF As Double, totalGallons As Double
        Dim countyTaxes As Double, stateTaxes As Double
        Dim amountDue As Double, latePaymentAmount As Double
        Dim first15HCF As Double, next10HCF As Double, remainingHCF As Double
        Dim sewerCharge As Double, stormCharge As Double, totalCharges As Double
        
        If IsNull(txtMeterReadingStart) Then
            Exit Sub
        End If
        
        If IsNull(txtMeterReadingEnd) Then
            Exit Sub
        End If
        
        totalHCF = CDbl(Nz(txtMeterReadingEnd)) - CDbl(Nz(txtMeterReadingStart))
        ' 1 HCF = 748.05 Gallons
        totalGallons = totalHCF * 748.05
        
        If totalHCF <= 15# Then
            first15HCF = totalHCF * 3.612
            next10HCF = 0#
            remainingHCF = 0#
        ElseIf totalHCF <= 25# Then
            first15HCF = 15# * 3.612
            next10HCF = (totalHCF - 15) * 3.918
            remainingHCF = 0#
        Else ' If totalHCF > 25# Then
            first15HCF = 15# * 3.612
            next10HCF = 10 * 3.918
            remainingHCF = (totalHCF - 25) * 2.2763
        End If
        
        waterUsageCharge = first15HCF + next10HCF + remainingHCF
        sewerCharge = waterUsageCharge * 0.252
        stormCharge = waterUsageCharge * 0.0025
        
        totalCharges = waterUsageCharge + sewerCharge + stormCharge
        countyTaxes = totalCharges * 0.005
        stateTaxes = totalCharges * 0.0152
        
        amountDue = totalCharges + countyTaxes + stateTaxes
        latePaymentAmount = amountDue + 8.95
        
        txtTotalHCF = FormatNumber(totalHCF)
        txtTotalGallons = FormatNumber(totalGallons, 0)
        txtFirst15HCF = FormatNumber(first15HCF)
        txtNext10HCF = FormatNumber(next10HCF)
        txtRemainingHCF = FormatNumber(remainingHCF)
        txtSewerCharge = FormatNumber(sewerCharge)
        txtStormCharge = FormatNumber(stormCharge)
        txtWaterUsageCharge = FormatNumber(waterUsageCharge)
        txtTotalCharges = FormatNumber(totalCharges)
        
        txtCountyTaxes = FormatNumber(countyTaxes)
        txtStateTaxes = FormatNumber(stateTaxes)
        txtAmountDue = FormatNumber(amountDue)
        txtLatePaymentAmount = FormatNumber(latePaymentAmount)
    End Sub
  8. Close Microsoft Visual Basic and return to Microsoft Access
  9. Save the Bill Preparation form
  10. Close Microsoft Access
 
 
   
 

Previous Copyright © 2016, FunctionX, Inc. Next