Question : How do I update a subform combobox from another form that the subform called?

Hello,
I am trying to make a generic search form that allows me to populate the list box and return the user's choice to the control where the user called the search form from.

Everything works fine except the part that updates the combo box.  I need a way to pass the name of the subform and control to be updated, then have the search form use that information to pass the selection to the control.

I think it's just a syntax issue, but I've been banging my head on this for a few hours now.  Any help is appreciated!

(Please ignore that the code may return multiple selections.  I was using the same code to generate a filter for another form, so I'll change that)
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
***Initial Call to function that opens the search form***
Private Sub cmd_search_approved_Click()
    Call LookupDetailSubform("Forms!frm_SIR_maintenance![frm_Approval_Log_subform]", "cmbo_Approved_By_ID", "qry_employee_lookup")
End Sub
 
***Function that opens Search Form***
Function LookupDetailSubform(frm As String, TxtBoxNm As String, QryName As String)
'Open the course search form and pass the name of the form that called it
    DoCmd.OpenForm "frm_search_detail", acNormal, , , , , frm
    Forms!frm_search_detail.txt_filter = ""
    Forms!frm_search_detail.txt_Txt_Box_Name = TxtBoxNm
    Forms!frm_search_detail.ListDetail.RowSource = QryName
    Forms!frm_search_detail.txt_qry_name = QryName
    Forms!frm_search_detail.txt_filter.SetFocus
End Function
 
***Search Form Code***
Private Sub cmd_OK_Click()
    Dim i As Integer
    Dim ListString As String
    Dim frmName As String
    frmName = Me.OpenArgs
    ' Build a list of the selections for control 1.
    With Me.ListDetail
         For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ListString = ListString & .ItemData(i)
            End If
         Next i
    End With
    'Update Field
    '*** this is the one I want to work
    Forms(frmName)(cmbo_Approved_By_ID) = ListString
    '*** this one works
    'Forms!frm_SIR_maintenance![frm_Approval_Log_subform].Form!cmbo_Approved_By_ID = ListString
    Me.Visible = False
End Sub

Answer : How do I update a subform combobox from another form that the subform called?

Hi bnielsen56

The problem is that "Forms!frm_SIR_maintenance![frm_Approval_Log_subform]" is not a valid member of the Forms collection.

If it were not a subform, then you could get away with passing the form name, because Forms("frmMainForm") is a valid reference to a Form object, but Forms("frmMainForm!frmSubform") is not.

To make your search form truly generic, you should pass a Form object (this could be a main form or a subform).

First, in the declarations section of frm_search_detail, declare a public variable:

   Public CallingForm as Form

Now, declare a Form object argument, instead of a form name, for the function that opens the search form:

   Function LookupDetailSubform(frm As Form, TxtBoxNm As String, QryName As String)

and have the calling form pass itself:

   Call LookupDetailSubform(Me, "cmbo_Approved_By_ID", "qry_employee_lookup")

If the command button is on the main form, then call it like this:

   Call LookupDetailSubform(Me!frm_Approval_Log_subform.Form, "cmbo_Approved_By_ID", "qry_employee_lookup")

Now, instead of passing the form name in OpenArgs, set the public variable after opening the form:

   DoCmd.OpenForm "frm_search_detail", acNormal
   With Forms!frm_search_detail
       Set .CallingForm = frm
       .txt_filter = ""
       .txt_Txt_Box_Name = TxtBoxNm
       ... etc
   End With

Finally, in cmd_OK_Click, set the value of the control on CallingForm:

   CallingForm(txt_Txt_Box_Name) = ListString

--
Graham Mandeno (Access MVP)

Random Solutions  
 
programming4us programming4us