VBA隐藏用户的形式,但保留进入它的数据 [英] VBA Hide User form but retain data entered into it

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

问题描述

我回来了我什么,我希望是一个相当简单的问题。

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

我试图创建一个VBA用户窗体。用户将输入信息的某些位到表单,然后关闭窗体。我想用户的形式,它是由用户关闭后保留输入的数据。我把它当作一个类模块,因为techinically他们,或者至少是我的理解它。这里是code我使用的是:

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:必须关闭或隐藏首最顶层的模式窗体如果我使用卸载我,当我试图让出的数据被清除的形式,我得到一个服务器不可用错误或东西的效果。

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?

最后的几个注意事项:这是该项目唯一的用户的形式,这里是如何我尝试使用公共产品的获取方法来获取数据出来的一个例子:

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 code模块中的

把这些:

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(),它应该重新展示的形式,而这是$ p $ preserving数据形式pviously输入。

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按钮或其他一些事件触发终止时,你将失去表单数据。有办法做验证和prevent这与将对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.

更新

我不认为你需要点心用户表单的一个实例(一个例外是,如果你将有可能显示的多个的在同一时间的形式)。否则,声明 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.

Inicidentally,这就是为什么你得到错误:必须关闭或隐藏首最顶层的模式窗体。如果必须这样实现的,而不是做这件事, 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 是一个公开的对象,就像的ThisWorkbook ActiveWorksheet

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 是一个对象)在任何子程序,例如再创建一个没有从$ P称为$ pvious潜艇。运行子显示形式,在一些数据输入,然后隐藏的形式。隐藏表单,然后运行该子程序,你应该从形式看所得到的数据。

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天全站免登陆