Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 661
Default Textbox edit

I have a text box in a form I would like the user to enter numbers only in.
On this forum, some code was posted:

Private Sub TextBox1_Key Press
If KeyAscii <48 or Key Ascii 57 Then
KeyAscii=0
End If

This works great EXCEPT it will not allow input of a negative number, which
I need. How can I modify this code to allow a negative number to be input?

Thanks!

--
Paul
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Textbox edit

The ASCII code for "-" is 45, so you can allow that; i.e:
If (KeyAscii < 48 and KeyAscii <45) or KeyAscii 57 Then...
The only problem: the user could enter something like 34-56 unless you also
check to see if it is the first character in the textbox, so you may need to
write some code to deal with this possibility.

"Paul" wrote:

I have a text box in a form I would like the user to enter numbers only in.
On this forum, some code was posted:

Private Sub TextBox1_Key Press
If KeyAscii <48 or Key Ascii 57 Then
KeyAscii=0
End If

This works great EXCEPT it will not allow input of a negative number, which
I need. How can I modify this code to allow a negative number to be input?

Thanks!

--
Paul

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Textbox edit

Hi Paul

Use this (45 = -)

Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Select Case keyascii
Case 45
Case 48 To 57 'Numbers
Case Else 'Discard anything else
keyascii = 0
End Select
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Paul" wrote in message ...
I have a text box in a form I would like the user to enter numbers only in.
On this forum, some code was posted:

Private Sub TextBox1_Key Press
If KeyAscii <48 or Key Ascii 57 Then
KeyAscii=0
End If

This works great EXCEPT it will not allow input of a negative number, which
I need. How can I modify this code to allow a negative number to be input?

Thanks!

--
Paul



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Textbox edit

Check separately, for the first char being a - and there not already being
one, or have a checkbox to signify negative.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"K Dales" wrote in message
...
The ASCII code for "-" is 45, so you can allow that; i.e:
If (KeyAscii < 48 and KeyAscii <45) or KeyAscii 57 Then...
The only problem: the user could enter something like 34-56 unless you

also
check to see if it is the first character in the textbox, so you may need

to
write some code to deal with this possibility.

"Paul" wrote:

I have a text box in a form I would like the user to enter numbers only

in.
On this forum, some code was posted:

Private Sub TextBox1_Key Press
If KeyAscii <48 or Key Ascii 57 Then
KeyAscii=0
End If

This works great EXCEPT it will not allow input of a negative number,

which
I need. How can I modify this code to allow a negative number to be

input?

Thanks!

--
Paul



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Textbox edit

Only once, and at start Ron

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Static fHyphen As Boolean
Select Case KeyAscii
Case 45
If fHyphen Or Len(TextBox1.Text) 1 Then
KeyAscii = 0
End If
Case 48 To 57 'Numbers
Case Else 'Discard anything else
KeyAscii = 0
End Select
End Sub


Regards

Bob


"Ron de Bruin" wrote in message
...
Hi Paul

Use this (45 = -)

Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Select Case keyascii
Case 45
Case 48 To 57 'Numbers
Case Else 'Discard anything else
keyascii = 0
End Select
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Paul" wrote in message

...
I have a text box in a form I would like the user to enter numbers only

in.
On this forum, some code was posted:

Private Sub TextBox1_Key Press
If KeyAscii <48 or Key Ascii 57 Then
KeyAscii=0
End If

This works great EXCEPT it will not allow input of a negative number,

which
I need. How can I modify this code to allow a negative number to be

input?

Thanks!

--
Paul







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Textbox edit

Hi Bob


Or maybe

Case 45: If TextBox1.SelStart < 0 Then keyascii = 0


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Bob Phillips" wrote in message ...
Only once, and at start Ron

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Static fHyphen As Boolean
Select Case KeyAscii
Case 45
If fHyphen Or Len(TextBox1.Text) 1 Then
KeyAscii = 0
End If
Case 48 To 57 'Numbers
Case Else 'Discard anything else
KeyAscii = 0
End Select
End Sub


Regards

Bob


"Ron de Bruin" wrote in message
...
Hi Paul

Use this (45 = -)

Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Select Case keyascii
Case 45
Case 48 To 57 'Numbers
Case Else 'Discard anything else
keyascii = 0
End Select
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Paul" wrote in message

...
I have a text box in a form I would like the user to enter numbers only

in.
On this forum, some code was posted:

Private Sub TextBox1_Key Press
If KeyAscii <48 or Key Ascii 57 Then
KeyAscii=0
End If

This works great EXCEPT it will not allow input of a negative number,

which
I need. How can I modify this code to allow a negative number to be

input?

Thanks!

--
Paul







  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Textbox edit

even nicer!

A better solution, and highlighting that my fHyphen variable was superfluous
(in such a nice way :-))

Bob


"Ron de Bruin" wrote in message
...
Hi Bob


Or maybe

Case 45: If TextBox1.SelStart < 0 Then keyascii = 0


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Bob Phillips" wrote in message

...
Only once, and at start Ron

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Static fHyphen As Boolean
Select Case KeyAscii
Case 45
If fHyphen Or Len(TextBox1.Text) 1 Then
KeyAscii = 0
End If
Case 48 To 57 'Numbers
Case Else 'Discard anything else
KeyAscii = 0
End Select
End Sub


Regards

Bob


"Ron de Bruin" wrote in message
...
Hi Paul

Use this (45 = -)

Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
Select Case keyascii
Case 45
Case 48 To 57 'Numbers
Case Else 'Discard anything else
keyascii = 0
End Select
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Paul" wrote in message

...
I have a text box in a form I would like the user to enter numbers

only
in.
On this forum, some code was posted:

Private Sub TextBox1_Key Press
If KeyAscii <48 or Key Ascii 57 Then
KeyAscii=0
End If

This works great EXCEPT it will not allow input of a negative number,

which
I need. How can I modify this code to allow a negative number to be

input?

Thanks!

--
Paul








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP! I Lost The Ability To Advance From TextBox To TextBox With the ENTER Or The TAB Keys Minitman[_4_] Excel Programming 0 February 22nd 05 08:50 PM
Textbox Bug? Missing/delayed update of textbox filled via VBA MarcM Excel Programming 0 November 4th 04 05:47 PM
Textbox Bug? Missing/delayed update of textbox filled via VBA MarcM Excel Programming 0 November 4th 04 05:43 PM
Edit Excel TextBox using C# Manish Excel Programming 1 September 29th 04 10:01 AM
Edit Textbox using KeyPress vba code in Access Arno Excel Programming 3 February 23rd 04 11:57 AM


All times are GMT +1. The time now is 07:58 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"