如何使用表单名称作为字符串创建新的表单实例 [英] How to create a new form instance using the name of the form as a String

查看:61
本文介绍了如何使用表单名称作为字符串创建新的表单实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用表单名称创建关闭表单的新表单实例的代码

Code to create new form instance of a closed form using form name

我想用一个变量替换冗长的选择案例"列表.

模块的完整代码

在Access 2010中,我有一个VBA函数,当给定包含表单名称的字符串时,该函数将打开表单的新实例.通过向集合添加表单变量"frm":

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:

mcolFormInstances.Add Item:=frm, Key:=CStr(frm.Hwnd)

我唯一想打开"frm"的方法是使用我手动输入的Select Case语句.

The only way I can figure out to open "frm" is with a Select Case statement that I've manually entered.

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)

或通过一些代码:

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.

推荐答案

我也做了一些自己的测试,并在线阅读了一些内容.据我所知,不使用DoCmd.OpenForm,就不可能使用表示该表单名称的字符串来创建新的表单对象并将其设置为现有表单的实例.

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.

换句话说,除非别人能证明我错了,否则您尝试做的事情将无法完成.

In other words, unless someone else can prove me wrong, what you are trying to do cannot be done.

这篇关于如何使用表单名称作为字符串创建新的表单实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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