Share via

Copy and Paste Form objects to another form

Anonymous
2017-06-04T14:05:26+00:00

I'm trying to copy objects from one Access form to another.  I am able to copy the objects but it doesn't copy the event procedures for the objects.  Is there any way to get this to work?  If not, please compel the smart programmers at Microsoft to fix this if possible.  Seems like it would be an easy fix for them.

Thanks!!

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-06-04T16:44:39+00:00

If you wish to use VBA code for a control's event property and have it copy with the control, then rather than using an event procedure, wrap the code in a function declared as Public in a standard module.  Then set the event property of the control in its properties sheet to the function:

=NameOfFunctionGoesHere()

When you copy and paste the control into another form or report, the event property is copied with it, and the same code in the function will be executed when the event is triggered in either object.  If you wish to vary the code on the basis of which object calls it, then you can pass the object's name as an argument into the function, e.g. in the case of a form:

=NameOfFunctionGoesHere(Form.Name)

and reference the argument in a Select Case statement in the function's code.

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-06-04T23:16:09+00:00

    > fix this if possible

    That won't happen, and for good reason. While it seems convenient for you, it is inconvenient for those who just want to copy a smartly-designed button to another form, and would hate to have the click event come across as well.

    And as has been pointed out, if you really want it, that behavior is available. In such cases where I have a button that needs to run the exact same procedure from several forms, I have a public function in a standard module (I call mine [app_abbreviation]_BusinessLogic) and I set the function call in the Click event. The function call may pass in [Form], so in the function I know which form has invoked me (Screen.ActiveForm and Screen.ActiveControl can also often be used for this).

    > fix this if possible

    That won't happen, and for good reason. While it seems convenient for you, it is inconvenient for those who just want to copy a smartly-designed button to another form, and would hate to have the click event come across as well.

    And as has been pointed out, if you really want it, that behavior is available. In such cases where I have a button that needs to run the exact same procedure from several forms, I have a public function in a standard module (I call mine [app_abbreviation]_BusinessLogic) and I set the function call in the Click event. The function call may pass in [Form], so in the function I know which form has invoked me (Screen.ActiveForm and Screen.ActiveControl can also often be used for this).

    While I have the greatest respect for Tom's expertise, I must respectfully disagree with his reason why it shouldn't be ''fixed''.  I doubt most people's buttons are so ''smartly-designed'' that they would go out of their way to copy them without the code.  I could be completely wrong, but I bet if you polled 1,000 ordinary users (that doesn't include you Tom, you're way too smart) , 950 of them would prefer to copy the code with the button.  You're probably going to do something similar with the new button so you can delete the old code when you go in to enter the new.  

    Either way, thank you all very much for taking the time to answer my question!!  I've never posted a question to the community before, so I sincerely appreciate the thoughtful responses.

    Was this answer helpful?

    0 comments No comments
  2. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2017-06-04T17:18:51+00:00

    > fix this if possible

    That won't happen, and for good reason. While it seems convenient for you, it is inconvenient for those who just want to copy a smartly-designed button to another form, and would hate to have the click event come across as well.

    And as has been pointed out, if you really want it, that behavior is available. In such cases where I have a button that needs to run the exact same procedure from several forms, I have a public function in a standard module (I call mine [app_abbreviation]_BusinessLogic) and I set the function call in the Click event. The function call may pass in [Form], so in the function I know which form has invoked me (Screen.ActiveForm and Screen.ActiveControl can also often be used for this).

    Was this answer helpful?

    0 comments No comments
  3. ScottGem 68,830 Reputation points Volunteer Moderator
    2017-06-04T15:01:30+00:00

    Nope, the Event Procedures don't copy over. They have to copied separately. I'm not sure why this is the case but its been that way forever. However, embedded macros do copy.

    I'm not sure how easy this is to do. But if you want the feature post the suggestion at access.uservoice.com

    Was this answer helpful?

    0 comments No comments