To refer to a form of a Microsoft Visual Basic application,
you can use the Me keyword.
After creating a database, or while working on one, all of
the forms that belong to it are stored in a collection called AllForms.
As seen for the AllTables collection, AllForms is equipped with
the Count property that holds the number of the forms that belong to the
current database. Each form can be located by its name or its index, using the Item()
property.
In Microsoft Access, a form is an object of type Form.
A form that is opened in a database is a member of the Forms collection. To
refer to an open form in your code, you can enter the Forms collection, followed by an
exclamation point !, followed by the name of the form. Here is an example that
refers to a form named Students:
Forms!Students
You can also include the name of the form between an opening
square bracket and a closing square bracket but, if the name is in one word,
then the square brackets are optional:
Forms![Students]
If the name is made of more than one word, then square
brackets become required. Here is an example that refers to a form named
Potential Applicants:
Forms![Potential Applicants]
If you omit the square brackets in this case, the expression
or the code may not work.
A form can also be referred to by an index in the Forms
collection. The first form of the collection has an index of 0 and can be
accessed with Forms(0). The second form in the collection has an index of 1 and
can be referred to by Forms(1), and so on. The name of the form can also be used
as index. For example, a form named Students can be referred to as
Forms("Students").
We mentioned that a form is mostly used as a container
because it hosts some of the controls of its application. The controls that a
form hosts are members of the Controls collection.
If a form has been
designed or exists already but you want its Windows controls to display the
values from a table, you can change its Record Source property to that of
the table. After doing this, all columns from the table become available to
controls on the form.
To programmatically specify the table that holds the data
that would be made available to the controls on a form or a report, assign the
name of the table to its RecordSource property. Here is an example:
Private Sub cmdRecordSource_Click()
Me.RecordSource = "CleaningOrders"
End Sub
|
Like a regular window container, the form displays a title
bar in its top section. On the left side of the title bar, it displays an icon. The system icon is
fixed and you should not spend time trying to change it. The system icon holds a menu that allows the user to perform the
regular operations of a Windows container, including minimizing,
maximizing, restoring or closing the window. To display this menu, the
user can click the form’s system icon:
The presence of the form’s system icon is partly
controlled by the Control Box property of a form. In most cases, you
should make this system icon and its menu available to the user. If for
some reason you don't want to provide this functionality, set the
Control Box property to No. The form would appear as
follows:
If you decide to do this, make sure you provide the
user with the ability to close the form and this type of title bar makes
it impossible. Of course, a user may know that the form can be closed by
clicking the Close menu item under the File group of the main menu or by
pressing Ctrl + F4.
|
The middle section of the title bar is actually
referred to as the title bar. It can be used to change the view of the
form after right-clicking it. The title bar uses a color set in the
Advanced Appearance of Control Panel as Active Title Bar:
Since you cannot control and cannot predict how your
users will modify their system colors (because most users are free to set
their system colors as they wish, even when they work corporate), you
should refrain from changing this color when designing your forms,
especially if you plan to distribute your database. Otherwise, the result
you see on your form may be different from your users computers.
|
|
When designing your forms, try to keep colors to their
default and to their minimum. In this lesson, we are showing most options
simply to let you know that they exist, not because you should or must
change them. |
The right side of the form’s title bar displays
three system buttons
,
or ,
. The
group of these buttons is called the Control Box. If you do not
want these system buttons at all, access the Properties window of the form
and set the Control Box Boolean property to No from its Yes
default value. In this case, the form would appear with neither the system
icon nor the system buttons as seen above. Once again, if you do this,
make sure the user has a way to close the form.
Instead of completely hiding all system buttons of a form, you can specify
which ones to display and thus control the ability to close, minimize, or
maximize the form. The presence and/or the role of the system buttons is
(partly) controlled by the Control Box and the Min Max Buttons
properties. When the Control Box property is set to No,
regardless of the value of the Min Max Buttons property, there
would be no system buttons on the title bar (this is also something you
learn if you do Windows GUI programming, like Win32).
Therefore, in order to control the appearance and behavior of the system
buttons, the Control Box property must be set to Yes. The
values of the Min Max Buttons produce the following results:
Min Max Buttons |
Result |
System Buttons |
Consequence |
Minimize |
Maximize |
Close |
None |
|
Hidden |
Hidden |
Available |
The form can only be closed |
Min Enabled |
|
Enabled |
Disabled |
Available |
The form can be minimized or closed but not
maximized |
Max Enabled |
|
Disabled |
Enabled |
Available |
The form cannot be minimized but can be maximized |
Both Enabled |
|
Enabled |
Enabled |
Available |
All operations (minimize, maximize, restore, and
close) are allowed |
|
Depending on the role and probably the number of
Windows controls on a form, you will decide what button to allow or not.
To minimize a form, the user can click its Minimize
button. To programmatically minimize a form, you can call the Minimize()
method of the DoCmd object. Here is an example:
Private Sub cmdManipulate_Click()
DoCmd.Minimize
End Sub
To maximize a form, the user can click its Maximize
button. To programmatically maximize a form, you can call the Maximize()
method of the DoCmd object. If a form is maximized, to restore it,
the user can click the Restore button. To programmatically restore a form,
call the Restore() button of the DoCmd object.
To close a form, the user can click its system Close
button. As seen earlier, to close a form, you can call the DoCmd.Close
method.
|
Practical Learning: Controlling the System Buttons of a Form
|
|
- To select the Central form, on the main menu, click Windows -> Central
- As the form is still in Design View, click the button at the intersection
of its rulers
- In the Properties window, set its Min Max Buttons property to Min
Enabled
- Save the form
Whether you allow the system icon and system buttons
or not, the user needs to be able to know where a form starts and where it
ends. This is seen by the borders of the form. In most cases, you will not
be concerned with this aspect. Otherwise, you can control the borders of a
form.
The borders of a form are controlled using the Border
Style property. If you set it to None, the form would appear
without borders:
|
A form displays on the screen using its dimensions.
These are the width and height. The form itself controls only the width.
The height is controlled by its sections. To change the width of a form,
in the Properties window, type the desired decimal value in the Width
field of the Format property page. You can also change the form's width by
dragging the right border of one of its sections.
|
The most visible part of a form is an area called
Detail. This section starts on a bar labeled Detail and ends at the bottom
unless a new section starts. To programmatically access the Detail section
of a form, if you working in an event of the form, you can use either Detail
or Me.Detail
The Detail section serves as the main host of
other controls. It can also serve to display messages of various kinds.
The Detail section can be enhanced by manipulating its properties. These
properties are not necessarily related to the parent form but can be used
conjointly with it.
We mentioned earlier that the form
itself controle its width. The vertical dimension of a form is controlled
by its sections. This means that a section, such as the Detail section,
can "decide" how much height it needs to accommodate its
content. The vertical measurement of the Detail section is controlled by
the Height property. There are two main ways you can change the
Detail section's height at design time:
- In the Format tab of the Properties window, you can change the value
of the Height field. The value must be a natural or a decimal
number. In reality it is a decimal number. If you type a natural
number, also called an integer, it would be converted to decimal. If
you type an invalid number, you would receive an error
- You can position your mouse in the lower border of the Detail
section until the mouse cursor changes into a vertical double arrow.
Then click and drag. You can drag up to shrink the section or drag
down to heighten it
To programmatically change the height of the Detail
section, assign a constant natural or decimal value to its Height
property. Here is an example:
Private Sub cmdChangeHeight_Click()
Me.Detail.Height = 3.18
End Sub
|
The Header and Footer Sections
|
|
Besides the Detail section, a form can be enhanced
with one or two more sections: Header and Footer. |
|
By default, the Header
and Footer sections are created on a form if you use the Form Wizard. |
To add the Header and Footer sections on a form that doesn't have them,
you can use the menu bar where you would click View -> Form
Header/Footer. Probably the fastest way to add these sections is by
right-clicking anywhere on the form and clicking Form Header/Footer. If
the form already has these sections but they don't contain anything, they
would be removed. If the form already has these sections and they contain
anything, you would receive a warning:
If you still want to delete the Header and Footer
sections, you can click Yes; this would un-recoverably delete their
content.Like the Detail section, the Header and Footer sections control
their own height, which you can change the same way we described above for
the Detail section.
To access the Header section in your code, use the
form's FormHeader property. Here is an example:
Private Sub cmdManipulate_Click()
Me.FormHeader.Height = 2.24
End Sub
To programmatically access the Footer section, use the
form's FormFooter property.
This also applies for the Back Color property.
The Special Effect property allows you to raise or sink a section.
|
Practical Learning: Closing a Form
|
|
- To select a different form, on the main menu, click Windows -> Cleaning
Orders
- To add the Header and Footer sections to the form, right-click the body of
the form and click Form Header/Footer
- On the Toolbox, make sure the Control
Wizards button is pushed ,
click Command Button
and click the right section under the Footer bar of the form
- In the Categories list of the first page of the Command Button Wizard,
click Form Operations
- In the Actions list, click Close Form, and click Next
- In the second page of the wizard, click the text box and change its string
to Close
- Click Next
- Change the name of the button to cmdClose and click Finish
|
The Record Selector is a vertical bar on the left side of a
form. It allows the user to select all of the fields’ contents of one or
more records and copy them to the clipboard. Such a selection can then be
pasted to another record, other records, or to a text or another type of
file (for example you can paste the record in Notepad, Microsoft Word,
Microsoft Excel, or any text file).
There are two reasons you would use record selectors on a form. If you
want the user to be able to access all fields of a record as a whole, make
sure that the form is equipped with record selectors. In the same way, you
can hide the scroll bar if the user would not need them. Another reason
you would use a record selector is for aesthetic reasons. Most of the
time, if designing a form for data entry, you would mostly allow the
presence of record selectors. If you are designing a dialog box that would
display the same content all the time, you should not allow the record
selectors.
The presence or absence of record selectors is controlled by the Boolean Record
Selectors property field. If you set the field value to Yes, the form
would display the record selector. Setting the field value to No would
hide the record selectors. To programmatically control the presence or
absence of the record selector on a form, assign a value of True (to
display it) or False (to hide it) to the RecordSelector property of the
form. Here is an example:
Private Sub cmdManipulate_Click()
Me.RecordSelectors = False
End Sub
|
As much as you can, you should design your
(non-Datasheet) form to display all of the fields of a record. Sometimes this will not be possible. If a form
possesses too many fields, Microsoft Access would equip
the form with one or two scroll bars. A scroll bar allows the user to
scroll from one side of the form to another. The vertical scroll bar is
used to scroll up and down while the horizontal scroll allows scrolling
left and right.
By default, scroll bars come up if the form "judges" that some
fields are hidden. The presence of scroll bars allows the user to be aware
of hidden objects on the form. Depending on your design, you can control the appearance
or disappearance of the scroll bars. To do this, change the value of the Scroll
Bars field in the Format property page of the Properties window. There
are four possible values. To display only the vertical scroll bar, set the
value to Vertical Only. In the same way, you can display only the
vertical scroll bar by selecting Horizontal Only. On the other
hand, you can display both scroll bars by setting the value to Both.
If for some reason you don't want any scroll bar, set the Scroll Bars
property value to Neither.
To programmatically control the presence or absence of
scroll bars, access the ScrollBars property of the form and assign one of
the following four values:
Value |
Description |
0 |
No scroll bar will display |
1 |
Only the horizontal scroll bar will
display |
2 |
Only the vertical scroll bar will
display |
3 |
Both the horizontal and the vertical
scroll bars will display |
|
When we study records, we will see that a form (also a table, a query, or a report) is equipped
with some buttons in its lower section. These buttons allow the user to
navigate back and forth between records. These buttons are very useful
during data entry and data analysis. If you are creating a form that would
display the same content all the time, such as a form that does not
actually display records, you can hide the form navigation buttons.
The presence or absence of navigation buttons is controlled by the Boolean
Navigation Buttons property. When its value is set to Yes,
the form would display the Navigation Buttons. To hide them, set
the Navigation Buttons property value to No.
To programmatically control the presence or absence of
the navigation buttons, access the form's NavigationButtons property and
assign the desired Boolean value. Here is an example:
Private Sub cmdManipulate_Click()
Me.NavigationButtons = False
End Sub
|
A form is equipped with special horizontal lines used
to visually separate sections of a form. They do not perform any other
special function. They can be useful on a continuous form.
To equip a form with dividing lines, add a header and a footer sections.
On the other hand, if you create a form that is directly equipped with a
header and a footer sections, it would display the dividing lines.
The presence or absence of the horizontal lines is controlled by the
Boolean Dividing Lines property. Its default value is Yes,
which means that the dividing horizontal lines would display on the form.
To hide both lines, you can set its value to No. To hide an
individual line, you can completely reduce its section.
To control the presence or absence of the dividing
lines of a form, access its DividingLines property and assign the desired
Boolean value. Here is an example:
Private Sub cmdManipulate_Click()
DividingLines = False
End Sub
|
By default, when a previously created and saved form
appears, Microsoft Access remembers the previous position the form had and
restores it. If you can make sure that the form is always centered when it
comes up. To make this possible, the form is equipped with the Auto Center Boolean property.
When set to Yes, this property causes the form to be centered when it
displays to the user.
|
|
|