gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:16:43 24 4
gpt4 key购买 nike

我有一个表格 Cases以及显示我看过的患者的 session 日期。我正在尝试创建一种方法,通过该方法我可以双击任何客户的名称,它会打开一个更详细的不同表单。我希望能够对多个患者执行此操作,例如,我可以为我今天要见的客户选择并打开更详细的表格。

显示摘要详细信息的表单称为 CaseAndSessionDetail和更详细的表格ClientDetails .

我正在使用在 Windows 8.1 下运行的 Ms-Access 2010

我已经编写/复制了一个模块,我认为应该通过使用全局变量来创建表单集合来做我想做的事情:

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

如果您在最后一行 mcolFormInstances.Add(frm) 设置断点,该功能将起作用并选择并打开/显示正确的客户详细信息表单.没有断点,表单将再次关闭(我怀疑是因为变量在函数结束后超出范围。

作为引用,该函数由 "CaseAndSessionDetail" 形式的宏调用。
If Not ISNull([Screen].[ActiveControl] Then 
Function OpenForm("frmContactDetails" = '"& Ltrim([Screen].[ActiveControl]) & "'")
EndIF

我怀疑我没有正确地将集合对象声明为全局变量。我尝试在单独的模块中以两种形式声明它,使用 PublicGlobal并且还没有找到成功。

我意识到我可能忽略了一些非常简单的事情,但欢迎任何帮助

最佳答案

您的问题只是这一行的一个晦涩的语法错误:
mcolFormInstances.Add (frm)
参数 frm 周围的括号是 不是 围绕参数列表 - 这就是 VBE 在 .Add 之间插入空格的原因和 (frm)当您尝试键入 mcolFormInstances.Add(frm) 时为您服务.这些在语义上是完全不同的。 VBA 语言规范的第 5.6.6 节描述了在此上下文中用括号括起来的表达式:

5.6.6 Parenthesized Expressions

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.



这意味着您正在评估运行时表达式 frm作为一种简单的数据类型并将其存储在您的 Collection 中.不管那个值实际上是多少 (我必须检查评估树以确定它的评估结果),您没有增加 frm 上的引用计数, 这允许它超出范围并以 OpenFormInstance 的形式发布正在退出。

关于ms-access - VBA 使用全局变量在 Access 2010 中打开表单的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com