Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Increasing value by arrow key

Status
Not open for further replies.

jamaarneen

Programmer
Dec 27, 2007
213
BE

Hi

Maybe it's very simple... but would somebody please help me ...
I would like to give the user of a form, to increase/decrease the value of a textbox by using the ArrowUp/ArrowDown keys.

I would use the 'Key Press' event, but how do I recognize if it's the 'ArrowUp' or 'ArrowDown' key ?

Thanks in advance,
Ja
 
In the dutch version of Access i have "bij Toets omhoog" & "Bij Toets Omlaag" dont know what it is in English version, but there should be two of them,.... one is up and one is down

Greetz

Lightlancer



ik heb het getest en het werkt wel, je moet alleen je eigen vba code er even op aanpassen.......
 

Hi Lightlancer,

Thank you, but by my knowledge, the 'On Key Up' 'On Key Down' event's are triggered when any key is being held down, or released. this event is passing as argument the 'Key Code', so we can know which key was pressed. but it had nothing to do with the 'ArrowUp' or 'ArrowDown' key.

but I found my solution - if the key pressed is 'ArrowUp', then its code will be a constant named 'vbKeyUp' and fro down it is 'vbKeyDown'.

So all I have to do is: if KeyCode = vbKeyUp Then...

Ja
 
well i only did try to use arrow up so i thought that was it, but thanks for sharing the info....
 
So all I have to do is: if KeyCode = vbKeyUp Then...

Not true

Key code is a number that represents the key being pressed. You want to trap 38 and 40 (Arrow up and down)

Your code should look something like this:

Code:
Private Sub txtIncDec_KeyDown(KeyCode As Integer, Shift As Integer)
Dim intValue As Integer
intValue = CInt(Me.txtIncDec)
Select Case KeyCode
    Case 38 ' Arrow Down
        intValue = intValue + 1
    Case 40 ' Arrow UP
        intValue = intValue - 1
    End Select
Me.txtIncDec = intValue
End Sub

Tyrone Lumley
SoCalAccessPro
 
How are ya Lightlancer . . .

In the [blue]On Key Down[/blue] event of the textbox in question, copy/paste the following:
Code:
[blue]   If Shift = 0 Then
      If KeyCode = vbKeyUp Or KeyCode = vbKeyDown Then
         Me![[purple][B][I]TextboxName[/I][/B][/purple]] = Nz(Me![[purple][B][I]TextboxName[/I][/B][/purple]]) + 39 + (-1 * KeyCode)
         KeyCode = 0
      End If
   End If
[/blue]
Be sure to disable or remove the code in your [blue]Key Press[/blue] event!

Note: by using the key down event, you can [blue]continuously increment/decrement[/blue] by holding the key!

[blue]Your Thoughts! . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Hi to all of you - Thanks for your replies...
[Further I'm quoting my code. Im prouwd that it is wotking, but PLEASE have a liik at it - I would like some comments from the experienced once...]

1) To TheAceman1: it's amazing the way you wrote this formula... (but does it make the code any faster?)

2) To SoCalAccessPro: PHV is right. vbKeyUp and vbKeyDown are constants representing numbers.

3) To all of you: I discovered the following - the Arrow keys will trigger only the 'KeyPress' event, while the Plus and Minus keys will trigger only the 'KeyDown' event!!!
Interesting. or am I gaga?...

3) here is the function I'm using - It is for both Arrow keys and plus/minus. (Also, the user can define if there is an upper or lower bound)
Code:
Private Function Increase(varValue As Variant, intUpper As Integer)
    If intUpper >= 0 Then
        If varValue < intUpper Then
            Increase = varValue + 1
        Else
            Increase = varValue
        End If
    Else
            Increase = varValue + 1
    End If
End Function
Private Function Decrease(varValue As Variant, intLower As Integer)
    If intLower >= 0 Then
        If varValue > intLower Then
            Decrease = varValue - 1
        Else
            Decrease = varValue
        End If
    Else
            Decrease = varValue - 1
    End If
End Function
Public Sub p_KeyPlusAndMinus(kKeyAscii, Optional intUpperBound As Integer = -1, Optional intLowerBound As Integer = -1)
Select Case kKey
    Case 43        ' Plus key
        kKey = 0
        Screen.ActiveControl = Increase(Screen.ActiveControl, intUpperBound)
    Case vbKeyUp
        kKey = 0
        Screen.ActiveControl = Increase(Screen.ActiveControl, intUpperBound)
    Case 45             ' Minus key
        kKey = 0
        Screen.ActiveControl = Decrease(Screen.ActiveControl, intLowerBound)
    Case vbKeyDown
        kKey = 0
        Screen.ActiveControl = Decrease(Screen.ActiveControl, intLowerBound)
    End Select

End Sub

5) To all of you again: Thank you
Ja
 

Hi

it's the sub "p_KeyPlusAndMinus" that I'm using.
And, yes, "kKeyAscii" is a mistake. should be "kKey".
 
jamaarneen said:
[blue]1) . . . (but does it make the code any faster?)
This is an academic question as no one can type as fast as VBA can execute. It would have to be a really long routine to make a difference.
jamaarneen said:
[blue]3) . . . I discovered the following - the Arrow keys will trigger only the 'KeyPress' event, while the Plus and Minus keys will trigger only the 'KeyDown' event!!!
Interesting. or am I gaga?...[/blue]
I'm forced to say gaga!

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thanks - AceMan1

TheAceman1 said:
I'm forced to say gaga!
Sorry, but can you then try to help me to de'gaga' myself?...


because rightnow I tried again. and here are the facts:
I am calling the public sub in both events. identicaly the same (the only differents is, that one is passing 'KeyAscii' as argument, while the other is passing 'KeyCode'. both seems to me integers).

when the line in 'KeyPress' was in comment - [blue]only[/blue] the Up/Down keys made something change...
when the line in 'KeyDown' was in comment - [blue]only[/blue] the plus/minus keys made something change...
(Well sorry, it is just the oppositr then I wrote in the previous thread...)

(Have you looked at my code?)
 
jamaarneen . . .

Note: [blue]Keys Code values[/blue] may not be the same as [blue]Acsii Values[/blue]. For instance:

[tt][blue] Key Press KeyDown/Up
Key ACSII Shift KeyCode
********** ********* ***** *******
KeyPad + 43 1 107
KeyPad - 45 0 109

KeyBoard + 43 1 187
KeyBoard - 45 0 189

KeyBoard Up X 0 38
KeyBoard Dn X 0 40[/blue][/tt]

Are you detecting the right codes?

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 

Thanks a loooooooooooot!

So I've learned the following:
- every key (except for ESC & ENTER) triggers both events. the difference is only which argument is passed.

- KeyCode and KeyASCII are in essence tow separated lists (set's?) of value. (i'll still need to make some homework to figure out why two separate lists...)

- by the way, sinds I'm also working with some other (RightToLeft) languages, I desperately need some reading-stuff about 'unicode' / ANSI vs ASCII / and other character-related topics. I would ver aptecite if you could refer me to some place where I can find somthing...

- conclusion for myself: never go gaga to quickly...:)

Ja
 
jamaarneen said:
[blue](i'll still need to make some homework to figure out why two separate lists...)[/blue]
Why two lists i can't say, but [blue]OnKey Down/UP[/blue] events include detection of control keys, function keys, arrow keys, and others, where as [blue]OnKeyPress[/blue] does not!

Also [blue]OnKeyDown[/blue] repeats as long as you hold the key(s), [blue]OnKeyPress[/blue] does not!

A big point here you only need [blue]OnKeyDown[/blue]!


Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top