Microsoft Access - Associate An Orphaned Event Procedure With A Control (Access 97/2000/2002/2003)
When you're designing a form or report, it's easy to accidentally orphan an event procedure as you tweak, delete, add, and rename controls. You can end up with a control that appears to have a broken event procedure. Even though the control and procedure seem to go together, the event doesn't appear to fire when it's supposed to. Fortunately, the situation is easy to address.
We'll start by illustrating the problem:
- First, create a blank form in Design view and add a button to it
- Name the button cmdDate
- Click the Code button to launch the Visual Basic Editor (VBE)
- At the insertion point, add the following code:
- Private Sub cmdDate_Click() MsgBox "Today is " & Date End Sub
- Close the VBE and switch to Form view
- Click the button and Access displays the message box
- At this point, switch back to Design view and delete the button
- Then, add another one and name it cmdDate
- Switch to Form view and try the button -- nothing happens
- Open the VBE and you'll see that the event procedure is still there. Although the procedure and control names match, they aren-t connected. Compiling the project won't fix the problem, nor will saving and reopening the form.
The fix is easy. All you have to do:
- Switch to Design view and set the command button's On Click property to [Event Procedure]
After you make the change, you'll find that Access once again runs the procedure when you click on the control.