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

Hi All

What is the simplest method to prevent users entering a non-integer into a
textbox control on a user form?

TIA
Cheers
Nigel




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! 100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Forcing Integers into Textbox

Nigel,

One way of doing it:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim strTemp As String, blnFound As Boolean

blnFound = False
strTemp = TextBox1.Text
If IsNumeric(strTemp) Then
If Int(strTemp) = strTemp Then blnFound = True
End If
If Not blnFound Then
MsgBox "Value must be an Integer", vbExclamation
TextBox1.Text = ""
Cancel = True
End If
End Sub

Rob


"Nigel" wrote in message
...
Hi All

What is the simplest method to prevent users entering a non-integer into a
textbox control on a user form?

TIA
Cheers
Nigel




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! 100,000

Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

=---


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Forcing Integers into Textbox

Nigel,

Another way is to trap on key input. This shows the way

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
Beep
End Select

End Sub

Typing anything other than 0-9 returns a beep. You could easily modify this
to play a sound, or throw up a message.

--

HTH

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

"Rob van Gelder" wrote in message
...
Nigel,

One way of doing it:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim strTemp As String, blnFound As Boolean

blnFound = False
strTemp = TextBox1.Text
If IsNumeric(strTemp) Then
If Int(strTemp) = strTemp Then blnFound = True
End If
If Not blnFound Then
MsgBox "Value must be an Integer", vbExclamation
TextBox1.Text = ""
Cancel = True
End If
End Sub

Rob


"Nigel" wrote in message
...
Hi All

What is the simplest method to prevent users entering a non-integer into

a
textbox control on a user form?

TIA
Cheers
Nigel




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! 100,000

Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via

Encryption
=---




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default Forcing Integers into Textbox

Thanks for the suggestions.

I need to allow any integer value from 1 to n; where n is in the range 1 to
9999, so the general purpose approach would seem to require taking the
integer value and testing against the input as Rob suggested.
I can cope with non numeric input since I take the Val(TextBox1.Value). I am
also using the TextBox update event to take the entry just made.

Can't see any problems with this at present

Thanks again
Cheers
Nigel


"Bob Phillips" wrote in message
...
Nigel,

Another way is to trap on key input. This shows the way

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
Beep
End Select

End Sub

Typing anything other than 0-9 returns a beep. You could easily modify

this
to play a sound, or throw up a message.

--

HTH

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

"Rob van Gelder" wrote in message
...
Nigel,

One way of doing it:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim strTemp As String, blnFound As Boolean

blnFound = False
strTemp = TextBox1.Text
If IsNumeric(strTemp) Then
If Int(strTemp) = strTemp Then blnFound = True
End If
If Not blnFound Then
MsgBox "Value must be an Integer", vbExclamation
TextBox1.Text = ""
Cancel = True
End If
End Sub

Rob


"Nigel" wrote in message
...
Hi All

What is the simplest method to prevent users entering a non-integer

into
a
textbox control on a user form?

TIA
Cheers
Nigel




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World!

100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via

Encryption
=---








----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! 100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Forcing Integers into Textbox

Yep, that's a better way!
Need to test for the minus character as well.


"Bob Phillips" wrote in message
...
Nigel,

Another way is to trap on key input. This shows the way

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
Beep
End Select

End Sub

Typing anything other than 0-9 returns a beep. You could easily modify

this
to play a sound, or throw up a message.

--

HTH

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

"Rob van Gelder" wrote in message
...
Nigel,

One way of doing it:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim strTemp As String, blnFound As Boolean

blnFound = False
strTemp = TextBox1.Text
If IsNumeric(strTemp) Then
If Int(strTemp) = strTemp Then blnFound = True
End If
If Not blnFound Then
MsgBox "Value must be an Integer", vbExclamation
TextBox1.Text = ""
Cancel = True
End If
End Sub

Rob


"Nigel" wrote in message
...
Hi All

What is the simplest method to prevent users entering a non-integer

into
a
textbox control on a user form?

TIA
Cheers
Nigel




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World!

100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via

Encryption
=---






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
decrease integers to zero Andi Excel Worksheet Functions 7 May 9th 09 02:35 AM
Calculate Textbox value based on another textbox value.doc Tdungate Excel Discussion (Misc queries) 1 February 12th 09 07:11 PM
Calculate Textbox value based on another textbox value Tdungate Excel Discussion (Misc queries) 0 February 12th 09 07:03 PM
A list of Consecutive Integers, can I search for missing integers CM Excel Worksheet Functions 4 September 2nd 05 06:38 PM
UserForm TextBox to ActiveSheet TextBox over 256 characters Dan E[_2_] Excel Programming 1 July 28th 03 07:36 PM


All times are GMT +1. The time now is 06:10 PM.

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"