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 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
Field Name | Field Size | Caption |
MeterID | Meter ID | |
MeterNumber | 20 | Meter # |
Make | 40 | |
Model | 40 | |
MeterSize | 20 | Meter Size |
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 |
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 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
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 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.
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 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
Control | Caption | Name | Other Properties | |
Label | Whirl Water Shine - Bill Preparation | |||
Label | Customer Account Information | |||
Text Box | Account #: | txtAccountNumber | ||
Text Box | Customer Name: | txtFirstName | ||
Text Box | txtLastName | |||
Text Box | Address: | txtAddress | ||
Text Box | txtCity | |||
Text Box | txtCounty | |||
Text Box | txtState | |||
Text Box | txtZIPCode | |||
Line | ||||
Label | Meter Information | |||
Text Box | Meter Details: | txtMeterInformation | ||
Text Box | Service From (Date): | txtServiceFromDate | Format: Fixed | |
Text Box | Service To: | txtServiceToDate | Format: Fixed | |
Text Box | Number of Days: | txtNumberOfDays | Format: Fixed Decimal Places: 0 |
|
Text Box | Meter Reading Start: | txtMeterReadingStart | Format: Fixed | |
Text Box | Reading End: | txtMeterReadingEnd | Format: Fixed | |
Text Box | Total HCF: | txtTotalHCF | Format: Fixed | |
Text Box | Total Gallons: | txtTotalGallons | Format: Fixed Decimal Places: 0 |
|
Text Box | 1st 15 HCF at $3.6121: | txtFirst15HCF | Format: Fixed | |
Text Box | Next 10 HCF at $3.9180: | txtNext10HCF | Format: Fixed | |
Text Box | Remaining HCF at $ 4.2763: | txtRemainingHCF | Format: Fixed | |
Text Box | Sewer Charge: | txtSewerCharge | Format: Fixed | |
Text Box | Storm Charge: | txtStormCharge | Format: Fixed | |
Text Box | Water Usage Charge: | txtWaterUsageCharge | Format: Fixed | |
Label | Bill Values | |||
Text Box | County Taxes: | txtCountyTaxes | Format: Fixed | |
Text Box | State Taxes: | txtStateTaxes | Format: Fixed | |
Line | ||||
Text Box | Amount Due: | txtAmountDue | Format: Fixed | |
Text Box | Late Payment Amt: | txtLatePaymentAmount | Format: Fixed |
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
|
||
Previous | Copyright © 2022, FunctionX, Inc. | Next |
|