Accessing a Macro Call from a Request (Opening a Form) Runtime Error "2486": You cannot perform this action at this time

I have an access request that requests a value that needs to be set in a combo box in a form in order to work

Criteria: Forms![_SelectCustomer]![CmbSelectCustomer] 

So far so good, however, I would like the request to open, read and close this form programmatically when it is run using a macro.

I am following @ David-W-Fenton's answer to an IT similar stack overflow question and came 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 the criteria field of the property that I want to filter in Query:

 Like rtnSelectCustomer() 

At this moment, I am facing several problems:

  • First, I’m not sure where to place the actual code: I can’t create a specific class or module for my request in the “Microsoft Access Class Objects” folder, so I resorted to creating my own module in the “Modules” folder. (Is it correct?)

  • The second problem is that when I run a query with the code in the current module that I created, I get the following error:

    Runtime Error "2486": You cannot complete this action at this time.

Any advice would be highly appreciated


Edit:

I should clarify that after further testing, a line that seems to cause a runtime error is as follows:

 DoCmd.OpenForm "_SelectCustomer", , , , , acDialog 

The function is actually called replacing the internal code with the following: it really works (although, admittedly, it's useless)

 Public Function rtnSelectCustomer() As Variant rtnSelectCustomer End Function 
+1
source share
1 answer

In general, I hate things that are "programmed" by Microsoft; I would rather do them myself. This seems to be your case too ...

I would do it in 2 steps.

Step 1. Show things to the user as if the request was being executed (without actually running) and saving the values ​​that the user selects.

Step 2. Use values ​​to parameterize the query

If your function works well, just remember what the user selects, and then:

 set qdf = new QueryDef ' set the qdf and add all parameters to it DoCmd.Execute qdf 

for further reference on how QueryDef works, I would use this msdn site

0
source

Source: https://habr.com/ru/post/1310222/


All Articles