Microsoft KB Archive/209087

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

Article ID: 209087

Article Last Modified on 6/30/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q209087



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

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



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's intended design.



RESOLUTION
To restore the original event procedure, do either of the following:
 * Copy the code from the original event procedure, and then 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 Command0 might be Command0_Click. The first line of the event procedure would be: Private Sub Command0_Click If you rename the button, the OnClick property still has an associated event procedure, but the original event procedure is not renamed accordingly. Instead, a new, empty event procedure is called by the button's OnClick property.

The Command0_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 Command0_Click in the Procedure box.

Steps to Reproduce Behavior
 Start Microsoft Access and open any database. Create a new form not based on any table or query. Add a command button to the form. Note that Access automatically names the button Command0. 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 Access automatically creates the following code for the event: Private Sub Command0_Click

End Sub </li>  Modify the event code to match the following: Private Sub Command0_Click MsgBox "This is a test" End Sub On the File menu, click Close and Return to Microsoft Access. </li> On the View menu, click Form View, and then click the Command0 button to make the message box appear. Click OK on the message box.</li> On the View menu, click Design View. In the property sheet, change the Name property and the Caption property from Command0 to MyButton.</li> On the View menu, click Form View, and then click the MyButton button. Notice that the message box does not appear.</li> On the View menu, click Design View. Click the Build button for the OnClick property. Note that the Sub procedure that you created in step 6 no longer shows in the module. Instead, a new, empty Sub procedure has been created called MyButton_Click.</li></ol>

To restore the original event procedure, follow these steps: <ol> Delete the entire Sub procedure MyButton_Click.</li> In the Object box on the toolbar, click General, and then in the Procedure box, click Command0_Click.</li>  Change the first line of the Sub procedure from Private Sub Command0_Click to: Private Sub MyButton_Click </li> Click MyButton in the Object box to ensure that the procedure has been related to the MyButton object. Close the code window and return to Access. Test the button to see that the message appears again.</li></ol>

<div class="references_section">