简体   繁体   English

VBA使用全局变量在Access 2010中打开表单的多个实例

[英]VBA Using Global Variables to open Multiple instances of a form in Access 2010

I have a form Cases and session dates that display the patients that I have seen. 我有一个表格Cases和会话日期,显示我见过的病人。 I am trying to create a method by which I can double click on any client's name and it opens up a different form with more detail. 我正在尝试创建一种方法,通过该方法我可以双击任何客户端的名称,它会打开一个更详细的不同表单。 I want to be able to do this with multiple patients, so for example, I could select and have open the more detailed forms for the clients I am going to see today. 我希望能够与多名患者一起做到这一点,例如,我可以为我今天要看的客户选择并打开更详细的表格。

The form that displays the summary detail is called CaseAndSessionDetail and the more detailed form ClientDetails . 显示摘要详细信息的表单称为CaseAndSessionDetail ,更详细的形式称为ClientDetails

I am using Ms-Access 2010 running under windows 8.1 我正在使用在Windows 8.1下运行的Ms-Access 2010

I have written/copied a module which I think should do what I want by using a global variable to create a collection of forms: 我已经编写/复制了一个模块,我认为应该通过使用全局变量创建表单集合来实现我想要的模块:

Option Explicit

'This holds the collection of forms as they are created
Global mcolFormInstances As New Collection

Function OpenFormInstance(FormName As String, WhereCondition As String)
    'Declare for name
    Dim frm As form

    Select Case FormName
    Case "ClientDetails"
        Set frm = New Form_ClientDetails
    Case Else
        Debug.Assert False
    End Select
    If WhereCondition <> "" Then
        frm.Filter = WhereCondition
        frm.FilterOn = True
    End If
    ''make the form visible
    frm.Visible = True
    'Need to add a reference to the form so that it does not
    'immediately close when the for variable goes out of scope
    mcolFormInstances.Add (frm)
End Function

And the function works and selects and opens/display the correct client details form if you set a breakpoint at the last line mcolFormInstances.Add(frm) . 如果在最后一行mcolFormInstances.Add(frm)设置断点,该函数将工作并选择并打开/显示正确的客户端详细信息表单。 Without the breakpoint the form is closed again (I suspect because the variable goes out of scope after the function ends. 没有断点,表单再次关闭(我怀疑因为变量在函数结束后超出了范围。

For reference the function is called by macro on the form "CaseAndSessionDetail" 作为参考,函数在宏"CaseAndSessionDetail"上由宏调用

If Not ISNull([Screen].[ActiveControl] Then 
    Function OpenForm("frmContactDetails" = '"& Ltrim([Screen].[ActiveControl]) & "'") 
EndIF

I suspect that I am not declaring the collection object as a global variable correctly. 我怀疑我没有正确地将集合对象声明为全局变量。 I have tried declaring it in both forms in a separate module, using Public and Global and have yet to find success. 我尝试在单独的模块中使用PublicGlobal在两种形式中声明它,但尚未找到成功。

I realize I am probably overlooking something very simple but would welcome any help 我意识到我可能忽略了一些非常简单的事情,但欢迎任何帮助

Your issue is simply an obscure syntax mistake on this line: 你的问题只是这行上一个模糊的语法错误:

mcolFormInstances.Add (frm)

The parentheses around the argument frm are not surrounding an argument list - that is why the VBE inserted a space between .Add and (frm) for you when you tried to type mcolFormInstances.Add(frm) . 参数frm周围的括号围绕参数列表 - 这就是当您尝试键入mcolFormInstances.Add(frm)时VBE为您插入.Add(frm)之间的空格的原因。 These are completely different semantically. 这些在语义上完全不同。 Surrounding an expression with parentheses in this context is described in section 5.6.6 of the VBA language specification: 在VBA语言规范的5.6.6节中描述了在此上下文中带括号的表达式:

5.6.6 Parenthesized Expressions 5.6.6带括号的表达式

A parenthesized expression consists of an expression enclosed in parentheses. 括号的表达式由括在括号中的表达式组成。

Static semantics. 静态语义。 A parenthesized expression is classified as a value expression, and the enclosed expression must able to be evaluated to a simple data value. 带括号的表达式被分类为值表达式,并且所包含的表达式必须能够被计算为简单的数据值。 The declared type of a parenthesized expression is that of the enclosed expression. 括号表达式的声明类型是所包含表达式的声明类型。

  parenthesized-expression = "(" expression ")" 

Runtime semantics. 运行时语义。 A parenthesized expression evaluates to the simple data value of its enclosed expression. 带括号的表达式求值为其包含的表达式的简单数据值。 The value type of a parenthesized expression is that of the enclosed expression. 带括号的表达式的值类型是所包含表达式的值类型。

What this means is that you are evaluating the run-time expression frm as a simple data type and storing that in your Collection . 这意味着,你正在评估运行时表达frm作为一个简单的数据类型和存储您的Collection Regardless of what that value actually is (I'd have to check the evaluation tree to figure out what it evaluates to), you aren't incrementing the reference count on frm , and this allows it to go out of scope and get released as OpenFormInstance is exiting. 无论该值实际什么(我必须检查评估树以确定它的评估结果),你没有增加frm上的引用计数,这使它超出范围并被释放为OpenFormInstance正在退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM