Introduction to Windows Controls |
|
Controls Fundamentals
Introduction
A Windows control, also called a control, is an object positioned on a form or a report (or any container). It allows a user to interact with the computer or with an application such as a database. This means that, when designing the interface of a database, you add forms and reports, then populate them with the necessary objects.
Microsoft Access supports various types of controls. The most basic or the most regularly used controls are available in the Controls section of the Ribbon when the form or report is displaying in the Design View:
If you resize the Ribbon to a size that cannot show all controls, to show the hidden objects, you can click a down-pointing arrow:
Besides the objects in the Controls section of the Ribbon, Microsoft Access provides additional ones. These are referred to as ActiveX controls. To access an ActiveX control, in the Controls section of the Ribbon, click the More button and click ActiveX Controls. A dialog box will display:
Practical Learning: Introducing Forms
CREATE TABLE Agents ( AgentCode char(12), FirstName text(20), LastName text(20), Title string(50), PayFrequency varchar(25) );
CREATE TABLE Companies ( CompanyCode text(10), CompanyName varchar(60) );
ALTER TABLE Transactions ADD COLUMN AgentCode char(12);
ALTER TABLE Transactions ADD COLUMN CompanyCode char(10);
ALTER TABLE Transactions
ADD COLUMN NumberOfShares Long;
ALTER TABLE Transactions
ADD COLUMN PricePerShare double;
ALTER TABLE Transactions
ADD COLUMN PaymentStatus string(25);
Visually Adding Controls to a Form or a Report
To visually select a control, click it in the Controls section of the Ribbon. Then, on the form or report, click an area of your choice:
To visually add the same control many times to a form or report, in the Controls section of the Ribbon, right-click the control and click Drop Multiple controls:
On the form or report, click where you want to (temporarily) position the control. To dismiss the multiple choice, in the Design tab of the Ribbon, click the Select button.
Programmatically Adding a Control to a Form or Report
To visually add a Windows to a form or report, in the Controls section of the Ribbon, click the desired control and click the desired area on the form or report. To add an ActiveX controls. To add an ActiveX control to a form or report, in the Insert ActiveX Controls dialog box, click the desired control and click OK.
To let you programmatically create a control, the Application class is equipped with a method named CreateControl. Its syntax is:
Function CreateControl(ByVal FormName/ReportName As String, _ ByVal Controltype As AcControlType, ByVal Optional Section As AcSection = AcSection.acDetail, _ ByVal Optional Parent As String, _ ByVal Optional ColumnName As String, ByVal Optional Left As Integer, _ ByVal Optional Top As Integer, _ ByVal Optional Width As Integer, _ ByVal Optional Height As Integer) As Control
The first argument is the name of the form or report on which the control will be positioned. For this method to work, the form or report must be opened in Design View. The second argument is a member of the AcControlType enumeration. The controls available are:
AcControlType Member | Ribbon Control | Control Name |
acAttachment | Attachment | |
acBoundObjectFrame | Bound Object Frame | |
acCheckBox | Check box | |
acComboBox | Combo Box | |
acCommandButton | Command button | |
acCustomControl | ActiveX control | |
acImage | Image | |
acLabel | Label | |
acLine | Line | |
acListBox | List box | |
acNavigationControl | Navigation Control | |
acObjectFrame | Unbound Object Frame | |
acOptionButton | Option button | |
acOptionGroup | Option group | |
acPage | Page | |
acPageBreak | Page break | |
acRectangle | Rectangle | |
acSubform | Sub-Form | |
acTabCtl | Tab control | |
acTextBox | Text box | |
acToggleButton | Toggle button | |
acWebBrowser | Web Browser |
Only the first and the second arguments are required. Here is an example of calling the Application.CreateObject method:
Private Sub cmdCreateControl_Click()
Application.CreateControl("Central", AcControlType.acTextBox)
End Sub
Because the Application object is automatically available when you start Microsoft Access, you can omit Application in your code. In the same way, you can omit the AcControlType enumeration.
The third argument specifies in what section of the form or report the new control would be positioned. This argument is a member the AcSection enumeration. The default value of this argument is acDetail. This means that if you omit the third argument as in the above code, the control would be created in the Detail section. If you want the control to be positioned in another section, pass a third argument and specify the desired member of the AcSection enumeration. Here is an example of passing this argument:
Private Sub cmdCreateControl_Click()
CreateControl "Central", AcControlType.acTextBox, acDetail
End Sub
The fourth argument is the name of the control that would serve as the parent of the new control. It can be the name of the form or report on which the control will be positioned. Here is an example:
Private Sub cmdCreateControl_Click()
CreateControl "Central", AcControlType.acTextBox, acDetail, "Central"
End Sub
You can pass this argument as an empty string "".
Invisible Controls
Besides the Controls you can see, Microsoft Windows provides some controls that a user cannot see but can feel its effects. An example is the timer. As a programmer, you create and configure such a control and it will simply act while the user is using your database.
Using the Section of a Form or Report
When adding a control to a form or report, you must specify in what section the control will be positioned. If you are adding the controls manually, you can click the control in the Controls section of the Ribbon and click the desired section on the form or report. If you are programmatically creating the control, pass the third argument of the Application.CreateControl() method as a member the AcSection enumeration. The default value of this argument is acDetail. This means that if you omit the third argument, the control would be positioned in the Detail section.
The fourth argument is the name of the control that would serve as the parent of the new control. It can be the name of the form or report on which the control will be positioned. Here is an example:
Private Sub cmdCreateControl_Click()
CreateControl "Central", AcControlType.acTextBox, acDetail, "Central"
End Sub
You can pass this argument as an empty string "".
Practical Learning: Introduction to Fields
The Collection of Controls of a Form or Report
A form or a control is mostly used as a container because it hosts some of the controls of a database. The controls that a form or a report hosts are members of a collection named Controls.
The Properties of Windows Controls
Introduction to Controls Characteristics
Every Windows control is managed through a class. We know that a typical class is equipped with properties that define its characteristics. In the same way, the class of a Windows control has properties. As mentioned in Lesson 6, the most fundamental class for Windows controls is named Control. This class is equipped with the characteristics and methods that all or most controls share. Other than that, some controls are treated as categories where they share some characteristics that the controls in other groups don't have or don't need. Below that, some controls have some characteristics that only they need.
The Property Sheet
After adding a control to a form or report, whether you manually positioned the control or you created it programmatically, as seen for forms and repots, you can use the Property Sheet and/or the Properties window to manage the properties of the Windows control.
In Microsoft Access, to get the Property Sheet of the properties associated with a control, while the form or report is in Design View:
This would display the Property Sheet of the control.
The Properties Window
In the Microsoft Visual Basic environment, the Properties window displays the characteristics of the control selected in Microsoft Access or in the Object combo box of the Code Editor in Microsoft Visual Basic.
Selecting a Control
To select the control whose properties you want to access:
Common Events of Windows Controls
Introduction
There are some events that many types of controls, including forms or reports, can fire. For example, many controls accept that you use the mouse with them. Some other controls allow the keyboard.
Click
When the user clicks an object, the control fires a simple event named Click (On Click in the Property Sheet, Click in the Procedure combo box, and OnClick in the Properties window). When this happens, no detailed information is provided.
The Double-Click Event
When the user double-clicks something, the control fires an event named DblClick (On Dbl Click in the Property Sheet, DblClick in the Procedure combo box, and OnClick in the Properties window).
Pressing a Key
When the user presses a key on a keyboard, the object on which the characters are typed sends one or more messages to the operating system. There are three main events that Microsoft Windows associates to the keyboard.
If the user presses a key on the keyboard, the operating system finds out if that key produces a character. If that's the case, the object fires an event named On Key Press. The formula of this event is:
Private Sub Something_KeyPress(KeyAscii As Integer) End Sub
The message of this event carries the ASCII code of the character corresponding to the key. This means that every character key (a, b, c, d, etc, or #, /, %, etc) has an equivalent ASCII code. You can find out what key the user had pressed.
To help you identify the key, check the value of the KeyAscii argument.
Keying Down
Besides the alphanumeric and symbol keys, the keyboard has other keys that do not display a character. Examples of these keys are Shift or Ctrl. To identify when the user presses one of these keys or in combination with a character key, you can use the On Key Down event. Its formula is:
Private Sub Something_KeyDown(KeyCode As Integer, Shift As Integer) End Sub
The first argument represents the character or symbol key that was pressed. The second argument represents the Shift, Ctrl, or Alt key if it was pressed also. You can find out what combination of keys the user pressed.
Releasing a Key
When the user releases a key that was pressed, the control fires an even named On Key Up. Its formula is:
Private Sub Something_KeyUp(KeyCode As Integer, Shift As Integer) End Sub
This event takes the same arguments as keying down. To identify the key or combination of keys that were pressed, you use the same approach as the On Key Down event.
Mouse Events
A mouse is equipped with buttons, usually two, that the user presses to request an action. Compared to the keyboard, the mouse claims many more events that are directly or indirectly related to pressing one of its buttons.
When the user presses one of the buttons on the mouse, an event called On Mouse Down fires. This event carries enough information through three parameters. It appears as follows:
Private Sub Something_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub
When the user releases a button that was pressed on the mouse, the On Mouse Up event fires. It provides the same types of information as the MouseDown event:
Private Sub Something_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub
The On Mouse Move event fires while the user is moving the mouse on an object. It provides the same pieces of information as the On Mouse Down and the On Mouse Up events:
Private Sub txtFirstName_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub
Primary Characteristics of Controls
The Name of a Control
After adding a control to a form, the control must have a name. Microsoft Access assigns a default name to every newly added control. To specify or change the name of a control:
If you are programmatically creating a control, declare a variable of type Control and give it a name. Use the Set operator to assign the return value of the Application.CreateObject() method. Call the method as a function using parentheses. Once you have created the control, you can use the name of the variable as the name of the control. Here is an example:
Private Sub cmdCreateControl_Click() Dim ctlLastName As Control Set ctlLastName = CreateControl("Customers1", _ AcControlType.acTextBox, _ acDetail, _ "Customers1") . . . Use the control here End Sub
After using the control in the code, you should free the memory it was using. To do this, assign Nothing to it. Here is an example:
Private Sub cmdCreateControl_Click()
Dim ctlLastName As Control
Set ctlLastName = CreateControl("Customers1", _
AcControlType.acTextBox, _
acDetail, _
"Customers1")
. . . Use the control here
Set ctlLastName = Nothing
End Sub
Practical Learning: Naming a Control
The Text of a Control
Depending on the type of control, for most of them, the primary characteristic is the text it displays. Also depending on the control, you can control the text visually or programmatically. In fact, the property of this text depends on the type of control.
For some controls, the text is represented by a property named Caption. Here is an example of using it:
Private Sub cmdProperties_Click()
Something.Caption = "Stella Productions"
End Sub
For some other controls, the text is represented by a property named Text. To specify the text of such controls, use only the name of the control and assign the desired text to it. Here is an example that specifies the text of a control:
Private Sub cmdProperties_Click()
ctlSomething = "Jonathan"
End Sub
In both cases, if the value is a number, assign it.
Practical Learning: Specifying the text of a Control
The Record Source of a Form or Report
The record source is a list that posseses the values that will be displayed by/in the controls of a form or report. The primiary type of record source of a form or report is a table. Therefore, after creating a table and saving it, you can use it as the source of data for a form or report. To do this visually, in the Properties window of the form or report, specify the Record Source property. To programmatically support this, the Form class is equipped with a property named RecordSource. To programmatically specify a record source, assign the name of a table, as a string, to this property. Here is an example:
Private Sub cmdSetRecordSource_Click()
Me.RecordSource = "Employees"
End Sub
If you had programmatically created the form and you plan to use it for data, access its RecordSource property and assign the name of the desired table to it. Here is an example:
Private Sub cmdCreateForm_Click()
Dim frmEmployees As Form
Rem Create the form
Set frmEmployees = CreateForm
frmEmployees.RecordSource = "Employees"
End Sub
Practical Learning: Specifying the Record Source of a Form
The Control Source
Many controls are made to request or display a value. In some other controls, the user must make a selection. A control source is a characteristics that specifies whether the values of a control are tied to a column of a table. Normally, after creating a table and setting it as the record source of a form (or report), you can use one of the fields of the table to provide the value(s) of the control. This characteristic is controlled by a property named Control Source.
To manually set the control source of a control, access its Property Sheet. In the All or the Data tab, click the arrow of the Control Source combo box and select the desired name of the column from the table.
If you are programmatically creating a new control using the Application.CreateObject() method, to specify the table column that holds the values of the control, pass a fifth argument as the name of the field. Here is an example:
Private Sub cmdCreateControl_Click()
CreateControl "Customers", _
AcControlType.acTextBox, _
acDetail, _
"Customers", _
"Last Name", _
840, 300
End Sub
To programmatically specify a control source, assign the name of the field to the control. Here is an example:
Private Sub cmdRecordSource_Click()
Me.RecordSource = "Cars"
Me.txtTagNumber.ControlSource = "TagNumber"
End Sub
Practical Learning: Specifying a Control Source
The Names of Columns and Controls
If you generate a form or report that is bound to a table or query, each control added to the container has a name that is the same as the column to which it is bound. If you add an unbound text box or control to a form or report, it receives a default name. Whether a control on a form or report is bound or not to a column of a table, the name of that control does not have anything to do with that of a column. This means that you can easily set or change the name of a control without changing its Control Source.
To specify the name of a control on a form or report:
To do this programmatically, assign the name of the column to the name of the control. Here are two examples:
Private Sub cmdRecordSource_Click()
Me.RecordSource = "CleaningOrders"
Me.txtPhoneNumber.ControlSource = "CustomerPhone"
Me.txtCustomerName.ControlSource = "CustomerName"
End Sub
Practical Learning: Changing the name of a Control
The Field List
Microsoft Access provides various tools to add controls to a form or report. One technique is to use the Field List.
Practical Learning: Using the Field List
Tab Ordering the Controls
When navigating among the controls on a form, the user can press Tab to move from one control to another. To let you control the sequence of controls navigation, Microsoft Access provides the Tab Order dialog box. To access it, first display the form in the Design View:
This would open the Tab Order dialog box:
To arrange the order of items, you can click the Auto Order button. To manually arrange the items, click a row header. Then click it again and hold the mouse down. Drag up or down as you need.
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2000-2022, FunctionX, Inc. | Next |
|