Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Can't disable events in UserForm

  Asked By: Lucina    Date: Feb 08    Category: MS Office    Views: 1942
  

Short version:

I have a UserForm that is being obstinate. Need some hints.

1- UserForm has text boxes.
2- Initializing the text boxes causes the change Event to fire.

Application.EnableEvents = False ' refuses to work.

Even if the text boxes are assigned values *Before* the Form is
shown... and/or if the Form is Disabled before showing.
(UserForm.Enabled = False)

How I assign a value from a sheet named cell:
UserForm.TextBoxName = Sheet4.Range("Cell_Name")


Longer:
I have text boxes in a user form. When I assign values to them, the
respective change event fires.

1- If I assign values BEFORE showing the UserForm
The change event fires after the form shows...Even if I disable
events or the form.

2- If I do it after showing it.
The change event fires (in the Initialize Sub) as soon as the
value is assigned. Even if the events are disabled right before the
assignment.

... Out of ideas ... Probably missing something obvious...

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Muhammad Evans     Answered On: Feb 08

I'm assuming you read the recent discussion between David and I
after your kind suggestion. Taking up logic . . . when
you step through it line by line what code fires the change  event,
after you'd shut it off (like I did in my recent struggle with
something similar).

Hey, one thing for sure, we're all in this together . .

 
Answer #2    Answered By: Kian Evans     Answered On: Feb 08

Several things.

1- I solved my event  problem by simply deleting the
UserForm_Initialize() Sub. Gone. The Sub and the problem. Having
this Sub appears to make events  within the form  "manditory"...
whatever....

RE:
> I'm assuming you read the recent discussion between David and I
> after your kind suggestion.

I tried, but your code was complex & I am lazy. If my comment helped,
great.


RE:
> when
> you step through it line by line what code fires the change  event...

It was the line that changed the list box value (as it should be).
OR, if I changed the value B4 Showing the form, it waited until after
the Initialize Sub exited...sort of an "EventSuspend", as near as I
can tell.

BTW I find that I rarely single step my code. I sprinkle carefully
configured Debug.Prints *very* liberally. Sometimes all the clutter
in the Immediate window makes it hard to figure out a problem and I
have to cull those in the code that's been debugged.

 
Answer #3    Answered By: Tomas Thompson     Answered On: Feb 08

one thing this has taught me is to not to assume too much about the
userform_intialize sub level. I do need to do some checking in on
that. But, I'm glad you got it working . .

 
Answer #4    Answered By: Madeeha Malik     Answered On: Feb 08

Basically this was a problem solved long ago. event  disabling in a userform
module does not work. So we have to manually create an event type variable to
control flow in the userform  initialize method.

Heres how you go about doing it

declare a variable global as boolean

Dim userEvent as boolean

Private Sub UserForm_Initialize()
userEvent = False
End Sub

Private Sub TextBox1_Change()

If userEvent = False Then
.........Your Code here......
End if
End Sub


Your second question also came at a good time as I had the same problem
yesterday. Managed to solve it.

TextBox1.Text = sheets(4).Range("Cell_Name").Cells(1, 1).Value

 
Answer #5    Answered By: Aaminah Khan     Answered On: Feb 08

I'm not sure what 'second question' you refer to:

--- neville wrote:
> ... Your second question also came at a good time as I had the
same problem yesterday. Managed to solve it.
>
> TextBox1.Text = sheets(4).Range("Cell_Name").Cells(1, 1).Value


If you mean this line:

> > How I assign  a value from a sheet  named cell:
> > UserForm.TextBoxName = Sheet4.Range("Cell_Name")


It was an example of how I assign the value, not a question.

 
Answer #6    Answered By: Anne Powell     Answered On: Feb 08

So, I think this says that changing the value of a UserForm text
box *after* exiting the Init Sub (but while the Form is still active)
will NOT fire  the text  box change  event (if events  are disabled),
right? This could be.

I did not try changing the values  after *exiting* the initialize
Sub. ..Because... It seemed to me that the initialize  Sub would be
the logical place to 'initialize' things in the UserForm. Duh!
Allowing change events here seems dumb to me because I *know* that
I am changing them and therefore, don't need an event  to detect
changes...whatever...

Deleting the Initialize Sub *altogether* solved my problem. I now
load the TextBox values before Showing the UserForm; the kind of
initialization I was after.

 
Didn't find what you were looking for? Find more on Can't disable events in UserForm Or get search suggestion and latest updates.




Tagged: