Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why does Access fire KeyDown event when combobox list is being displayed?

Posted on 2003-11-09
8
Medium Priority
?
475 Views
Last Modified: 2012-06-27
I'm running into a problem with tabbing.  I have a form which is like
a spreadsheet, and I've implemented Excel-like navigation by catching
the KeyDown event and if it is a vbKeyDown or vbKeyUp (and no shift or
Alt key is pressed) then I move to the next or previous row.
This works fine.

The problem is that my combo boxes no longer work after adding the
code to catch vbKeyDown.

The problem is this: When I am on a combo box and ALT vbKeyDown is
pressed, the MS-Access displays the combobox list.  If I try to use
the up or down arrows to navigate within this combobox list, it
instead moves to the previous or next rows.  This happens because
Access is firing the keydown event when the combobox list is being
displayed.

IMHO, this is a bug.  Access should not fire those events when the
combo-box is being displayed.

(Q) Is there a way to prevent these from being triggered?

(Q) Or is there a way to know when the combo box is being displayed so
    I can detect this condition and not move focus to the next or
    previous row?
0
Comment
Question by:steranka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 32

Accepted Solution

by:
jadedata earned 1050 total points
ID: 9711038
Hey steranka!

 This is not a bug, Access is doing exactly what you told it to do when you programmed to a KeyDown event that is used for other actions under normal circumstances.  You are trying to make Access behave like Excel (it doesn't like that....)

 Is the KeyPreview property of the form set to true(yes)
 This property tells Access that the Form_KeyDown get first crack at the KeyPress
 Otherwise the individual form CONTROL has to handle a KeyDown.
 But, with this comes the disadvantage that your combobox reacts to the keydown the same as ALL other controls on the form

 If you want to "exempt" your combobox from this behavior then you will have to turn OFF the Form_KeyPreview property and code each control to react to the KeyDown. (lots of coding.....) or you can put a specific trap in the KeyDown event that bypasses the programmed behaviour if the activecontrol is any combobox.  Although I don't think this will get you exactly where you want to be.

 Why don't you just let Access act naturally, instead of sending to a behavior modification school??



regards
Jack
0
 

Author Comment

by:steranka
ID: 9722167
LOL.  I have a form and subform.  The question I'm having is within the subform if that matters.
Form.KeyPreview is set to No for both the form and sub-form.

I'm laughing because of your comment.  I thought it would be nice to let the user navigate
through the spreadsheet-like data using natural up/down arrows.  That is why I'm trying
to get it to work.  I _don't_ want to send Access to behavior modification and if that's what
it'll take then I'll back off and tell the customer, sorry it's too complicated and will costs
you too much to implement.

I'm just noticing another problem/behavior that may or may not be related.
In some cases (I don't know what causes this), the 1st character of the combo-box is
lost (eaten).  I'm sharing this becuse it may be relavent.

From your comments it seemed like the solution you were suggesting is to turn off form preview.
Since I don't have that on, I'm not sure what to try next.

Thanks,
steranka
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9722485
For the "key gobbling combo's" - Is there any conflicting code in operations like a OnChange or KeyPress/Down/Up events or is the Auto-Correct feature in access turned on.

Access has it's own set of nav keys and features.  Remember its a Database not a Spreadsheet.  The two were never intended to be similar in function.  Their common ground is that they present information.

You could build buttons on your forms that have "hot-Key" associations...
  Turn ON Form KeyPreview
  This Button caption "&Next"  make the "Alt-N" the hotkey for a button that would move to the next record in a recordset.
  This Button caption "&Previous"  make the "Alt-P" the hotkey for a button that would move to the next record in a recordset.

Make sure the hotkey you assigne does not conflict with others that Windows and Access already use.


0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:steranka
ID: 9737374
Using Alt-N is a work around not a solution, right?  If the answer is "sorry, there is no solution." Access just doesn't support what your trying to do, then just say that.  I think you were saying that it could be done but it would require a lot of coding.  I just don't understand what "code needs to be written".  What is the algorithm for making this work?

Right now I have code like this (see below), so the code is already written to do the navigation.
What I didn't understand was your comment on how to disable this while the combo-box "dropdown list" was open.

Is there a way to do that?  I think I could easly turn on Form.KeyPreview and call my MoveUpOrDown method in Form.KeyDown.  Is this what you were saying is the way to do this?
Sorry I'm not following.

A sample of the code I have for dowing this is shown below:

Private Sub GarmCode_KeyDown(KeyCode As Integer, Shift As Integer)
    MoveUpOrDown KeyCode, Shift
End Sub

Private Sub GS1PRICE_KeyDown(KeyCode As Integer, Shift As Integer)
    MoveUpOrDown KeyCode, Shift
End Sub

Private Sub HandleKeyDownArrow(KeyCode As Integer, ToControl As Control, MoveToNextRow As Boolean)
    On Error GoTo Local_Exit:
    If KeyCode = vbKeyDown Then
        If MoveToNextRow Then
            Me.Recordset.MoveNext
        End If
        ToControl.SetFocus
    End If
Local_Exit:
End Sub

Private Sub HandleKeyUpArrow(KeyCode As Integer, ToControl As Control, MoveToPreviousRow As Boolean)
    On Error GoTo Local_Exit:
    If KeyCode = vbKeyUp Then
        If Not Me.Recordset.BOF And MoveToPreviousRow Then
            Me.Recordset.MovePrevious
        End If
        ToControl.SetFocus
    End If
Local_Exit:
End Sub

Private Sub MoveUpOrDown(KeyCode As Integer, Shift As Integer)
    Dim isKeyUp As Boolean
    Dim isKeyDown As Boolean
    Dim curControlName As String
    Dim ShiftDown As Boolean
    Dim AltDown As Boolean
   
    ShiftDown = (Shift And 1) > 0
    AltDown = (Shift And 4) > 0
   
    If KeyCode = vbKeyUp And Not ShiftDown And Not AltDown Then
        isKeyUp = True
    ElseIf KeyCode = vbKeyDown And Not ShiftDown And Not AltDown Then
        isKeyDown = True
    End If
   
    If Not isKeyUp And Not isKeyDown Then
        ' ignore this key
    Else
        curControlName = ActiveControl.Name
        '======================================================================
        ' Well, isn't this ugly.  The following set of if/elseif statements
        ' handle moving up and down like an excel spreadsheet within the
        ' line item detail
        '======================================================================
        If isKeyUp And UCase(curControlName) = UCase("GS1PRICE") Then
            HandleKeyUpArrow KeyCode, Me!QUANT1, False
        ElseIf isKeyDown And UCase(curControlName) = UCase("GS1PRICE") Then
            HandleKeyDownArrow KeyCode, Me!QUANT1, True
        ElseIf isKeyUp And UCase(curControlName) = UCase("QUANT1") Then
            HandleKeyUpArrow KeyCode, Me!GS1PRICE, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("QUANT1") Then
            HandleKeyDownArrow KeyCode, Me!GS1PRICE, False
       
        '----------------------------------------------------------------------
        ' HANDLE GS2PRICE and QUANT2
        ElseIf isKeyUp And UCase(curControlName) = UCase("GS2PRICE") Then
            HandleKeyUpArrow KeyCode, Me!QUANT2, False
        ElseIf isKeyDown And UCase(curControlName) = UCase("GS2PRICE") Then
            HandleKeyDownArrow KeyCode, Me!QUANT2, True
        ElseIf isKeyUp And UCase(curControlName) = UCase("QUANT2") Then
            HandleKeyUpArrow KeyCode, Me!GS2PRICE, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("QUANT2") Then
            HandleKeyDownArrow KeyCode, Me!GS2PRICE, False
       
        '----------------------------------------------------------------------
        ' HANDLE GS3PRICE and QUANT3
        ElseIf isKeyUp And UCase(curControlName) = UCase("GS3PRICE") Then
            HandleKeyUpArrow KeyCode, Me!QUANT3, False
        ElseIf isKeyDown And UCase(curControlName) = UCase("GS3PRICE") Then
            HandleKeyDownArrow KeyCode, Me!QUANT3, True
        ElseIf isKeyUp And UCase(curControlName) = UCase("QUANT3") Then
            HandleKeyUpArrow KeyCode, Me!GS3PRICE, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("QUANT3") Then
            HandleKeyDownArrow KeyCode, Me!GS3PRICE, False
       
        '----------------------------------------------------------------------
        ' HANDLE GS4PRICE and QUANT4
        ElseIf isKeyUp And UCase(curControlName) = UCase("GS4PRICE") Then
            HandleKeyUpArrow KeyCode, Me!QUANT4, False
        ElseIf isKeyDown And UCase(curControlName) = UCase("GS4PRICE") Then
            HandleKeyDownArrow KeyCode, Me!QUANT4, True
        ElseIf isKeyUp And UCase(curControlName) = UCase("QUANT4") Then
            HandleKeyUpArrow KeyCode, Me!GS4PRICE, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("QUANT4") Then
            HandleKeyDownArrow KeyCode, Me!GS4PRICE, False
       
        '----------------------------------------------------------------------
        ' HANDLE GS5PRICE and QUANT5
        ElseIf isKeyUp And UCase(curControlName) = UCase("GS5PRICE") Then
            HandleKeyUpArrow KeyCode, Me!QUANT5, False
        ElseIf isKeyDown And UCase(curControlName) = UCase("GS5PRICE") Then
            HandleKeyDownArrow KeyCode, Me!QUANT5, True
        ElseIf isKeyUp And UCase(curControlName) = UCase("QUANT5") Then
            HandleKeyUpArrow KeyCode, Me!GS5PRICE, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("QUANT5") Then
            HandleKeyDownArrow KeyCode, Me!GS5PRICE, False
       
        '----------------------------------------------------------------------
        ' HANDLE GS6PRICE and QUANT6
        ElseIf isKeyUp And UCase(curControlName) = UCase("GS6PRICE") Then
            HandleKeyUpArrow KeyCode, Me!QUANT6, False
        ElseIf isKeyDown And UCase(curControlName) = UCase("GS6PRICE") Then
            HandleKeyDownArrow KeyCode, Me!QUANT6, True
        ElseIf isKeyUp And UCase(curControlName) = UCase("QUANT6") Then
            HandleKeyUpArrow KeyCode, Me!GS6PRICE, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("QUANT6") Then
            HandleKeyDownArrow KeyCode, Me!GS6PRICE, False
       
        '----------------------------------------------------------------------
        ' HANDLE Design
        ElseIf isKeyUp And UCase(curControlName) = UCase("DsgnCode") Then
            HandleKeyUpArrow KeyCode, Me!DsgnCode, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("DsgnCode") Then
            HandleKeyDownArrow KeyCode, Me!DsgnCode, True
       
        '----------------------------------------------------------------------
        ' HANDLE TapeOff
        ElseIf isKeyUp And UCase(curControlName) = UCase("TapeOff") Then
            HandleKeyUpArrow KeyCode, Me!TapeOff, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("TapeOff") Then
            HandleKeyDownArrow KeyCode, Me!TapeOff, True
       
        '----------------------------------------------------------------------
        ' HANDLE Line
        ElseIf isKeyUp And UCase(curControlName) = UCase("LineCode") Then
            HandleKeyUpArrow KeyCode, Me!LineCode, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("LineCode") Then
            HandleKeyDownArrow KeyCode, Me!LineCode, True
       
        '----------------------------------------------------------------------
        ' HANDLE Color
        ElseIf isKeyUp And UCase(curControlName) = UCase("ColorCode") Then
            HandleKeyUpArrow KeyCode, Me!ColorCode, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("ColorCode") Then
            HandleKeyDownArrow KeyCode, Me!ColorCode, True
       
        '----------------------------------------------------------------------
        ' HANDLE Garment
        ElseIf isKeyUp And UCase(curControlName) = UCase("GarmCode") Then
            HandleKeyUpArrow KeyCode, Me!GarmCode, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("GarmCode") Then
            HandleKeyDownArrow KeyCode, Me!GarmCode, True
       
        '----------------------------------------------------------------------
        ' HANDLE Name Drop
        ElseIf isKeyUp And UCase(curControlName) = UCase("NumOfNameDrops") Then
            HandleKeyUpArrow KeyCode, Me!NumOfNameDrops, True
        ElseIf isKeyDown And UCase(curControlName) = UCase("NumOfNameDrops") Then
            HandleKeyDownArrow KeyCode, Me!NumOfNameDrops, True
        End If
    End If
End Sub



0
 
LVL 32

Expert Comment

by:jadedata
ID: 9739435
Hot-keys are not a work around, they are a valid built-in feature of form controls and control labels.

If I believed it couldn't be done, I would have said that from the beginning.

Not knowing the structures of your database, I don't think I could have come anywhere near that routine.
0
 

Author Comment

by:steranka
ID: 9744739
I didn't mean to imply that the Hot-keys wouldn't work, just that they aren't the solution I was looking for.  What I'm asking is "Is there a way to make it work with the up/down arrow keys"?  If so what is it?
0
 

Author Comment

by:steranka
ID: 9744747
Just upped the points, because I appreciate your input.
0
 
LVL 39

Expert Comment

by:stevbe
ID: 10025094
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jadedata {http:#9711038}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

stevbe
EE Cleanup Volunteer
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question