Results 1 to 13 of 13
  1. #1
    sgerber
    Guest

    Change event bafflement

    Oh, well -- so much for the euphoria.

    Here's one that's got me seriously confused.

    Say I have a userform with code that (for simplicity's sake) looks something like this:

    Private Sub Userform_Initialize()
    ComboBox1.AddItem "Voosh", 0
    ComboBox1.AddItem "Zing", 1
    ComboBox1.AddItem "Wakoom", 2
    ComboBox1.AddItem "Blurf", 3
    ComboBox1.AddItem "Smundge", 4
    ComboBox1.ListIndex = 0
    End Sub

    Private Sub cmdOK_Click()
    End
    End Sub

    Private Sub ComboBox1_Change()
    Msgbox "You changed!"
    End Sub

    How do I keep the change event from firing when the combo box is first populated?!?

    Am I using the wrong type of event, or what?

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    Steve,

    I'd be trying something like this:

    <pre>dim blnLoaded as boolean ' Defined at the start, has scope throughout the form

    Private Sub Userform_Initialize()
    blnLoaded = False
    ComboBox1.AddItem "Voosh", 0
    ComboBox1.AddItem "Zing", 1
    ComboBox1.AddItem "Wakoom", 2
    ComboBox1.AddItem "Blurf", 3
    ComboBox1.AddItem "Smundge", 4
    ComboBox1.ListIndex = 0
    blnLoaded = true
    End Sub

    Private Sub cmdOK_Click()
    End
    End Sub

    Private Sub ComboBox1_Change()
    if blnLoaded then
    Msgbox "You changed!"
    end if
    End Sub
    </pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    > the change event from firing when the combo box is first populated

    You can't.

    The Change Event "belongs" to VBA. It happens every time something changes. What you DO with the Change Event is another matter. A pedantic point, I know, but it's gotta be significant.

    Do you really want to use the Change Event? Is there some action you have to do every time that box changes? Or do you need to take action only when the user exits the box? If the latter, try using the Exit event.

    If you are in your VBE code (go ahead, I can wait .....) click anywhere in your Sub ComboBox1_Change code, then go up near the top of the VBE pane and you'll see two drop-down lists. One holds "ComboBox1" and the other holds "Change" (ComboBox1_Change, right?!!!!!). Choose "Exit" from the RH box and you'll be presented with a shell that looks like this:
    <pre> Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    End Sub
    </pre>



    Knock, as they say, yourself out!



    As an aside: How many VBA programmers are using the Chnage event, and why? The only reason I can think of is wanting to POUNCE on a user the INSTANT he/she/it has made a keying mistake.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Change event bafflement

    A better alternative in Access is the BeforeUpdate, since it only occurs if a value is selected in the combobox and before the exit event. In VB, the Validate event serves the same purpose as BeforeUpdate.
    Charlotte

  5. #5
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    <hr>How many VBA programmers are using the Chnage event, and why?<hr>
    I use it. It's very useful. I can use, for instance, it to not echo alpha keypresses in a numeric entry; to translate everything to uppercase; or even to do a lookup when more than 3 characters are entered in one box, to populate a listbox elsewhere.

    Some very nice uses.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Change event bafflement

    The Change event is useful when you need to validate, or respond to, an entry, after every keystroke.
    The best illustration of the use for this that I can think of:

    In Word, create a blank new document (containing no bookmarks).
    Show the Bookmark dialog (Insert>Bookmarks).
    The Add, Delete and GoTo button are disabled.

    As soon as you start typing a name in the Bookmark Name textbox, the Add button becomes enabled. But if you type an invalid character, the Add button becomes disabled.

    The Delete and GoTo buttons remain disabled unless the name in the Bookmark Name textbox happens to exactly match the name of an existing bookmark.

    All that button enabling/disabling is as a result of validation functions that run every time there is any change in the state of the Bookmark Name textbox. So if you wanted to create like functionality w/VBA, the Change event would be it.

    (By the way, I've written my own replacement dialog for the Bookmarks dialog - the main advantage is that it's modeless, but it has some other really useful features that are not in the one Word provides - I'll post it here soon.)

    PS: Just thought of another built-in 'Change event': create a new userform - click in the Caption item in the properties window, delete the default userform name, and start typing a new one. With every keystroke you type, the character you type also appears in the title of the userform - courtesy of a change event that's triggered by typing in the caption property textbox.

    Gary

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    The little Greaves bows to the two big Gs (grin!)


    OK guys. Good uses.

    I guess I don't spend a lot of time doing the keystroke things through a GUI. I tend to wait for a whole string and then go see if the file exists.

  8. #8
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    For the first 2 examples, you might be better off using the KeyPress event. Advantage: It processes the keystroke BEFORE it is added to the control's text value. For example, to uppercase, you could put:

    KeyAscii = Asc(UCase$(Char$(KeyAscii)))

    Or, to filter out non-numeric characters, you could put:

    If KeyAscii < 48 Or KeyAscii > 57 Then
    Beep
    KeyAscii = 0
    End If

    (48 and 57 being the ascii values of 0 and 9)

    Setting KeyAscii to 0 has the effect of cancelling the keystroke.

  9. #9
    kelliel
    Guest

    Re: Change event bafflement

    <hr>Chris said: How many VBA programmers are using the Chnage event, and why? <hr>

    It is one of my most favorite events!

    Example. I gather a lot of data from the user to create specific, but customized, documents. I use the MultiPage control along with real-time VB form manipulation. Most of my option button and check box controls are used to make different labels, text boxes, other option buttons, and other check boxes enabled/visible or disabled/unvisible based on what is chosen. The Change Event (along with AfterUpdate) is my most used event to control the customization of the VB form.

    I especially like it when I have a yes/no option button pair. If that choice makes visible a label, for example, I just place the following code in only one of the option button's change event.

    <pre> label1.visible = optionbutton1.value</pre>


    Slicker than slick. That one line of code works just like a light switch.

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    > using the Chnage event,


    OK. It's VERY GOOD at picking up user errors during data-entry. I got that (grin!)



    Right now I feel that that's Change Events best use - interacting with a keyboard user. Most of my development centres around grabbing a few parameters and then turning the machine loose to process zillions of files. I suspect that's why i hardly need to use the Change event. There. Got it!

  11. #11
    kelliel
    Guest

    Re: Change event bafflement

    Absolutely. If all you are doing is getting a couple of parameters and "turning the machine loose to process zillions of files," then the Change Event may not be something you use much. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  12. #12
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    This sounds like an old mainframe programmer at work <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change event bafflement

    I'm not old.





    I'm <small> <small> <small> <small> <small><small> <small> <small> <small> <small> <small>chronologically </small></small></small></small></small></small> </small></small></small></small></small>-challenged</small>.






    I was at a meeting last night - I'm to be the "architect" for the world's latest Web killer-ap (God help us all). Me and six young people. Everyone rushing off in all directions about how we can write this and add that and do the other.

    My mainframe days were spending YEARS developing stuff, so now I sit and ask myself "how much of this coding can we throw out before we start it?".

    If I get the job and do my job well, there'll be very little actual work to do.


    That will leave me with more time to post incredibly-wide sets of declarations ....

Posting Permissions

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