Cancel keydown event when editing textbox

JasperK

New member
Local time
Today, 18:17
Joined
Dec 20, 2007
Messages
7
Hi all,

Like it says in the topic, i want to cancel a keydown event when i am editing a textbox, but not when a textbox has the focus.

For example:

Private Sub TextBox2_KeyDown(KeyCode As Integer, Shift As Integer)
if keycode = 39 then
Textbox1.setfocus
end if
End Sub

In this example the focus always goes to textbox1 when i am pressing the left arrow. So if i want to correct a type error, i cannot use the arrow!

Is there a sollution for this? Is it possible to see when a textbox just has the focus, or when you really editing the value inside the textbox?

Many thanks,
Jasper
 
I didn't understand that (I'm a bit slow), but I think you can find out which control is active like this (this example test for buttons, not textboxes).

Dim c As Object
Set c = Screen.ActiveControl
If c.ControlType = Access.acCommandButton Then
MsgBox c.Name
End If
 
No i am sorry, this is not what i meant.

Back to the example:

I have a textbox named TextBox2. On the keydown event i have :

Private Sub TextBox2_KeyDown(KeyCode As Integer, Shift As Integer)
if keycode = 39 then
Textbox1.setfocus
end if
End Sub

When that textbox has the focus, and i press the letters HELO, this will appear in the textbox, but when i want to add one 'L' (to make the text HELLO appear in the textbox), i try to use the left arrow to move the cursor, but then the focus goes to Textbox1.

i only want to move the focus when i am not editing that textbox.
 
I think the code I gave you should help. Based on what you said, if a textbox already has the focus (i.e. you are editing that textbox), you want to abstain from calling setFocus. Therefore you need code to detect when a textbox is being edited (i.e. detect when it has has focus). That's what my code above should help you do, or at least get you started.

In fact, that seems what you asked for:
Is it possible to see when a textbox just has the focus

which I read that to mean:

Is it possible to detect when a textbox just has the focus
 
Last edited:
I'm sorry, I was just thinking I've been wrong on this.

Your question reminds me of an app (from a long time ago) where the user used a hotkey to notify me he wanted to be in edit mode. After editing he would hit that key again to toggle off edit mode.

Maybe you could use a couple of boolean variables to capture when the user wants edit mode. Meaning, when he hits any alphanumeric key, assume that edit mode has started, and then stay in edit mode until he leaves the textbox.
 
No problem!

Your last post is what i meant! The edit mode can be captured by pressing the F2 button and the left mouse button in the textbox.

Thanks jal!
 
okay well if you set the keycode to -1 it appears to cancel the event's . I found this useful for disabling the default PGDN and PGUP events of an Access form.. where PGDN and PGUP as well as CTRL+HOME ctrl+END allow the user to navigate, even though there were some buttons with validation.. the key presses were not getting validated
 

Users who are viewing this thread

Back
Top Bottom