Solved

Form_KeyDown event in Access not firing when press Escape key

Posted on 2013-01-06
20
628 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

932 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

8 Experts available now in Live!

Get 1:1 Help Now