Microsoft Access 2010 Bible

(Rick Simeone) #1

Part III: More-Advanced Access Techniques


682


Manipulating controls at runtime
The form design capabilities of Access are so engaging that it’s easy to lose sight of the fact that all
Access controls are programmatically accessible at runtime. Changing the design of a form on the
fly, based on input from the user, can sometimes be very convenient. For example, a list box that
contains information that is relevant only some of the time doesn’t have to be displayed all the
time. Well-designed forms often hide irrelevant controls whenever possible. You can achieve this
and similar functionality by assigning values to control and form properties at runtime.

Here are a few properties that are often good candidates for dynamic assignment at runtime:

l (^) The Enabled property: If you want a control to be visible but grayed out, set the
Enabled property to False. Clicking the grayed-out control has no effect. This tech-
nique enables you to maintain a consistent form design, and helps the user become famil-
iar with the location of the controls on the form, but it prevents the user from selecting a
control that’s irrelevant to the current operation.
l The Visible property: You can easily toggle the display of any control by assigning
Yes or No to the control’s Visible property. While the control is invisible, it’s also inac-
tive and can’t receive the focus. An invisible control can’t respond to events. Making a
control invisible is sometimes more appropriate than simply disabling the control, espe-
cially in the case of text, combo, or list boxes.
l (^) The Caption property: By dynamically assigning values to a control’s Caption prop-
erty, the control can serve double- or triple-duty. This technique is especially useful when
two command buttons serve mutually exclusive functions and don’t need to appear on the
form at the same time. For example, you might place a command button with the caption
&New Patient Data on your application’s main switchboard form. This button opens a
form for adding a new record to your patient database. After the new patient data has
been entered, but before the full transaction has been committed to the database, you
could change the command button’s caption to &Edit Patient Data using the button’s
Caption property. The code in the button’s Click event procedure determines which
task to perform based on the caption at the time the user clicks on the button.
You can also use the active form’s Caption property creatively to display information
about the current record. Each time the current event occurs, assign a new value to the
Caption property that contains pertinent information from the current record.
l The RowSource property: Use the RowSource property of a combo- or list-box control
to synchronize the list contents with values in other controls in the active form. For exam-
ple, if txtDepartment contains the value Sales, you can set the cboPhoneList
combo box’s RowSource property to show only members of the sales department in the
combo box.
l (^) The BackColor and ForeColor properties: Judicious use of color can be very effec-
tive in conveying important information. Access enables you to dynamically change the
color of controls as they receive or lose focus, or to reflect a state of the data by changing
the ForeColor and BackColor properties.

Free download pdf