ACC2000: Event Procedure Disappears When a Control Is Renamed (209087)



The information in this article applies to:

  • Microsoft Access 2000

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

  1. Start Microsoft Access and open any database.
  2. Create a new form not based on any table or query.
  3. Add a command button to the form. Note that Access automatically names the button Command0.
  4. In the property sheet, click the Build button for the OnClick property.
  5. 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
    					
  6. 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.
  7. 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.
  8. On the View menu, click Design View. In the property sheet, change the Name property and the Caption property from Command0 to MyButton.
  9. On the View menu, click Form View, and then click the MyButton button. Notice that the message box does not appear.
  10. 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().
To restore the original event procedure, follow these steps:
  1. Delete the entire Sub procedure MyButton_Click().
  2. In the Object box on the toolbar, click General, and then in the Procedure box, click Command0_Click.
  3. Change the first line of the Sub procedure from
    Private Sub Command0_Click ()
    					
    to:
    Private Sub MyButton_Click ()
    					
  4. 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.

REFERENCES

For more information about creating event procedures, click Microsoft Access Help on the Help menu, type event properties in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

Modification Type:MajorLast Reviewed:6/30/2004
Keywords:kbprb kbusage KB209087