ACC: Event Procedure Disappears When a Control Is Renamed

ID: Q113876


The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0, 97


SYMPTOMS

Moderate: Requires basic macro, coding, and interoperability skills.

Event procedure code does not run when you expect it to.


CAUSE

You changed the Name property of the control that has the event procedure code. When you change a control's name by changing its Name property, the event procedure code name, which is made up of the control name and the event name, does not change to reflect the new control name.

This behavior is consistent with Microsoft Visual Basic behavior.


RESOLUTION

To restore the original event procedure, do either of the following:

  • Copy the code from the original event procedure and paste it into the new event procedure.

    -or-


  • Delete the new event procedure code, including the beginning Sub and ending End Sub lines, and then rename the original event procedure to the correct name for the control.



MORE INFORMATION

Microsoft Access creates the name of the procedure in an event procedure using a combination of the control name and the event name. For example, the procedure name for an event procedure in the OnClick property of a command button control named MyButton might be MyButton_Click. The first line of the event procedure would be:


   Sub MyButton_Click () 

If you rename the button, the OnClick property will still have an associated event procedure, but the original event procedure will not be renamed accordingly. Instead, a new, empty event procedure will be called by the button's OnClick property.

The MyButton_Click subprocedure is still a part of the form, however. To access it in the form's module, select General in the Object box on the module toolbar, and then select MyButton_Click in the Procedure box.

Steps to Reproduce Behavior


  1. Create a new form not based on any table or query.


  2. Add a command button to the form. Note that Microsoft Access automatically names the button Button0.


  3. In the property sheet, click the Build button for the OnClick property.


  4. In the Choose Builder dialog box, click Code Builder, and then click OK. Note that Microsoft Access automatically creates the following code for the event:
    
          Sub Button0_Click ()
    
          End Sub 


  5. Modify the event code to match the following, and then close the form module:
    
          Sub Button0_Click ()
             Msgbox "This is a test"
          End Sub 


  6. In the property sheet, change the Name property from Button0 to MyButton.


  7. Click the Build button for the OnClick property. Note that the Sub procedure you created in step 5 no longer shows in the module. Instead, a new, empty Sub procedure has been created, using the control's new name.


To restore the original event procedure, follow these steps:
  1. Delete the entire Sub procedure.


  2. In the Procedure box on the toolbar, select Button0_Click.


  3. Change the first line of the Sub procedure from
    
          Sub Button0_Click ()
    
       to:
    
          Sub MyButton_Click () 



REFERENCES

For more information about creating event procedures, search the Help Index for "event procedures," or ask the Microsoft Access 97 Office Assistant.

Keywords : kbusage FmsEvnt
Version : 2.0 7.0 97
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: April 3, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.