Consulting

Results 1 to 11 of 11

Thread: Form Control Calling Macro

  1. #1

    Form Control Calling Macro

    I have a bunch of form controls all calling the same macro by specifying it as the "Run Macro on Exit" property. How can the macro determine which control has called it?

  2. #2
    VBAX Regular
    Joined
    Jul 2009
    Location
    Dorset, UK
    Posts
    14
    Location
    Add a parameter to your macro that holds the control or the control's name (depending on what you want to use it for). Then in each place that you call the macro you need to add the parameter that identifies the control.

    If you want to be able to call it when you don't care where it's from then make the parameter optional.

    To help more specifically I'd need to know why you need to know where the macro is called from, what are you going to do with this information?
    K

    Software Matters - VBA Programmers - Straightforward Solutions To Take Care Of Your Business

  3. #3
    Quote Originally Posted by Kafrin
    To help more specifically I'd need to know why you need to know where the macro is called from, what are you going to do with this information?
    Thanks. Unfortunately the "run Macro on Entry/Exit" dropdowns on the control properties doesn't let me select any macro's which have parameters. What I have is a table with many rows (y) and columns (x). The top row (of any pair of rows) contains the drop downs and when they are filled in by a user I need to color the cell immediately below the cell with the dropdown (i.e. y+1,x ). Currently I'm just scanning all the controls on the form every time the macro is called on exit from the field - which seems a little inefficient. What I really want to pass to the sub is a reference to self, then, via the name property, I can immediately determine which control it is and which cell should be colored.

  4. #4
    VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    If you can't select a macro with a parameter, what about creating a new macro. Your new macro would simply call the first macro and set the parameter, and your control would run the new macro on entry/exit.

    [vba]
    Sub MyNewMacro
    MyRealMacro "ControlName"
    End Sub
    [/vba]

    Clunky I know, but it will work.

  5. #5
    Quote Originally Posted by geekgirlau
    If you can't select a macro with a parameter, what about creating a new macro. Your new macro would simply call the first macro and set the parameter, and your control would run the new macro on entry/exit.

    [vba]
    Sub MyNewMacro
    MyRealMacro "ControlName"
    End Sub
    [/vba]
    Clunky I know, but it will work.
    That would work, requires a macro for each dropdown and there will be ~100 dropdowns. Still, would be quicker than looping through each control. Thanks.

  6. #6
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi pcstru,

    Another way is to use an 'On Entry' macro attached to each of the formfields in question, coded along the lines of:
    Option Explicit
    Public FFName As String
     
    Sub GetFFName()
    FFName = Selection.FormFields(1).Name
    End Sub
    Your 'On Exit' macro can then retrieve the value of the 'FFName' variable. If you've got a lot of formfields to make this modification to, you could use a one-off piece of code to modify each formfield's properties ...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You may get unexpected results from that.

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You may get unexpected results from that.

  9. #9
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Gerry,

    I've never had any unexpected results from the approach I suggested. The only thing one needs to watch out for is that, if one of the fields concerned is the default when the document is opened, you need to use a Document_Open macro to ensure the FFName variable gets set.

    Perhaps you could explain your response?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    I don't know about getting unexpected results, but I'm slightly allergic to public variables - seen too much poorly written code with hundreds of them, making it very difficult to trace values.

  11. #11
    Quote Originally Posted by macropod
    Hi pcstru,

    Another way is to use an 'On Entry' macro attached to each of the formfields in question, coded along the lines of:
    Option Explicit
    Public FFName As String
     
    Sub GetFFName()
    FFName = Selection.FormFields(1).Name
    End Sub
    Your 'On Exit' macro can then retrieve the value of the 'FFName' variable. If you've got a lot of formfields to make this modification to, you could use a one-off piece of code to modify each formfield's properties ...
    Thankyou! You have effectively answered the question I was asking - the "selection" reference is effectively "self" if used with care. I'll watch out for the first control being the default selection - since there should only be the dropdowns active (and calling the proc), then if the global is "" it must be the first control. Thanks again.

Posting Permissions

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