Showing and hiding controls

Just like every control on a form has an Enabled property, every control also has a Visible property. When the Visible property is True (Yes), the control is visible on the form. When the Visible property is False (No), the control is invisible in Form view. You can use this property to make controls on the form appear or disappear, depending on values in other controls.

For example, the earlier PaymentMethod_AfterUpdate() procedure uses the .Enabled property to make controls either enabled or disabled. You can simply change Enabled to Visible in that procedure, as shown here:

If [PaymentMethod] = "Credit Card" Then

'Show controls for entering credit



CCType.Visible = True

CCNumber.Visible = True

CCExpireMonth.Visible = True

CCExpireYear.Visible = True

CCAuthorization. Visible = True


'Hide controls for entering credit



CCType.Visible = False

CCNumber.Visible = False

CCExpireMonth.Visible = False

CCExpireYear.Visible = False

CCAuthorization. Visible = False

End If

This procedure causes the credit card controls to disappear from the form when Credit Card isn't selected in the PaymentMethod field. The controls are visible only when Credit Card is selected as the PaymentMethod.

Making controls appear and disappear instead of enabling and disabling them is a matter of preference. Generally, it's bad practice to have things appear and disappear as you change data on a screen. A disabled control lets a user know that they can probably do something (check a check box or change a value, for example) to make that control available. If the control is invisible, the user doesn't know that it's there, which may be desirable in other situations, such as configuring a form when you first open it so that it doesn't change while the form is being used.

0 0

Post a comment