Access Call Macro from Query (Opening a Form) Run-time error '2486':您目前无法执行此操作 [英] Access Call Macro from Query (Opening a Form) Run-time error '2486': You can't carry out this action at the present time
问题描述
我有一个访问查询,需要在表单的组合框中设置一个值才能工作
I have an Access Query that requiers a value to be set in a combo-box within a form in order to work
Criteria: Forms![_SelectCustomer]![CmbSelectCustomer]
到目前为止一切顺利,但是,我希望查询在使用宏运行时以编程方式打开、读取和关闭此表单.
So far so good, however, I would like the query to open, read and close this form programatically when it is run using a macro.
我一直在关注@David-W-Fenton 在这个类似的堆栈溢出问题中的回答 并提出了以下代码:
I have been following @David-W-Fenton's answer in THIS similar stack overflow question and have come up with the following code:
Public Function rtnSelectCustomer() As Variant
DoCmd.OpenForm "_SelectCustomer", , , , , acDialog
With Forms![_SelectCustomer]
If .Tag <> "Cancel" Then
rtnSelectCustomer = Nz(!CmbSelectCustomer, "*")
Else
rtnSelectCustomer = "*"
End If
End With
Close acForm, "_SelectCustomer"
End Function
我从要在查询中过滤的属性的标准字段中调用此函数:
I call this function from within the criteria field of the property I want to filter by in the Query:
Like rtnSelectCustomer()
此时我遇到了几个问题:
At this point I run into several problems:
首先,我不确定将实际代码放在哪里:我似乎无法在Microsoft Access Class Objects"文件夹中为我的查询创建特定的类或模块,所以我求助于在 Modules 文件夹中创建我自己的模块.(这是正确的方法吗?)
The first being, I'm not sure where to place the actual code: I can't seem to create a specific class or module for my query within the "Microsoft Access Class Objects" folder so I have resorted to creating my own module within the Modules folder. (Is this the correct approach?)
第二个问题是,当我使用我创建的当前模块中的代码运行查询时,出现以下错误:
The second issue is that when I run the query with the code in the current module I have created I get the following error:
运行时错误2486":您目前无法执行此操作.
Run-time error '2486': You can't carry out this action at the present time.
任何建议将不胜感激
我应该澄清一下,在进一步测试似乎导致运行时错误的行后,如下所示:
I should clarify that after further testing the line that seems to cause the Run-time error is the following:
DoCmd.OpenForm "_SelectCustomer", , , , , acDialog
该函数实际上被调用,因为用以下代码替换内部代码确实有效(尽管确实没用)
The function is actually called as replacing the internal code with the following does actually work (although is admittedly useless)
Public Function rtnSelectCustomer() As Variant
rtnSelectCustomer
End Function
推荐答案
一般来说,我讨厌微软预先编程"的东西,我宁愿自己做.看来你也是这种情况...
Generally, I hate things that are "pre-programmed" by Microsoft, I'd rather do them myself. It seems this is your case as well...
我会分两步完成.
第 1 步:向用户显示内容,就像查询正在运行一样(没有实际运行它)并存储用户选择的值.
Step1: Show things to the user as if the query was running (without actually running it) and store the values the user picks.
Step2:使用这些值来参数化查询
Step2: Use the values to parameterize the query
如果您的功能运行良好,那么只需记住用户选择的内容然后执行:
If your function works well, then simply remember what the user picks and then do:
set qdf = new QueryDef
' set the qdf and add all parameters to it
DoCmd.Execute qdf
有关 QueryDef 如何工作的进一步参考,我将使用此 msdn 站点
for further reference on how QueryDef works I would use this msdn site
这篇关于Access Call Macro from Query (Opening a Form) Run-time error &#39;2486&#39;:您目前无法执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!