Event procedure code does not run when you expect it to.
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.
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.
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
- 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.
- 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.
- On the View menu, click Design View. In the property sheet, change the Name property and the Caption property from Command0 to MyButton.
- On the View menu, click Form View, and then click the MyButton button. Notice that the message box does not appear.
- 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:
- Delete the entire Sub procedure MyButton_Click().
- In the Object box on the toolbar, click General, and then in the Procedure box, click Command0_Click.
- Change the first line of the Sub procedure from
Private Sub Command0_Click ()
to:
Private Sub MyButton_Click ()
- 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.
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.