formscriteriacaptionsubform

Referring to a Subform from a Query


In MS Access 2010, I have a Query which quotes the following in the Criteria;

[Forms]![frm_Add_Item_Subform].[ActiveControl].[Caption]

This lets me use the "Caption" text of a Button within the query. The following code is on the Button to capture the click.

Private Sub cmdClickMe_Click()
Debug.Print Me.cmdClickMe.Caption
Debug.Print Screen.ActiveControl.Caption
End Sub

I obtained information on how to do this at the following StackOverflow URL. use caption of pressed button from main form in query of other form

The functionality works ok when used in a Form. But doesn't when its used within a Subform (which is where I need to use it). I believe I need help tweaking the Query Criteria to refer to the Subform correctly.

Hoping someone will be able to give me some guidance on this. Thanks in advance!

P.S. The main Form name is "frm_Add_Item_Main"


Solution

  • This is how to refer to subforms:

    Forms!mainFrm!subFormControlName.Form
    

    so you need:

    Forms!mainFrm!subFormControlName.Form.ActiveControl.Caption
    

    where subFormControlName is the name of the subform control (not the name of the subform) in the main form (it shows when the subform control is selected in the main form)