vba Access VBA中声明和设置表单变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41600859/
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
Declaring and Setting Form Variable in Access VBA
提问by Steve W
It seems so simple, but I can't seem to find the correct syntax. I am looking to create a form variable for an unopened form in my Access 2013 application so I can pass the form object to a series of methods rather than passing separate properties of the form.
看起来很简单,但我似乎找不到正确的语法。我希望在我的 Access 2013 应用程序中为一个未打开的表单创建一个表单变量,以便我可以将表单对象传递给一系列方法,而不是传递表单的单独属性。
Here's what I've been attempting, but I run into Run-time error '2465' "Microsoft Access can't find the field 'frmSuppliers' referenced in your expression.
这是我一直在尝试的内容,但我遇到了运行时错误“2465”“Microsoft Access 找不到您的表达式中引用的字段“frmSuppliers”。
Private Sub cmdSuppliers_Click()
Dim frm As Form
Set frm = Form("frmSuppliers")
Debug.Print frm.Name
回答by MoondogsMaDawg
Private Sub cmdSuppliers_Click()
Dim frm As Form
Set frm = Forms("frmSuppliers")
Debug.Print frm.Name
You referenced the Forms collection but made it singular instead of plural.
您引用了 Forms 集合,但将其设为单数而不是复数。
回答by Steve W
Thank you Sorceri and Christopher. I seem to have been missing a compilation of the wrong reference to the forms collection and not opening the form prior to setting the variable. This code seems to do the trick:
谢谢 Sorceri 和克里斯托弗。我似乎错过了对表单集合的错误引用的编译,并且在设置变量之前没有打开表单。这段代码似乎可以解决问题:
Private Sub cmdSuppliers_Click()
Dim frm As Form
DoCmd.OpenForm "frmSuppliers", acNormal, , , , acHidden
Set frm = Forms("frmSuppliers")
Debug.Print frm.Name
DoCmd.Close acForm, frm.Name
Set frm = Nothing
End Sub