Solved Pass value from subform to mainform (1 Viewer)

VzqCrs

Member
Local time
Today, 00:58
Joined
Nov 15, 2021
Messages
54
Hi All,

I've searched and only found ways to pass values from main form to sub form but not the other way around.

Example: I have a form that when a button is clicked, another form opens to write a reason for the decision. This text will be used in an outgoing email.

So Form 1: Form 1 is in a Navigation form, so it's already a SubForm. Option to click a button to open form 2
Form 2 (unbound): resembles an input box because I need to add a few lines that the input box doesn't allow.

Now I want to take the input from the text box in Form 2 and put that value in a text box on my main form. This is the value that I'm referencing in my email message.

I've tried creating a function and referencing it but no cigar. Then I tried setting the default value on Form 1's textbox to [Forms]!....etc. and nothing.

Please advise.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:58
Joined
May 21, 2018
Messages
8,525
Your title says subform to mainform. What you describe sounds like a popup form. Do you want to get a value from a popup?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:58
Joined
Oct 29, 2018
Messages
21,455
Hi. Can you post the code that works from form to subform? Usually, from subform to main form simply refers to the Parent property.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:58
Joined
May 21, 2018
Messages
8,525
Here is a function I use to get a value from a popup. Requires an OK, Cancel button on the popup. If this is not a pop up form then disregard
 

VzqCrs

Member
Local time
Today, 00:58
Joined
Nov 15, 2021
Messages
54
Your title says subform to mainform. What you describe sounds like a popup form. Do you want to get a value from a popup?
I looked at the link you shared and wrote this but nothing came out...

In the Module:
Public Function getValueFromPopUp(formName As String, PopUpControlNae As String, Optional MyOpenArgs As String = "None") As Variant
'FormName: Name of the popup form
'PopupControlName: Name of the control on the pop up/dialog that you want the value
'MyOpenArgs: If you want to pass something to your pop up.

Dim frm As Access.Form
Dim ctrl As Access.Control
'Code execution stops after next line of code

DoCmd.OpenForm "frmRejected", , , , acFormEdit, acDialog, MyOpenArgs

'Wait until form is closed or hidden
'The popup needs an OK button that hides the popup(me.visible = false), and a Cancel button that just closes it

If CurrentProject.AllForms(frmRejected).IsLoaded Then

Set frm = Forms(frmRejected)
Set ctrl = frm.Controls(RejectRsn)

If ctrl.ControlType = acLabel Then
getValueFromPopUp = frm.Controls(RejectRsn).Caption

Else

getValueFromPopUp = frm.Controls(RejectRsn).Value

End If

DoCmd.Close acForm, frm.Name

End If
End Function

------------------------------------------
Behind the main form's button: (partial code, there's other stuff before this that's not relevant to this convo)
Dim sRejectNotes As Variant
sRejectNotes = getValueFromPopUp("frmRejectReason", "RejectRsn", Nz(Me.txtNotes, ""))
If Not IsNull(sRejectNotes) Then Me.txtNotes = sRejectNotes

Me.txtNotes = sRejectNotes

sSubj = "*** REQUEST REJECTED: & VBA.UCase(Me.txtRequestTitle.Value) & " ***"

sBody = "<p> " & _

"<br> Hello " & sF & "," & _

"<br> Your Request " & Me.txtRequestTitle.Value & " has been rejected because: " & txtNotes & ". " & _

"<br> Please address the concern(s) and resubmit."

Debug.Print sBody

SendEmail sTo, Nz(Me.txtCCEMails), sSubj, sBody
 

VzqCrs

Member
Local time
Today, 00:58
Joined
Nov 15, 2021
Messages
54
Hi. Can you post the code that works from form to subform? Usually, from subform to main form simply refers to the Parent property.
Yes, I believe you helped me with it...[Forms]![NavigationForm].[NavigationSubForm].[Form]![txtRequestTitle]
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:58
Joined
Feb 19, 2002
Messages
43,233
There is the concept of "push" and the concept of "pull". You are asking for "pull" but "push" is more appropriate in this case. Once the popup form has validated the data entry, "push" it back to the main form by referencing the form fields you want to fill.

Forms!frmNavigation!frmsfrm.Form!fld1 = Me.fld1
Forms!frmNavigation!frmsfrm.Form!fld2 = Me.fld2

The code you just posted should work. Do you get an error message?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:58
Joined
May 21, 2018
Messages
8,525
I looked at the link you shared and wrote this but nothing came out.
That was a demo. You would have to tailor it to meet your needs.
 

VzqCrs

Member
Local time
Today, 00:58
Joined
Nov 15, 2021
Messages
54
There is the concept of "push" and the concept of "pull". You are asking for "pull" but "push" is more appropriate in this case. Once the popup form has validated the data entry, "push" it back to the main form by referencing the form fields you want to fill.

Forms!frmNavigation!frmsfrm.Form!fld1 = Me.fld1
Forms!frmNavigation!frmsfrm.Form!fld2 = Me.fld2

The code you just posted should work. Do you get an error message?
I didn't get anything for my Me.txtNotes = sRejectNotes
 

bastanu

AWF VIP
Local time
Yesterday, 21:58
Joined
Apr 13, 2010
Messages
1,402
You altered it wrong, please review the updated module:
Code:
Public Function getValueFromPopUp(formName As String, PopUpControlName As String, Optional MyOpenArgs As String = "None") As Variant
'FormName: Name of the popup form
'PopupControlName: Name of the control on the pop up/dialog that you want the value
'MyOpenArgs: If you want to pass something to your pop up.

Dim frm As Access.Form
Dim ctrl As Access.Control
'Code execution stops after next line of code

'DoCmd.OpenForm "frmRejected", , , , acFormEdit, acDialog, MyOpenArgs
DoCmd.OpenForm formName, , , , acFormEdit, acDialog, MyOpenArgs  'open the form that was passed as the first argument
'Wait until form is closed or hidden
'The popup needs an OK button that hides the popup(me.visible = false), and a Cancel button that just closes it

If CurrentProject.AllForms(formName).IsLoaded Then
    Set frm = Forms(formName)
    Set ctrl = frm.Controls(PopUpControlName)

    If ctrl.ControlType = acLabel Then
        getValueFromPopUp = frm.Controls(PopUpControlName).Caption
    Else
        getValueFromPopUp = frm.Controls(PopUpControlName).Value

    End If

'DoCmd.Close acForm, frm.Name

End If
End Function

You also need to ensure that there are two buttons on the frmRejectReason pop-up form as explained in the comments on the module: the OK that simply hides the popup and another that closes it.

Cheers,
 

VzqCrs

Member
Local time
Today, 00:58
Joined
Nov 15, 2021
Messages
54
You altered it wrong, please review the updated module:
Code:
Public Function getValueFromPopUp(formName As String, PopUpControlName As String, Optional MyOpenArgs As String = "None") As Variant
'FormName: Name of the popup form
'PopupControlName: Name of the control on the pop up/dialog that you want the value
'MyOpenArgs: If you want to pass something to your pop up.

Dim frm As Access.Form
Dim ctrl As Access.Control
'Code execution stops after next line of code

'DoCmd.OpenForm "frmRejected", , , , acFormEdit, acDialog, MyOpenArgs
DoCmd.OpenForm formName, , , , acFormEdit, acDialog, MyOpenArgs  'open the form that was passed as the first argument
'Wait until form is closed or hidden
'The popup needs an OK button that hides the popup(me.visible = false), and a Cancel button that just closes it

If CurrentProject.AllForms(formName).IsLoaded Then
    Set frm = Forms(formName)
    Set ctrl = frm.Controls(PopUpControlName)

    If ctrl.ControlType = acLabel Then
        getValueFromPopUp = frm.Controls(PopUpControlName).Caption
    Else
        getValueFromPopUp = frm.Controls(PopUpControlName).Value

    End If

'DoCmd.Close acForm, frm.Name

End If
End Function

You also need to ensure that there are two buttons on the frmRejectReason pop-up form as explained in the comments on the module: the OK that simply hides the popup and another that closes it.

Cheers,
Thank you!
 

Users who are viewing this thread

Top Bottom