Results 1 to 5 of 5

Thread: Problem with Recursive Click Events.

  1. #1
    Guest

    Lightbulb

    Problem with recursive click event.

    After a selection made from a combo box I want to check to see if certain conditions are met if not I want to reset the combo box to force the user to make the appropriate selection. Then problem is that when I reset the combo box it triggers the click event again and I get a repeat of the test and the message. Is there a way to turn off the event checking for the combo box while I reset it.

    Private Sub cmboTest_Click()

    If condition Then
    msgrtn = MsgBox("Incorrect selection made", vbOKOnly)
    cmboTest.ListIndex = -1
    Exit Sub
    End If

    End sub



  2. #2
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    This should work for you. In this example I put a combobox on a form with the values 1,2,3, and 4 in the list property. Of course your data and condition for checking will be different.

    In this example the condition is the number "3" on the list is the only valid choice. All other selections cause a reset. The boolean flag causes the code in the combobox click event to be skipped if the reset flag is set to true.

    Hope this helps.

    Code:
    'Put these in the General Declarations part
    Option Explicit
    'blnCboReset is a flag to indicate combo box is being reset
    Private blnCboReset As Boolean
    
    
    Private Sub Form_Load()
        'initialize combobox reset flag
        blnCboReset = False
    End Sub
    
    Private Sub Combo1_Click()
        If Not blnCboReset Then
            If Combo1.Text <> "3" Then
                MsgBox "Incorrect selection made", vbInformation & vbOKOnly, "Try Again"
                'bad selection so set flag
                blnCboReset = True
                'This triggers click event again.
                Combo1.ListIndex = -1
                'flag reset to allow next user selection.
                blnCboReset = False
                Exit Sub
            Else
                MsgBox "Good Choice", vbOkOnly, "You Did It!"
            End If
        End If
    End Sub

    JC

  3. #3
    Guest
    Thanks John

    I was thinking of something along the lines of the solution you offered but more as a last resort. I have numerous situations in where the same problem will occur. Therefore I was hoping for a more automatic solution like setting some property etc.

  4. #4
    Guest
    Instead of the

    Private blnCboReset As Boolean

    You could make it a static variable and put it in the sub, that way you cant accidently change it.
    Code:
    Private Sub Combo1_Click()
    Static blnCboReset As Boolean



  5. #5
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Excellent point, Azzmodan. The static variable is definitely the way to go on that.

    Lioness, I've often run into the same situation with comboboxes, checkboxes, etc, but have yet come across a better solution. I was hoping someone else would post a solution that magically disables the click event for a control, but it looks like we're stuck with this route for now. Hopefully, someone will prove me wrong on this.
    JC

Posting Permissions

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



Click Here to Expand Forum to Full Width