Results 1 to 5 of 5

Thread: Replace OnClick Macro with VBA Code [solved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29

    Resolved Replace OnClick Macro with VBA Code [solved]

    Hello there!

    I have the following problem:
    I create a form with VBA (because the fields in the form are different each time the form is opened).

    Also I create a control (Combobox), in which the user can select a socalled Template. After selecting a value in the combobox some fields in the form shall be filled (=execute a codesequence after raising the OnClick event).

    At the moment i use this:

    VB Code:
    1. ctrl.Properties("Name") = "ComboSelectTemplate"
    2. ctrl.Properties("OnClick") = "MacroComboSelectTemplate.OnClick"
    3. ctrl.Properties("RowSourceType") = "Table/Query"
    4. ctrl.Properties("RowSource") = "SELECT [" & Templates & "].[TemplateName] FROM [" & Templates & "]"

    I have created a macro "MacroComboSelectTemplate" that then executes a VBA procedure.

    Now I want to remove the macro-workaround and replace it by VBA code.

    Anyone has an idea?
    Last edited by Clywd; Feb 11th, 2005 at 10:46 AM. Reason: [Solved]

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Replace OnClick Macro with VBA Code

    If you go to the control in design view and click into the OnClick property, you can select to either use the
    custom code builder, expression builder or assign a macro to it. I think that through code you can do it by setting its
    property to "[Event Procedure]". I know if you do it in design view it takes you to a module of a procedure with the
    same control name _Click, but I am unsure how it links the two if the name is different from the control name.

    Any possibility of just doing it through the design window?

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29

    Re: Replace OnClick Macro with VBA Code

    Thanks for your ideas!
    As I create the form completely in vba code there is no design view.
    Also setting the OnClick event to [Event Procedure] unfortunately does not work, as Access then tries to run the macro "Event Procedure"

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    29

    Re: Replace OnClick Macro with VBA Code

    Solved it on my own; there is a function for creating procedures in modules. So I needed first to get the form's module:

    VB Code:
    1. dim mdl as module
    2. dim result as integer
    3.  
    4. set mdl = frm.module
    5. result = mdl.createEventProc("Click", ctrl.Name)
    6. mdl.InsertLines result + 1, vbTab & "Msgbox ""My Code here."""

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Replace OnClick Macro with VBA Code [solved]

    Sorry, my bad. I guess it didnt register with me on how you needed it done.

    Checkout my code in CodeBank on Modifying Macro Code from VB.

    Later
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width