View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rob van Gelder[_4_] Rob van Gelder[_4_] is offline
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
=---