Run Macro from a query (MS ACCESS 2013)

0

Is it possible to run a macro from a query in ACCESS 2013?

I need to run it just by clicking on the query, I do not know if this is possible with clauses such as PROCEDURE, EXECUTE ...

-

.

Attending the suggestion of calling the macro with a function that in turn calls a Sub, try the following

Function funcion1() 

Call proc1 

End Function
Sub proc1()

    DoCmd.OpenQuery "Consulta1", acViewNormal, acReadOnly
    DoCmd.Close acQuery, "consulta1"
    DoCmd.OpenQuery "Consulta2", acViewNormal, acReadOnly


End Sub

Each one works perfectly when I run them from VBA, but I tried to call them from the query with

SELECT funcion1()

But mark an error '2486' this action can not be executed now

Now my main question to the suggestion is: is Select the correct way to call the function from the SQL window? Or what clause should I use?

    
asked by Enrique Salazar 24.07.2018 в 21:14
source

1 answer

0

Yes you can, although in a couple of steps.

First, no, you can not call a Sub procedure from a query. But nevertheless, you can call a UDF that in turn calls the Sub.

  

To execute a Sub procedure or written event procedure   In Visual Basic, create a Function procedure that calls the   Sub procedure or an event procedure. Next, use the   macro action RunCode to execute the procedure of the   function.

You have all the information in Macro Action RunCode

UPDATE: I give a simple example. On the one hand, I have in a module the Sub that I want to execute. Mine just makes an Msgbox.

Sub SALUDAR()
Debug.Print "Hola" 'Escribe Hola en la ventana Inmediato
End Sub

I also have the function defined. My UDF simply calls the previous sub:

Function LLAMAR_A_SUB()
Call SALUDAR
End Function

Then, my query is like this (I have called it Query1)

SELECT LLAMAR_A_SUB() AS Expr1;

And finally, I've replicated your way to open the queries from VBA.

Sub PRUEBAS()

Application.DoCmd.OpenQuery "Consulta1"
Application.DoCmd.Close acQuery, "consulta1", acSaveNo

End Sub

If I run the Sub tests, it first opens Query1, but this query has a calculated field that calls the function CALL A SUB, and when the function is executed, then the Sub Greet is executed. And you will see that in the immediate window you get the word Hello.

    
answered by 25.07.2018 в 11:34