The main form exists to populate the subform. (Which is in datasheet view) On the main form is a combo box and the command button in question. The combo box is populated using the "parents'" names. The combo box, on change, instructs the subform to requery. Requerying the subform populates it with the "child" records of the selection made in the combo box in main.
The intended function for this form is: After selecting the parent, the user will then make the child selection(s) on the datasheet and click the delete button on the main form.
If I put the delete button on the toolbar, it can be used to produce the desired effect. However, the toolbars are customized and usage of a toolbar button would break the uniformity of my user interface.
The key to this is making sure that you are in fact deleting a record within the subform and not from the parent form, therefore we need to check that you had in fact clicked onto the record to delete within the subform before carrying out the actual delete process.
You can do this by using the PreviousControl statement and here's how:
In the On Click event of your parent form Command button, place the following code:
Dim Ctrl As Control 'Must be the 1st line in the event!
Set Ctrl = Screen.PreviousControl 'Must be the 2nd line in the event!
If Ctrl.Name = "mySubFormName" Then
If the subform was not the last control which had focus then the Delete process will not be carried out. You can use this method to delete records from any number of SubForms located within your parent form.