Microsoft KB Archive/113876

= ACC: Event Procedure Disappears When a Control Is Renamed =

Article ID: 113876

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q113876



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



SYMPTOMS
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
 Create a new form not based on any table or query. Add a command button to the form. Note that Microsoft Access automatically names the button Button0. In the property sheet, click the Build button for the OnClick property.  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   Modify the event code to match the following, and then close the form module:

Sub Button0_Click Msgbox "This is a test" End Sub </li> In the property sheet, change the Name property from Button0 to MyButton.</li> 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.</li></ol>

To restore the original event procedure, follow these steps:

<ol> Delete the entire Sub procedure.</li> In the Procedure box on the toolbar, select Button0_Click.</li>  Change the first line of the Sub procedure from

Sub Button0_Click

to:

Sub MyButton_Click </li></ol>

<div class="references_section">