Solved

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

Posted on 2003-11-09
8
463 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
  • 4
  • 3
8 Comments
 
LVL 32

Accepted Solution

by:
jadedata earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:steranka
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 32

Expert Comment

by:jadedata
Comment Utility
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
Comment Utility
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
Comment Utility
Just upped the points, because I appreciate your input.
0
 
LVL 39

Expert Comment

by:stevbe
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now