ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Edit Textbox using KeyPress vba code in Access (https://www.excelbanter.com/excel-programming/292231-edit-textbox-using-keypress-vba-code-access.html)

Arno

Edit Textbox using KeyPress vba code in Access
 
Hello,
I am trying to edit a textbox which contains a number. When focus is
on the textbox, and a user press the "arrow up", I would like the
textbox value to increase of 100, and at the opposite when the user
press "arrow down", I would like this value to decrease by 100.
For the moment I have done that using the "keypress" event, but I have
a problem, as this event reacts to any key of the keyboard.
So I would need whether another idea to proceed or a way to qualify
the key which is being pressed as the "arrow up" or "down", then I
could use a conditional phrase.
Thank you for your help.


No Name

Edit Textbox using KeyPress vba code in Access
 
Why don't you simply use a Spin Button instead of a
TextBox? It does exactly what you need to do.
-----Original Message-----
Hello,
I am trying to edit a textbox which contains a number.

When focus is
on the textbox, and a user press the "arrow up", I would

like the
textbox value to increase of 100, and at the opposite

when the user
press "arrow down", I would like this value to decrease

by 100.
For the moment I have done that using the "keypress"

event, but I have
a problem, as this event reacts to any key of the

keyboard.
So I would need whether another idea to proceed or a way

to qualify
the key which is being pressed as the "arrow up"

or "down", then I
could use a conditional phrase.
Thank you for your help.

.


Keith Willshaw

Edit Textbox using KeyPress vba code in Access
 

"Arno" wrote in message
om...
Hello,
I am trying to edit a textbox which contains a number. When focus is
on the textbox, and a user press the "arrow up", I would like the
textbox value to increase of 100, and at the opposite when the user
press "arrow down", I would like this value to decrease by 100.
For the moment I have done that using the "keypress" event, but I have
a problem, as this event reacts to any key of the keyboard.
So I would need whether another idea to proceed or a way to qualify
the key which is being pressed as the "arrow up" or "down", then I
could use a conditional phrase.
Thank you for your help.


Keypress only returns ASCII codes, you need
to use the keydown function which returns the
code of the key pressed.

Addin the following sub to a text box will display
the value returned by the key pressed and
allow you to chose what action to take

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
TextBox1.Text = "Key pressed was " & KeyCode
End Sub

The down Arrow returns 40, up arrow returns 38,
left arrow 27 and right arrow 39

Keith



Bob Phillips[_6_]

Edit Textbox using KeyPress vba code in Access
 
Arno,

Try the KeyDown event. For instance

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)

Select Case KeyCode
Case Is = vbKeyUp
If TextBox1.Value = "" Then
TextBox1.Value = 100
Else
TextBox1.Value = TextBox1.Value + 100
End If
KeyCode = 0
Case Is = vbKeyDown
If TextBox1.Value <= 101 Or TextBox1.Value = "" Then
TextBox1.Value = ""
Else
TextBox1.Value = TextBox1.Value - 100
End If
KeyCode = 0
End Select

End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Arno" wrote in message
om...
Hello,
I am trying to edit a textbox which contains a number. When focus is
on the textbox, and a user press the "arrow up", I would like the
textbox value to increase of 100, and at the opposite when the user
press "arrow down", I would like this value to decrease by 100.
For the moment I have done that using the "keypress" event, but I have
a problem, as this event reacts to any key of the keyboard.
So I would need whether another idea to proceed or a way to qualify
the key which is being pressed as the "arrow up" or "down", then I
could use a conditional phrase.
Thank you for your help.





All times are GMT +1. The time now is 01:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com