将变量从访问表单传递到访问表单
Posted
技术标签:
【中文标题】将变量从访问表单传递到访问表单【英文标题】:Pass Variables From Access Form To Access Form 【发布时间】:2017-09-25 00:25:03 【问题描述】:我有一个父表单,我单击一个按钮启动第二个表单以供用户进一步输入,一旦输入这些值,我需要将值返回到父表单。如何将值从第二种形式返回到第一种形式?
这是我当前的代码:
'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
【问题讨论】:
选项:全局变量、TempVars、form 2 设置 form1 上文本框的值。 @June7 - 我将如何使用全局变量或 TempVars 做到这一点? 我从未使用过 TempVars,只是阅读了一下。我避免使用全局变量,因为它们在代码中断时会失去价值——这在开发中是一个真正的麻烦。在模块头中声明全局变量。如果您在表单或报表中执行此操作,则该变量可用于该对象中的所有过程。在通用模块中声明并在数据库中的任何位置可用。 【参考方案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
我需要返回几个值,通过引用使用函数参数。
【讨论】:
以上是关于将变量从访问表单传递到访问表单的主要内容,如果未能解决你的问题,请参考以下文章