vba 如何使用表单名称作为字符串创建新的表单实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10917896/
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
How to create a new form instance using the name of the form as a String
提问by Ryan Clarke
Code to create new form instance of a closed form using form name
使用表单名称创建封闭表单的新表单实例的代码
I want to replace the long Select Case list with a variable.
我想用一个变量替换长的 Select Case 列表。
In Access 2010 I have a VBA function that opens a new instance of a form when given a string containing the form's name. By adding a form variable "frm" to a collection:
在 Access 2010 中,我有一个 VBA 函数,当给定一个包含表单名称的字符串时,它会打开一个新的表单实例。通过向集合添加表单变量“frm”:
mcolFormInstances.Add Item:=frm, Key:=CStr(frm.Hwnd)
The only way I can figure out to open "frm" is with a Select Case statement that I've manually entered.
我能找到的打开“frm”的唯一方法是使用我手动输入的 Select Case 语句。
Select Case strFormName
Case "frmCustomer"
Set frm = New Form_frmCustomer
Case "frmProduct"
Set frm = New Form_frmProduct
... etc ... !
End Select
I want it to do it automatically, somewhat like this (although this doesn't work):
我希望它自动执行,有点像这样(虽然这不起作用):
Set frm = New Eval("Form_" & strFormName)
Or through some code:
或者通过一些代码:
For Each obj In CurrentProject.AllForms 'or AllModules, neither work
If obj.Name = strFormName Then
Set FormObject = obj.AccessClassObject 'or something
End If
Next
Set frm = New FormObject
I just want to avoid listing out every single form in my project and having to keep the list updated as new forms are added.
我只是想避免列出我项目中的每个表单,并且在添加新表单时必须更新列表。
回答by HK1
I've also done some testing of my own and some reading online about this. As near as I can tell, it isn't possible to create a new form object and set it to an instance of an existing form using a string that represents the name of that form without using DoCmd.OpenForm.
我也做了一些我自己的测试和一些关于这个的在线阅读。就我所知,在不使用 DoCmd.OpenForm 的情况下,不可能使用表示该表单名称的字符串创建新的表单对象并将其设置为现有表单的实例。
In other words, unless someone else can prove me wrong, what you are trying to do cannot be done.
换句话说,除非其他人可以证明我是错的,否则您尝试做的事情是无法完成的。
回答by Joshua Utter
Here's an ugly hack I found:
这是我发现的一个丑陋的黑客:
DoCmd.SelectObject <acObjectType>, <YourObjectsName>, True
DoCmd.RunCommand acCmdNewObjectForm
The RunCommand step doesn't give you programmatic control of the object, you'll have to Dim a Form variable and Set using Forms.Item(). I usually close the form after DoCmd.RunCommand, then DoCmd.Rename with something useful (my users don't like Form1, Form2, etc.).
RunCommand 步骤不会让您以编程方式控制对象,您必须使用 Forms.Item() 对 Form 变量进行 Dim 和 Set。我通常在 DoCmd.RunCommand 之后关闭表单,然后在 DoCmd.Rename 之后使用一些有用的东西(我的用户不喜欢 Form1、Form2 等)。
Hope that helps.
希望有帮助。
回答by ron tornambe
I think you are looking for something like this MS-Access 2010 function. (The GetForm sub is just for testing):
我认为您正在寻找类似 MS-Access 2010 功能的功能。(GetForm sub 仅用于测试):
Function SelectForm(ByVal FormName As String, ByRef FormExists As Boolean) As Form
For Each f In Application.Forms
If f.Name = FormName Then
Set SelectForm = f
FormExists = True
Exit Function
End If
Next
FormExists = False
End Function
Sub GetForm(ByVal FormName As String)
Dim f As New Form
Dim FormExists As Boolean
Set f = SelectForm(FormName, FormExists)
If FormExists Then
MsgBox ("Form Found: " & f.Caption)
Else
MsgBox ("Form '" & FormName & "' not found.")
End If
End Sub