MORPHING ACCESS CONTROLS

As sometimes happens when you develop an application, you (or the user) might change your mind about what kind of control you want to use to represent a particular field. Access has a way of dealing with this situation. You can morph controls either through the user interface (UI) at form design time with Access menus, or you can morph them in code using VBA at runtime.

Morphing Controls at Design Time

Changing controls from one type to another at design time, also called morphing, is simple. For example, open the ComboBoxExample1 form in Design view. Now highlight the cboEmployeeToQuery combo box and then choose Change To from the Format menu.

Notice that because it's a combo box that's being changed, the only choices are Text Box and List Box. For the most part, it makes no sense to try to change a combo box into a label or toggle button. Various items are enabled and disabled, depending on the type of control that's being changed.

Morphing Controls with VBA at Runtime

Changing control types for a field at runtime takes a bit more work. By controlling morphing programmatically, you can display one type of control for one purpose and a different type of control for a different purpose, all by using the same form and field. To do so, you need to change the control's ControlType property. But first, you need to open the form in Design view. If the form isn't open, you can open it by using the following command:

DoCmd.OpenForm "NameOfForm", acDesign, , , , acHidden

This code opens the form in Design and Hidden views. You can then change the type of the control you want by assigning the new type to the ControlType property, as in the following syntax:

Forms!NameOfForm!NameOfControl.ControlType = ControlTypeConstant
						

Replace ControlTypeConstant with one of the constants listed in Table 10.2.

Table 10.2. Control Type Constants
Constant Control
acLabel Label
acImage Image
acCommandButton Command button
acOptionButton Option button
acCheckBox Check box
acTextBox Text box
acListBox List box
acComboBox Combo box
acCustomControl Custom control
acToggleButton Toggle button

Note

Just as when you use the menus to change control types at design time, at runtime not all types can be changed to other types, and the types that can change at all are limited in what type they can be changed to. To find out whether one type can be changed to another, test it out by using the menus at design time.


Chap10.mdb, found on the accompanying CD-ROM in the ExamplesChap10 folder, uses two forms to show how to morph controls on another form while the form is open:

  • ControlMorphExampleForm1 has two command buttons: one with code behind it for changing the types of the two controls on the second form, and one that simply closes the form.

  • ControlMorphExampleForm2 can hold different types of controls, depending on whether the routine from the first form is run on it. It has either a combo box and an option button on it, or a list box and a check box. Figures 10.24 and 10.25 show these two possibilities.

    Figure 10.24. There's nothing up your sleeve as the form starts out with a combo box and an option button.

    Figure 10.25. Click the magic button and voilà—the form now has a list box and a check box.

The code to do this task is on the cmdPerformMorph command button on the ControlMorphExampleForm1 form. Listing 10.4 shows the code attached to the OnClick event.

Listing 10.4. Chap10.mdb: Morphing a Combo Box/Option Button into a List Box/Check Box
Private Sub cmdPerformMorph_Click()

   On Error Resume Next

   DoCmd.Echo False, "Morphing controls, please wait..."

   DoCmd.SelectObject acForm, "ControlMorphExampleForm2"

   '-- Switch to Design Mode
   DoCmd.DoMenuItem acFormBar, 2, 0

   If Forms!ControlMorphExampleForm2!cboEmployeeToQuery.ControlType _
      = acListBox Then
      Forms!ControlMorphExampleForm2!cboEmployeeToQuery.ControlType _
         = acComboBox
   Else
      Forms!ControlMorphExampleForm2!cboEmployeeToQuery.ControlType _
         = acListBox
   End If
   If Forms!ControlMorphExampleForm2!optMorphing.ControlType _
      = acOptionButton Then
      Forms!ControlMorphExampleForm2!optMorphing.ControlType = acCheckBox
   Else
      Forms!ControlMorphExampleForm2!optMorphing.ControlType _
         = acOptionButton
   End If

   '-- Switch back to form mode
   DoCmd.DoMenuItem acFormBar, 2, 1

   DoCmd.SelectObject acForm, "ControlMorphExampleForm1"
   DoCmd.Echo True

End Sub

Notice that the constants are used for testing and assigning the control types to the ControlType property.

After changing the control type through the UI and through code, you might have to set other properties, depending on which type the control was changed to. Otherwise, Access sets the controls to the default settings for the new control type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.195.16