Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Programmatically Change a Control Type


View products that this article applies to.

This article was previously published under Q210247
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

↑ Back to the top


Summary

This article shows you how to programmatically change a control on a form from one type to another. By using the ControlType property in a Visual Basic for Applications procedure, you can change a text box into a combo box, a toggle button into an option button, and so on.

↑ Back to the top


More information

The method described in this article uses a custom Visual Basic for Applications function that is called from a command button on Form1 to change the ControlType property of a text box on Form2 to a combo box. You have to use two forms because the ControlType property is available only in a form's Design view. As a result, you cannot use Visual Basic for Applications to change a control's type while the form that contains the control is open in Form view.

To programmatically change a form's control type, follow these steps:

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. Create a new form not based on any table or query, and then name it Form1.
  3. Add a command button to Form1, and then set its properties as follows:
    Name: Command0
    Caption: Change Control Type
  4. Set the command button's OnClick property to the following event procedure:
    Private Sub Command0_Click()
    
       If Forms!Form2.CurrentView <> 0 Then DoCmd.OpenForm "Form2", _
       acDesign
    
       If Forms!Form2![CategoryName].ControlType = acComboBox Then
         Forms!Form2![CategoryName].ControlType = acTextBox
       Else
         Forms!Form2![CategoryName].ControlType = acComboBox
       End If
    
    End Sub
    					
  5. Close and save Form1.
  6. Make a copy of the Categories form and call it Form2.
  7. Open Form2 in Form view.
  8. Open Form1 in Form view and click the command button. Note that Form2 opens in Design view and that the CategoryName control changes from a text box to a combo box. When you click the Change Control Type button again on Form1, the CategoryName control in Form2 changes back to a text box.
NOTE: When you change a control to another type of control, Microsoft Access copies the appropriate property settings from the original control to the new control. If a property exists for the original control but not for the new control, Microsoft Access does not copy it. If a property exists for the new control but not for the original control, Microsoft Access sets the property to the default control for that type of control.

↑ Back to the top


References

For more information about the ControlType property, click Microsoft Access Help on the Help menu, type controltype property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB210247, kbprogramming, kbinfo, kbhowto

↑ Back to the top

Article Info
Article ID : 210247
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 351