将变量从 Access 窗体传递到 Access 窗体
Pass Variables From Access Form To Access Form
我有一个父表单,我单击一个按钮启动第二个表单以供用户进一步输入,一旦输入这些值,我就需要 return 将这些值添加到父表单。我如何return值从第二种形式到第一种形式?
这是我当前的代码:
'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
有很多方法可以将值从一种形式传递到另一种形式。我更喜欢直接从表单中读取值。我创建了一个 public 函数,其中 return 需要信息。像这样:
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
我需要return几个值,通过引用使用函数参数。
我有一个父表单,我单击一个按钮启动第二个表单以供用户进一步输入,一旦输入这些值,我就需要 return 将这些值添加到父表单。我如何return值从第二种形式到第一种形式?
这是我当前的代码:
'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
有很多方法可以将值从一种形式传递到另一种形式。我更喜欢直接从表单中读取值。我创建了一个 public 函数,其中 return 需要信息。像这样:
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
我需要return几个值,通过引用使用函数参数。