通过查询访问访问宏(打开表单)运行时错误"2486":您目前无法执行此操作 [英] Access Call Macro from Query (Opening a Form) Run-time error '2486': You can't carry out this action at the present time

查看:105
本文介绍了通过查询访问访问宏(打开表单)运行时错误"2486":您目前无法执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个访问查询,要求在表单的组合框中设置一个值才能正常工作

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

推荐答案

通常,我讨厌Microsoft预先编程"的东西,我宁愿自己做.看来这也是您的情况...

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

这篇关于通过查询访问访问宏(打开表单)运行时错误"2486":您目前无法执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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