how to pass rowsource from parent to child form (1 Viewer)

eugzl

Member
Local time
Today, 17:37
Joined
Oct 26, 2021
Messages
100
Hi All.
I call child form from parent form using two events

First event I created to click button btnNew. It is using to open child form to create new record. Also by this event I would like to pass query1 to populate ComboBox1 rowsource in child form.

Second event is double click record in ListBox. It used to open and populate child form by selected record. Also in this event I would like to pass query2 to populate ComboBox1 rowsource in child form.

By other words in first case I need open blank child form and populate ComboBox1 with query1. And in second case I need open and populate child form by selected record and populate ComboBox1 with query2.
I will appreciate if someone will show how it to do.
Thanks
 

June7

AWF VIP
Local time
Today, 13:37
Joined
Mar 9, 2014
Messages
4,317
One way to is to pass a value to child form via OpenArgs then code in child form Open event sets combobox RowSource:

If Me.OpenArgs = "Q1" Then
Me.Combobox1.RowSource = "This SQL"
Else
Me.Combobox1.RowSource = "Other SQL"
End If

Open form to new record and set OpenArgs
DoCmd.OpenForm "formaname", , , , , acFormAdd, "Q1"

Open form to existing record and set OpenArgs
DoCmd.OpenForm "formname", , , "ID=" & Me.listbox , , , "Q2"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:37
Joined
Feb 28, 2001
Messages
22,560
June7 shows you how to let the child form do the work directly.

The other way is that you can change the .Rowsource before you launch the child form. To do this, you must know the name of the subform control that houses the subform.

For changing control properties in the CURRENT form, it is of course Me.controlname.property (e.g. Me.controlname.Rowsource = <<SQL string>>

For the control properties of the child form, it is Me.subformcontroname.Form.controlname.property, such as Me.subformcontrolname.Form.controlname.Rowsource = <<SQL string>>

Obviously, you have TWO control names here - the name of the control that houses the subform, which appears on the parent form, and the name of the control on the child form that you wanted to update.

If you have lots of properties to change in that subform, you can do this to shorten the typing

Code:
Dim sbfrm as Access.Form
...
Set sbfrm = Me.subformcontrolname.Form
sbfrm.controlname.Rowsource = <<SQL string>>
 

eugzl

Member
Local time
Today, 17:37
Joined
Oct 26, 2021
Messages
100
Thanks useful information. That is my first project and any new information is very helpful.
I used code that suggested June7. It works fine. But it works for one ComboBox and two queries. That is exactly like I asked before. But I modified the form and now I have two similar ComboBoxes and according four queries.
The question is. How to assign different rowsource Q1 to ComboBox1 and rowsources Q2 to ComboBox2 if child form open to create new record. And if child form open to update a record how to assign rowsource Q3 to ComboBox1 and rowsource Q4 to ComboBox2?
Thanks
 

June7

AWF VIP
Local time
Today, 13:37
Joined
Mar 9, 2014
Messages
4,317
Okay, then OpenArgs aren't really needed.

If Me.NewRecord Then
Me.Combobox1.RowSource = "SQL1 statement"
Me.Combobox2.RowSource = "SQL2 statement"
Else
Me.Combobox1.RowSource = "SQL3 statement"
Me.Combobox2.RowSource = "SQL4 statement"
End If
 

eugzl

Member
Local time
Today, 17:37
Joined
Oct 26, 2021
Messages
100
And this IF statement must be in the Form_Open() or Form_Current() procedure of the child form? I coded in in Form_Open() but don't know if it is correct.
Thanks
 
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 17:37
Joined
Jan 23, 2006
Messages
14,253
This "seems" to be a cross post of
And

I realize that the OP is relatively new. But I have asked repeatedly for a description of the requirement in plain English. Just trying to understand the requirement.
 

bastanu

AWF VIP
Local time
Today, 14:37
Joined
Apr 13, 2010
Messages
1,172
As suggested in your other post(s) the best place to dynamically change the row source of each combo is in the Enter or Got Focus of that combo, as that is when you are actually using it.

Cheers,
 

Users who are viewing this thread

Top Bottom