更新时间:2023-12-06 16:33:04
使用对象而不是使用全局/默认实例。
Sub DoSomething()
/ pre>
Dim parentForm As frmDatastation1
Set parentForm = New frmDatastation1
parentForm.Show vbModal
End Sub没有必要真的
卸载
任何东西 - 对象引用enceparentForm
将在执行退出DoSomething
时死机,对象超出范围。Sub ShowChildForm()
Dim childForm As frmDatastation2
Set childForm = New frmDatastation2
childForm.Show vbModal
End Sub为使您的两个对象彼此交流,您需要传递对象引用。说孩子表单需要一个对父母的引用,它可以有一个这样的属性:
私人父窗体作为frmDatastation1
公共属性获取父()作为frmDatastation1
设置Parent = parentForm
结束属性
公共属性集父(ByVal值作为frmDatastation1)
设置parentForm = value
End Property然后父表单可以这样做:
Sub ShowChildForm()
Dim childForm As frmDatastation2
Set childForm = New frmDatastation2
设置childForm.Parent = Me
childForm.Show vbModal
textbox1.Text = childForm.textbox12.Text
End Sub...虽然子表单可以访问其父表单,如下所示:
Sub DoSomething()
Me.Caption = Parent.textbox1.Text
End Sub如果你想要能够交流确定已关闭但尚未卸载/丢弃的表单的属性,不要关闭它 - 只是隐藏它(您将需要在
QueryClose
以防止在用户X-out的窗体中丢弃该对象。I have two userforms (
frmDatastation1
andfrmDatastation2
). When the user clicks on cmdbutton onfrmDatastation1
, the first form becomes hidden (frmDatastation.hide
) and the second appears (frmDatastation.show
). That works fine. When the user is done withfrmDatastation2
, I'd like to go back tofrmDatastation1
and unloadfrmdatastation2
(to erase all it's content to use it again later fromfrmDatastation1
). But when I unloadfrmDatastation2
, everything closes (evenfrmDatastation1
).Here's my code in
frmDatastation2
:frmDatastation1.TextBox1.Text = "" 'this is to reset my textbox not sure it's needed Me.Hide 'hiding frmDatastation2 Unload frmDatastation1 frmDatastation1.Show vbModeless ' to be able to continue with the next step frmDatastation1.TextBox1.SetFocus Unload frmDatastation2 'when this line is executed, it closes everything (even frmDatastation1)
Use objects instead of using the global/default instance.
Sub DoSomething() Dim parentForm As frmDatastation1 Set parentForm = New frmDatastation1 parentForm.Show vbModal End Sub
There's no need to really
Unload
anything then - the object referenceparentForm
will die when execution exitsDoSomething
and the object goes out of scope.Sub ShowChildForm() Dim childForm As frmDatastation2 Set childForm = New frmDatastation2 childForm.Show vbModal End Sub
To make your two objects "talk" to each other, you need to pass object references around. Say the child form needs a reference to the parent, it could have a property like this:
Private parentForm As frmDatastation1 Public Property Get Parent() As frmDatastation1 Set Parent = parentForm End Property Public Property Set Parent(ByVal value As frmDatastation1) Set parentForm = value End Property
And then the parent form could do this:
Sub ShowChildForm() Dim childForm As frmDatastation2 Set childForm = New frmDatastation2 Set childForm.Parent = Me childForm.Show vbModal textbox1.Text = childForm.textbox12.Text End Sub
...While the child form could access its parent form like this:
Sub DoSomething() Me.Caption = Parent.textbox1.Text End Sub
If you want to be able to access the properties of a form that's closed but not yet unloaded/discarded, don't close it - just hide it instead (you'll need to implement the behavior in
QueryClose
to prevent discarding the object when the user "X-out"'s of the form).