Matteo NNZ
Matteo NNZ

Reputation: 12655

Stopping "TextBox_Change" event from executing if called by code

I have a userform which contains a TextBox object named myTextBox.

The text inside the object can be changed either by the user or by the code. There's a "onChange" procedures attached to the textbox:

Private Sub myTextBox_Change()
    'do some stuffs
End Sub

I would like the event to be processed only when is the user changing the text, but not the code. I had thought about adding an Optional parameter like the following:

Private Sub myTextBox_Change(Optional isCode As Boolean)
    If isCode = False Then
        'do some stuffs
    End If
End Sub

but this is not really helpful because I cannot pass the isCode = True when changing the name programmatically, like this:

myForm.myTextBox = "new text"

Does anyone have an idea on how I can fix this?

Upvotes: 0

Views: 203

Answers (1)

GavinP
GavinP

Reputation: 687

One way (perhaps not the right way?) would be to declare a global variable to use in the same way as your IsCode Boolean. Set it to true during your code block so the textbox_change event knows code is processing in the background, and then set back to false when that code has finished.

Put this at the top of a normal module (won't work in a form module)

Option Compare Database
Option Explicit
Global IsCode As Boolean

and then in the code you're running just set IsCode=true as required.

Upvotes: 1

Related Questions