Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Form_KeyDown event in Access not firing when press Escape key

Posted on 2013-01-06
20
Medium Priority
?
739 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 750 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 58
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 - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 750 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 58
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 58
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

963 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