Consulting

Results 1 to 7 of 7

Thread: How to use Embedded Macro - RunMacro feature

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,400
    Location

    How to use Embedded Macro - RunMacro feature

    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.
    Last edited by Aussiebear; 07-08-2024 at 02:56 PM.
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    372
    Location
    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?
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,400
    Location
    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.

    Embedded Macro.jpg

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

    Visit my website: http://gregmaxey.com

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,236
    Location
    Greg, this is Microsoft advice
    Convert macros to VBA code

    You 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 report

    This 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.

    1. In the Navigation Pane, right-click the form or report, and then click Design view.
    2. 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.
    3. 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.
    4. To view and edit the VBA code:

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

      2. On the Event tab of the property sheet, click in any property box that displays [Event Procedure], and then click the build button
        . 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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,236
    Location
    Then they go on to say this...
    Convert global macros

    1. In the Navigation Pane, right-click the macro that you want to convert, and then click Design View.
    2. On the Macro Design tab, in the Tools group, click Convert Macros To Visual Basic.
    3. 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.
    4. To view and edit the VBA code:

      1. In the Visual Basic Editor, if the Project Explorer pane is not displayed, on the View menu, click Project Explorer.
      2. Expand the tree under the name of the database in which you are working.
      3. Under Modules, double-click the module Converted Macro- macro name.
        The Visual Basic Editor opens the module.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Banned VBAX Newbie
    Joined
    Aug 2024
    Posts
    1
    Location
    Quote Originally Posted by June7 View Post
    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.
    Last edited by Aussiebear; 08-05-2024 at 02:38 AM.

  7. #7
    VBAX Contributor
    Joined
    Nov 2020
    Location
    Swansea,South Wales,UK
    Posts
    113
    Location
    Also be aware that the RunCode will only run Functions not Subs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •