通过模块 [VBA] 获取表单字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5618219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get form field value via module [VBA]
提问by d30DK
I have a program built with VBA, in access. I have a form with the field chDate and I need to get the value of the field in a module file named Global (not class module).
我有一个用 VBA 构建的程序,可以访问。我有一个带有 chDate 字段的表单,我需要在名为 Global(不是类模块)的模块文件中获取该字段的值。
I tried to access it but I think I get empty value, string. not error. I'm no expert with VBA, its new to me (I have experience with VBS).
我试图访问它,但我想我得到了空值,字符串。不是错误。我不是 VBA 专家,对我来说是新手(我有 VBS 经验)。
Can someone please help me and tell me how can I access the value of a form field via module file?
有人可以帮助我并告诉我如何通过模块文件访问表单字段的值吗?
Thanks!
谢谢!
回答by Harry
If you can provide some sample code, it might help us in trying to get you a solution.
如果您可以提供一些示例代码,它可能会帮助我们尝试为您提供解决方案。
Here's what might work:
以下是可能有效的方法:
Dim
an object as an instance of your form, and set the instance to a new instance. These two lines will do that (assuming the form is called frmForm
)
Dim
一个对象作为表单的实例,并将该实例设置为一个新实例。这两行将这样做(假设表单被称为frmForm
)
Dim theForm as frmForm
Set theForm = new frmForm
then Show that Form:
然后显示该表格:
theForm.show
The form will get the focus, so you can populate the fields. At that point, once the form is hidden, your code should be able to pull the field as such:
表单将获得焦点,因此您可以填充字段。那时,一旦表单被隐藏,您的代码应该能够像这样拉出该字段:
var1 = theForm.txtFormField.Text
However, if you unloadthe form in code, all variables directly tied to the form will be lost. In that case, you might want to follow Oneide's example, and set a global variable to the value of the form field. You can do this in one of the form's event handlers.
但是,如果您在代码中卸载表单,则直接绑定到表单的所有变量都将丢失。在这种情况下,您可能希望遵循 Oneide 的示例,并将全局变量设置为表单字段的值。您可以在表单的事件处理程序之一中执行此操作。
回答by Raj
Let me see if I can recall my Access days. In your global module you should access the form field be prefixing the form name i.e. var1 = Form1.txtFirmField.Text
让我看看我是否能回忆起我的 Access 时代。在您的全局模块中,您应该访问表单字段,并以表单名称作为前缀,即 var1 = Form1.txtFirmField.Text
回答by Oneide
As far as I know, you're trying to get the data the wrong way.
据我所知,您正试图以错误的方式获取数据。
Anytime you would try to get your form data, from a standard module, your form probably will already be closed or, if you're not opening it as a restricted window, the value will not be ready yet.
任何时候您尝试从标准模块获取表单数据时,您的表单都可能已经关闭,或者,如果您没有将其作为受限窗口打开,则该值还没有准备好。
Anyway, if you could get pass through all the difficulties above, you're probably coding in a not standard / right way.
无论如何,如果您能够克服上述所有困难,那么您可能正在以不标准/正确的方式进行编码。
Remember that VBA is an event drivenlanguage, and you would be better using it's strenghts rather than fighting against them.
请记住,VBA 是一种事件驱动的语言,您最好使用它的优点而不是与它们作斗争。
As a final word, if this is just a casual coding, I suggest you using a global variable and make de Code Behind Formhave set it up on control's change event.
最后,如果这只是一个随意的编码,我建议你使用一个全局变量,并在控件的更改事件上设置代码隐藏表单。
If you're not sure what I mean, please leave a comment and I'll try to explain it better.
如果您不确定我的意思,请发表评论,我会尽力解释得更好。
And, if you don't mind, let us know more about your starting code to get better answers.
而且,如果您不介意,请告诉我们更多有关您的起始代码的信息,以获得更好的答案。