从访问表单传递变量到访问表单
问题描述:
我有一个父窗体,我点击一个按钮,启动第二个窗体进一步用户输入,一旦这些值被输入,然后我需要返回值的父窗体。我如何从第二种形式返回值到第一种形式?从访问表单传递变量到访问表单
这是我当前的代码:
'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 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
我,我们需要返回几值,通过引用使用函数参数。
选项:全局变量TempVars,form 2设置form1上的文本框的值。 – June7
@ June7 - 我如何用Global Variable或TempVars来做这些事情? – IcyPopTarts
我从来没有把TempVars使用,只读了。我避免了全局变量,因为当代码中断时它们会失去价值 - 这是发展中的一个真正的麻烦。在模块头部声明全局变量。如果您在表单或报表中执行此变量,则该变量可用于该对象中的所有过程。声明在一个通用模块中,并可在db中的任何地方使用。 – June7