Forcing Integers into Textbox

  • Thread starter Thread starter Nigel
  • Start date Start date
N

Nigel

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
 
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 said:
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
=---
 
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)
 
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
 
Back
Top