Combination of "Key Down" & "Not in List" events produce error 7777

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doma23
    New Member
    • May 2010
    • 107

    Combination of "Key Down" & "Not in List" events produce error 7777

    I have two comboboxes, cmbClient and cmbCountry.
    Both combos have On Key Down events set, so the user can easily go trough the combo values.

    Combo Client has "After Update" and "On Key Down" event set, and it works fine.
    I used After Update event to take care manually (via code) the items that are not in the list.

    On combo Country, however, there was no need for that and I've used "On Not in List" event instead. And that was working fine until I've added the "On Key Down" event.
    Now as soon as I press the down cursor, it activates "Not on List" event and it gives me the message that the item is not on the list, although it obviously is.
    After I press OK button, I get additional error "Run-time error 7777: You've used the Listindex property incorrectly."
    When I go to debug, the next line is higlihted:
    "Me.cmbCountry. ListIndex = Me.cmbCountry.L istIndex + 1"

    Here is the full code of the KeyDown event:
    Code:
    Private Sub cmbCountry_KeyDown(KeyCode As Integer, Shift As Integer)
    
        Select Case KeyCode
            Case vbKeyDown
                KeyCode = 0
                If Me.cmbCountry.ListIndex <> Me.cmbCountry.ListCount - 1 Then
                    Me.cmbCountry.ListIndex = Me.cmbCountry.ListIndex + 1
                End If
            Case vbKeyUp
                KeyCode = 0
                If Me.cmbCountry.ListIndex > 0 Then
                    Me.cmbCountry.ListIndex = Me.cmbCountry.ListIndex - 1
                Else
                    Me.cmbCountry.ListIndex = 0
                End If
        End Select
    
    End Sub
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    I dont get what exactly it is you are trying to do with the keyDown event? Why do you use that, is the normal access filtering/selecting not working good enough for you?

    Comment

    • doma23
      New Member
      • May 2010
      • 107

      #3
      I have comboboxes on the form and I want to give the users of my tool the possiblity to scroll down and up through the values of the comboxes only with the cursor keys, without the use of mouse.
      The feature is very useful.

      Anyway, I've solved the problem, by adjusting the code:
      Code:
          Select Case KeyCode
              Case vbKeyDown
                  KeyCode = 0
                  If Me.cmbCategory.ListIndex <> Me.cmbCategory.ListCount - 1 Then
                      Me.cmbCategory.Value = Me.cmbCategory.ItemData(Me.cmbCategory.ListIndex + 1)
                  End If
              Case vbKeyUp
                  KeyCode = 0
                  If Me.cmbCategory.ListIndex > 0 Then
                      Me.cmbCategory.Value = Me.cmbCategory.ItemData(Me.cmbCategory.ListIndex - 1)
                  Else
                      Me.cmbCategory.Value = Me.cmbCategory.ItemData(Me.cmbCategory.ListIndex)
                  End If
              Case vbKeyBack  'backspace puts the value to null, otherwise the previous value would remain
                  KeyCode = 0
                  Me.cmbCategory = Null
          End Select

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        I still dont see why you need custom code for that. Isn't that standard built-in access behavior? Maybe I am just tired and missing something.

        Comment

        • doma23
          New Member
          • May 2010
          • 107

          #5
          Not in Access 2002 is not. ;)

          Comment

          Working...