Consulting

Results 1 to 11 of 11

Thread: Stop Userform Events

  1. #1
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location

    Stop Userform Events

    Hey folks,

    I'm loading up a userform and in the Userform_Initialize event, I am setting all the defaults for the checkboxes. Unfortunately, they all have change events associated with them.

    I cannot get the change events to stop firing as I load the defaults. Application.enableevents = false doesn't work for me. Is there another object /event that I'm missing here?

    I can code round it be setting a temporary Boolean such as UpdateCB's = True/False, but this seems pretty hokey...

    Any ideas?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  2. #2
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    I feel your pain. I'll be interested if someone has a clean solution to this.

    I've been using a counter for this purpose. All my _Change() code comes enclosed in a block like:
    If canUpdate = 0 Then
    ...
    ...
    End If
    Everytime I enter code where I want to disable _Change() code, I increment canUpdate, and then I decrement it on the way out. Hokey? Very, but at least it works.

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hmmm... I was afraid of that. Hopefully someone will have a better way, but in the mean time, I'll give this a go.

    Thanks!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    VBAX Newbie chitosunday's Avatar
    Joined
    Nov 2004
    Location
    Philippines
    Posts
    3
    Location
    Try the click event. It's much tamer than the change event

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Ken,

    This is a common problem, and I use a variable associated with the event to control it, something like


    Private mEnableEvents As Boolean
    
    Private Sub UserForm_Initialize()
        With Me
            .CheckBox1.Value = True
            .CheckBox2.Value = False
            'etc.
        End With
    mEnableEvents = True
    End Sub
    
    Private Sub CheckBox1_Click()
        If mEnableEvents Then
            mEnableEvents = False
    MsgBox "1 Changed"
    mEnableEvents = True
        End If
    End Sub
    
    Private Sub CheckBox2_Click()
        If mEnableEvents Then
            mEnableEvents = False
    MsgBox "2 Changed"
    mEnableEvents = True
        End If
    End Sub

    Probably not what you want, but unavoidable I feel.

    .
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Ken,
    Can you not set the default value True/False in the Checkbox Value property, then there is no change occurring.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    I also use a boolean and wrap all the form events in If... Then's
    I believe it's the best way to handle it
    K :-)

  8. #8
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey all,

    Thanks for the input. As I said, I was afraid that would be the way, but was hoping that I'd just missed something. It does seem odd though that an Application level command like EnableEvents wouldn't extend to Userforms.

    No biggie, and I can code that in.

    Chitosunday, I misspoke, they are actually Click events. A click is a good as a change with textboxes though, and my brain was wearing down by the time I posted.

    Malcolm, not in this case. I have a (another) userform which captures/changes specific user preferences, and writes them to the registry using getsetting/savesetting. I retrieve those in the Userform startup to set the checkboxes correctly.

    Thanks again to all of you. I'm going to mark this one solved.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by kpuls
    It does seem odd though that an Application level command like EnableEvents wouldn't extend to Userforms.
    Not so odd, a Userform is not part of the Excel object, it is part of MSForms.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    FWIW, I switched to the counter method because the boolean switch was getting too complicated to maintain on some of my larger forms. For example a piece of code that might trigger an event might also call another piece of code that might also trigger an event. That second piece of code might have more than one entry route, and so keeping track of the desired boolean status was just too complex.

    The counter is more a fire-and-forget solution. No matter how many iterations and nested Calls get involved, each Sub will stack the counter up, each End Sub takes it down. When it gets back to zero again, events are re-enabled.

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by xld
    Not so odd, a Userform is not part of the Excel object, it is part of MSForms.
    Okay, fair enough. In that case I think I'll say it's strange that there is not MSFroms.EnableEvents = False command available!

    Quote Originally Posted by BlueCactus
    The counter is more a fire-and-forget solution. No matter how many iterations and nested Calls get involved, each Sub will stack the counter up, each End Sub takes it down. When it gets back to zero again, events are re-enabled.
    Interesting! Mine is pretty simple, I just need to turn them off while the Userform_Initialize event takes place, so a boolean would work. I can certainly see how your method would be superior in a more complex setting though. Nice strategy!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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