2017-09-25 48 views
0

我有一個父窗體,我點擊一個按鈕,啓動第二個窗體進一步用戶輸入,一旦這些值被輸入,然後我需要返回值的父窗體。我如何從第二種形式返回值到第一種形式?從訪問表單傳遞變量到訪問表單

這是我當前的代碼:

'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 
+0

選項:全局變量TempVars,form 2設置form1上的文本框的值。 – June7

+0

@ June7 - 我如何用Global Variable或TempVars來做這些事情? – IcyPopTarts

+1

我從來沒有把TempVars使用,只讀了。我避免了全局變量,因爲當代碼中斷時它們會失去價值 - 這是發展中的一個真正的麻煩。在模塊頭部聲明全局變量。如果您在表單或報表中執行此變量,則該變量可用於該對象中的所有過程。聲明在一個通用模塊中,並可在db中的任何地方使用。 – June7

回答

1

有很多的方法可以從一種形式傳遞值到另一個。我更喜歡直接從表單中讀取值。我創建了一個公共函數,它返回所需的信息。事情是這樣的:

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 

對話框的形式接受直通OpenArgs參數的參數,當用戶點擊確定或取消按鈕,我們隱藏對話框的形式,而不是收盤:

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 

我,我們需要返回幾值,通過引用使用函數參數。