VBA隐藏用户表单但保留输入的数据 [英] VBA Hide User form but retain data entered into it

查看:41
本文介绍了VBA隐藏用户表单但保留输入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我又回来了,我希望这是一个相当简单的问题.

I'm back again with what I hope is a fairly easy question.

我正在尝试在 VBA 中创建用户表单.用户将在表单中输入某些信息,然后关闭表单.我希望用户表单在用户关闭后保留输入的数据.我将它视为一个类模块,因为从技术上讲它们是,或者至少我是这样理解的.这是我正在使用的代码:

I'm attempting to create a user form in VBA. The user will enter certain bits of information into the form, and then close the form. I'd like the user form to retain the data entered after it is closed by the user. I'm treating it as a class module, since techinically they are, or at least that is how I understand it. Here is the code I'm using:

在显示用户表单的主子中:

In the main sub that displays the user form:

Sub NonACATMemo()

Dim UserInput As MemoReasons
Set UserInput = New MemoReasons
UserInput.Show

...然后在用户窗体本身中关闭它...

... And then in the user form itself to close it...

Private Sub UserForm_Terminate()
MemoReasons.Hide
End Sub

我还从表单上的命令按钮调用此子程序.我遇到的问题是,当我使用此方法时,出现错误运行时错误'402':必须首先关闭或隐藏最顶层的模态表单."如果我使用 unload me,当我尝试从表单中获取数据时,它会被清除,并且会出现服务器不可用"错误或类似情况.

I also call this sub from a command button on the form. The issue I'm running into is that when I use this method, I get an error "Run-time error '402': Must close or hide topmost modal form first." If I use unload me, when I try to get data out of the form it is cleared and I get a "server not available" error or something to that effect.

那么,关于隐藏用户表单但保留其中数据的任何想法?

So, any ideas on hiding a user form but retaining the data inside?

最后几点说明:这是项目中唯一的用户表单,下面是我如何尝试使用 Public Property Get 方法从中获取数据的示例:

Final couple of notes: This is the only user form in the project, and here is an example of how I'm trying to get data out of it using the Public Property Get method:

Debug.Print UserInput.EmailFlag
Debug.Print UserInput.ContraFirm
Debug.Print UserInput.MemoReason

好吧,如果有人有任何建议,我会全力以赴.

Well, I'm all ears if anyone has any suggestions.

推荐答案

我以前没见过这种方法.通常,我会通过以下方式实例化表单:

I've not seen this approach before. Normally, I would just instantiate the form by:

MemoReasons.Show

确实,_Terminate() 事件正在清除表单中保存的数据.所以解决方案是从按钮单击中调用 _Terminate() 事件.相反,只需隐藏表单,例如:

Indeed the _Terminate() event is wiping out the data held in the form. So the solution is to not call the _Terminate() event from the button-click. Instead, simply hide the form, e.g.:

Sub ShowMemoReasons()
'In a normal code module, this calls the form
' could be run from the macros menu or attached to
' a shape/button/etc on the worksheet.

MemoReasons.Show

End Sub

将这些放在MemoReasons代码模块中:

Put these in the MemoReasons code module:

Private Sub CommandButton1_Click()  '<-- Rename to handle your button's click event

    MemoReasons.Hide  '## Hides the form but does not release it from memory

End Sub
Private Sub UserForm_Terminate()
'Any events pertaining to the termination of the form object
' otherwise, all form control data will be wiped out when
' this object releases from memory

End Sub

完成这些操作后,如果您使用按钮隐藏表单,您可以调用 ShowMemoReasons() 并且它应该重新显示表单,同时保留之前在表单中输入的数据.

After you do these, if you use the button to HIDE the form, you can call the ShowMemoReasons() and it should re-display the form, while preserving data that was previously entered in the form.

如果您使用红色的X"按钮或其他一些事件触发了 Terminate 事件,您将丢失表单数据.如有必要,可以使用 QueryClose 事件进行验证并防止这种情况发生.

If you use the red "X" button or some other event triggers the Terminate event, you will lose the form data. There are ways to do validation and prevent this with the QueryClose event if necessary.

更新

我认为您不需要 Dim 用户表单的实例(如果您可能同时显示 多个 表单,则例外).否则,将 UserInput 声明为公共变量是多余且令人困惑的.

I don't think you need to Dim an instance of the user form (an exception would be if you will be potentially displaying multiple forms at the same time). Otherwise, declaring UserInput as a public variable is redundant and confusing.

顺便说一句,这就是您收到错误消息的原因:必须首先关闭或隐藏最顶层的模态表单.如果您必须以这种方式实现它,而不是执行 MemoReasons.hide 您应该使用 Me.Hide.

Inicidentally, this is why you're getting the error: Must close or hide topmost modal form first. If you must implement it this way, instead of doing MemoReasons.hide you should use Me.Hide.

只要你只显示表单的一个实例,你总是可以参考 MemoReasons.property 因为 MemoReasons 是一个公共对象,就像 ThisWorkbookActiveWorksheet

As long as you are only displaying one instance of the form, you can always refer to MemoReasons.property because MemoReasons is a public object, just like ThisWorkbook or ActiveWorksheet, etc.

相反,您应该能够在任何子例程中引用此对象(MemoReasons 是一个对象),例如创建另一个未被先前子程序调用的对象.运行 sub 以显示表单,输入一些数据,然后隐藏表单.隐藏表单,然后运行此子例程,您应该会看到表单的结果数据.

Instead, you should be able to refer to this object (MemoReasons is an object) in any subroutine, for example create another one that is not called from the previous subs. Run the sub to show the form, enter in some data, and then hide the form. With the form hidden, then run this subroutine, and you should see the resulting data from the form.

Sub Test2()
    Debug.Print MemoReasons.EmailFlag
End Sub

这篇关于VBA隐藏用户表单但保留输入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆