0

I have a parent form that I click a button which launches a second form for further user input, once those values are input I then need to return the values to the parent form. How do I return values from the second form to the first form?

This is my current code:

    'Form 1 - Main Form called frmFirstSet
Private Sub cmdDoStep1_Click()
    'Declare Variables
    Dim OrderNumber As String

    'Get the OrderNumber
    OrderNumber = Me.[frmDLZC].Form!OrderNumber

    'Open the second form for data Capture
    DoCmd.OpenForm "frmInputValues", acNormal

    'Return variables from frmInputValues
    Debug.Print green
    Debug.Print red
    Debug.Print orange

End Sub

'Form 2 - Secondary Form launched for data capture
Private Sub cmdReturnToStep1_Click()
Dim green As String, red As String, orange As String
    'Ensure all values have been input
    If IsNull(Me!txtgreen) Then
        MsgBox "Please Input the Value for green", vbOKOnly
        Me.txtgreen.SetFocus
        Exit Sub
    Else
        green = Me.txtgreen
    End If
    If IsNull(Me!txtred) Then
        MsgBox "Please Input the Value for red", vbOKOnly
        Me.txtred.SetFocus
        Exit Sub
    Else
        red = Me.txtred
    End If
    If IsNull(Me!txtorange) Then
        MsgBox "Please Input the Value for orange", vbOKOnly
        Me.txtorange.SetFocus
        Exit Sub
    Else
        orange = Me.txtorange
    End If
    'How to return these variables to the original form
End Sub
3
  • Options: Global variable, TempVars, form 2 sets value of a textbox on form1. Commented Sep 25, 2017 at 1:00
  • @June7 - how would I do such with Global Variable or TempVars? Commented Sep 25, 2017 at 1:01
  • 1
    I've never put TempVars to use, only read about. I avoid global variable because they lose value when code interrupts - a real nuisance in development. Declare global variable in module header. If you do in a form or report the variable is available to all procedures in that object. Declare in a general module and is available anywhere in the db. Commented Sep 25, 2017 at 3:00

1 Answer 1

1

There are a lot of ways to pass values from one form to another. I prefer to read value directly from the form. I create a public function, which returns required information. Something like this:

Public Function DialogInputBox(strHeader As String, Optional strValueLabel As String) As String
    On Error Resume Next
    ' make sure that hidden window closed
    DoCmd.Close acForm, "frm_InputDialog"
    On Error GoTo ErrorHandler
    ' open the form in dialog mode
    DoCmd.OpenForm "frm_InputDialog", WindowMode:=acDialog, OpenArgs:="Header=" & strHeader & "|ValueLabel=" & strValueLabel
    ' when control returns here, the form is still open, but not visible
    DialogInputBox = Nz(Forms("frm_InputDialog").txtValue, "")
    ' close the form
    DoCmd.Close acForm, "frm_InputDialog"
ExitHere:
    Exit Function
ErrorHandler:
    MsgBox "Error " & Err.Number & " (" & Err.Description & "), vbExclamation + vbMsgBoxHelpButton"
    Resume ExitHere
End Function

The dialog form accepts arguments thru OpenArgs parameter and when user clicks Ok or Cancel buttons, we hide the dialog form instead of closing:

Private Sub cmdConfirm_Click()
    If Len(Nz(Me.txtValue, "")) = 0 Then
        MsgBox "Please enter value", vbExclamation, GetDBName()
        Me.txtValue.SetFocus
        Exit Sub
    End If
    ' return execution control to the public called function
    Me.Visible = False
End Sub

I we need to return few values, use function parameters by reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.