Log in

View Full Version : [SLEEPER:] How to use Embedded Macro - RunMacro feature



gmaxey
07-08-2024, 01:34 PM
I have a command button on a subform whose on_click event is an embedded macro. The purpose is to delete/undo the associated record.
After that, I want to run another VBA procedure that is defined in the forms vb object:


Public Function RunMacroTest()
MsgBox "A"
End Function

In the embedded macro definition, I have added "RunMacro" and entered RunMacroTest as the procedure to run. I save everything, exit the design view and test. Nothing happens.

How do you run a custom procedure after an embedded macro is run?

Thanks.

June7
07-08-2024, 07:12 PM
VBA is not a macro so RunMacro is not appropriate. Try RunCode. VBA must be in a general module, not behind a form.

I don't use macros. If you are going to use VBA why use any macros?

gmaxey
07-08-2024, 07:57 PM
June7, Thank you RunCode worked. Why use macros? Because I don't know any better. I inherited a colossal mess and was asked to try to fix it. There are a lot of embedded macros used. They worked in most case so I didn't try to fix them.

For example a lot of the subforms have delete button to delete the associated record. It's click event is tied to an embedded macro which contains all of this mash which I have no idea how to replace with VBA.

31698

I am a fair hand with Word VBA but Access at this point is a bit over my head.

Aussiebear
07-08-2024, 10:39 PM
Greg, this is Microsoft advice


Convert macros to VBA codeYou can use Access to automatically convert macros to VBA modules or class modules. You can convert macros that are attached to a form or report, whether they exist as separate objects or as embedded macros. You can also convert global macros that are not attached to a specific form or report.
Convert macros that are attached to a form or reportThis process converts to VBA any macros that are referred to by (or embedded in) a form or report (or any of its controls) and adds the VBA code to the form or report's class module. The class module becomes part of the form or report and moves with the form or report if it is moved or copied.


In the Navigation Pane, right-click the form or report, and then click Design view.

On the Form Design tab, in the Tools group, click either Convert Form's Macros To Visual Basic or Convert Report's Macros To Visual Basic.

In the Convert form macros or Convert report macros dialog box, select whether you want Access to add error handling code to the functions it generates. Also, if you have any comments in your macros, select whether you want to have them included as comments in the functions. Click Convert to continue.
If no class module exists for the form or report, Access creates one and adds a procedure to the module for each macro that was associated with the form or report. Access also changes the event properties of the form or report so that they run the new VBA procedures instead of the macros.

To view and edit the VBA code:


While the form or report is still open in Design view, if the property sheet is not already displayed, press F4 to display it.


On the Event tab of the property sheet, click in any property box that displays [Event Procedure], and then click the build button
https://support.content.office.net/en-us/media/b20be472-bfe9-4961-be17-36a8cbc0f807.gif. To view the event properties for a specific control, click the control to select it. To view the event properties for the entire form or report, select Form or Report from the drop-down list at the top of the property sheet.Access opens the Visual Basic Editor and displays the event procedure in its class module. You can scroll up or down to view any other procedures that are in the same class module.

Aussiebear
07-08-2024, 10:40 PM
Then they go on to say this...


Convert global macros

In the Navigation Pane, right-click the macro that you want to convert, and then click Design View.

On the Macro Design tab, in the Tools group, click Convert Macros To Visual Basic.

In the Convert Macro dialog box, select the options that you want, and then click Convert.
Access converts the macro and opens the Visual Basic Editor.

To view and edit the VBA code:


In the Visual Basic Editor, if the Project Explorer pane is not displayed, on the View menu, click Project Explorer.

Expand the tree under the name of the database in which you are working.

Under Modules, double-click the module Converted Macro- macro name.
The Visual Basic Editor opens the module.

Scott324
08-05-2024, 02:19 AM
VBA is not a macro so RunMacro is not appropriate. Try RunCode. VBA must be in a general module, not behind a form.

I don't use macros. If you are going to use VBA why use any macros?
Yes, in VBA (Visual Basic for Applications), if you don't want to use macros and just want to run code, you can use RunCode. To run VBA code, you need to make sure that the code is in a standard module instead of in a form.

Gasman
08-05-2024, 02:42 AM
Also be aware that the RunCode will only run Functions not Subs.

Sophian
02-24-2025, 03:29 AM
Thank you so much, I needed this information