2

Is it possible to pass a variable into a button_click procedure?

Background:

I have 2 procedures, both of which perform some actions, and then both open Form_1. Now, dependant on which procedure opened Form_1, I want the command button to perform a different task with the data on the form. Ordinarily, I would pass a variable from the first procedure into the procedure being called.

However, due to it being a form being called, which requires the user to click the command button to initiate the second procedure, I do not think it's possible to pass a variable to this procedure?

For now I assign a public variable in procedure_1: override = False and procedure_2: override = True, but I know it's generally frowned upon to use public variables (from what I have read).

Within the button_click procedure I then use this variable to route the code accordingly.

Private Sub CommandButton1_Click() If override Then Call override_data Else Call submit_data End If End Sub 

Am I okay, in this scenario, to use a public variable? or is there a better way to achieve my target?

8
  • "Depending on which procedure opened form_1" - does this mean there are two different buttons that can be clicked to open the form? Or that the form is opened first (one of two ways) and you want the button to know that? Commented Jun 25, 2015 at 11:47
  • Correct with the first assumption; there are 2 different buttons which can be clicked on a userform, which then run one of the procedures which leads to form_1 opening. Commented Jun 25, 2015 at 11:50
  • Why not assign each procedure to a different button? Maybe I'm confused. Commented Jun 25, 2015 at 11:53
  • 2
    It is OK to use a Public variable - I'd put it in the form and set it before showing the form - though a Property procedure in the form would be better practice. Commented Jun 25, 2015 at 11:54
  • 1
    Look at Jon Peltier's explanation to learn how to set and read user form properties. This should solve your problem and avoid a public variable. Commented Jun 26, 2015 at 14:28

1 Answer 1

1

I ended up reading Jon Peltier's article on user form properties, as suggested in the comments by @PeterT.

This enabled me to use a user form property instead of a public variable.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.