Solved

Form_KeyDown event in Access not firing when press Escape key

Posted on 2013-01-06
20
625 Views
Last Modified: 2013-03-05
For some reason it fires when I press CTRL, but not ESC.

Any ideas?
0
Comment
Question by:TimHudspith
  • 6
  • 4
  • 4
  • +2
20 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38749882
According to this link, the KeyDown event does not occur when you press the ESC key if the form has a command button for which the Cancel property is set to Yes.

Try using the KeyUp event instead.
0
 

Author Comment

by:TimHudspith
ID: 38749959
My question needs amending: this behaviour relates to the ListView_KeyDown event.
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38750439
I don't understand. Is it not the Escape key anymore we're talking about?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38751378
TimHudspith,

What is your ultimate goal here...?

If you want to see if the ESC key was pressed for a control, then you may have to use the KeyPRESS event not the KeyDown event

Private Sub YouListBox_KeyPress(KeyAscii As Integer)
    If KeyAscii = 27 Then
        MsgBox "You pressed the ESC key"
    End If
End Sub


JeffCoachman
0
 
LVL 75
ID: 38751538
"ListView_KeyDown event"

Are you talking about the Microsoft ActiveX List View control - if so, there is no Keydown event.

1
2
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38752067
TimHudspith,

MX is correct, ..
I saw "ListView", and thought "ListBox".
:-(

What does a ListView do for you that a Listbox cannot?

Still not clear on your ultimate goal here.
0
 

Author Comment

by:TimHudspith
ID: 38944742
Sorry for the late response to this, but the list view (MS common control Lib.) does have a Keydown event, but it does not show in the event list in the properties window. It is only accessible through the form code module.

For some reason - unlike the Form_KeyDown handler - it will not capture a press on the Escape key.
0
 

Author Comment

by:TimHudspith
ID: 38951875
I've requested that this question be deleted for the following reason:

no solution
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38945406
I assume your trying to deselect an item you just selected in the listview control.  How about using the Form's KeyDown event along with ActiveControl like this:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 27 And Me.ActiveControl.Name = "ListView1" Then
        Me.ListView1.SelectedItem.Selected = False
    End If
End Sub

Open in new window

You would need to set the form's KeyPreview property to true for this to work.
0
 
LVL 75
ID: 38945870
"You would need to set the form's KeyPreview property to true for this to work."
Which may be the problem to start with ?

mx
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 38949707
Tim,

<<Sorry for the late response to this, but the list view (MS common control Lib.) >>

  Your going to find that there are few controls that work properly and fully with Access as it does not have a full implementation of the COM interfaces.

  Unless a control is stated as specifically working with Access, it's best to stay away from it.

  The behavior your seeing is a result of that.  Access/VBA is the not the same as VB.

Jim.
0
 
LVL 57
ID: 38949712
I should also add with that the fact that there is no solution is not  the fault of those answering the question in all cases.

  Sometimes the answer is "you really can't to that", which in of itself, is an answer.

Jim.
0
 
LVL 75
ID: 38951199
OK ... I just added a ListView Ax Control to a Form.
I went into the code behind the LV ... and added a Msgbox in the KeyDown event.

It works for me - see images.

Be *sure* the Form's Key Preview is set to Yes.

KeyDown ...

mx
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 250 total points
ID: 38951257
OK ... the only key event that seems to work - for ESCAPE - for the ListView control is KeyUp ... see image.

ESC - Key UP
0
 

Author Comment

by:TimHudspith
ID: 38951880
Thanks for responses. Jim Dettman - as you say, it is one of those things that just cannot be done.
0
 
LVL 75
ID: 38951890
Well ... apparently not with Key Down, but it can with Key Up ...

?
0
 
LVL 57
ID: 38953558
As MX has pointed out, it seems you can get what you want with Keyup.  Please check out his comments and if that's the case, I'll re-open the question so you can reassign points.

Jim.
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38953810
Be *sure* the Form's Key Preview is set to Yes.
No need to do this.
You only need to set the form's KeyPreview property to Yes if you are using the Form's KeyUp, KeyDown, or KeyPress event just like in the example I gave earlier.

The KeyUp event of the ListView control works (as pointed out by MX).  No need to change the KeyPreview property.
0
 
LVL 75
ID: 38954400
"You only need to set the form's KeyPreview property to Yes if you are using the Form's KeyUp, KeyDown, or KeyPress event just like in the example I gave earlier."

The things one forgets. Thx for the reminder ... :-)

Jim ... no worries about reopening.  I was really just curious as to why this was not working at all.  And still, bit of odd behavior.



mx
0
 
LVL 57
ID: 38954581
<<Jim ... no worries about reopening. >>

 For future readers, it's always best to make sure things are as they should be.  If the technique solves the problem, it should be labled as such.

Jim.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

760 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

23 Experts available now in Live!

Get 1:1 Help Now