ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Validate textbox entry (https://www.excelbanter.com/excel-programming/329332-validate-textbox-entry.html)

Stuart[_21_]

Validate textbox entry
 
I present a textbox where the user knows to enter only text (upper case,
lower case or a mixture) and/or numbers. Also no spaces.

I can use trim to remove spaces.....but how to check for just alphabetic
characters and numbers, please?

Regards.



Darrin Henshaw

Validate textbox entry
 
Use the KeyDown event. This is some code, I use to allow the users only
to enter numbers for a zipcode:

Private Sub ZipCode_KeyDown(ByVal KeyCode As _ MSForms.ReturnString,
ByVal Shift As String)
With Me.ZipCode
Select Case True
Case (KeyCode = 95 And KeyCode < 105)
'exit quietly
Case (KeyCode = 48 And KeyCode < 57)
'exit quietly
Case KeyCode = 9 'tab
Case KeyCode = 8 'backspace
If Len(.Text) 0 Then
.Text = Left(.Text, Len(.Text) - 1)
End If
KeyCode = 0
Case Else
KeyCode = 0
Beep
End Select
End With
End Sub


Modify your's like so:

Private Sub TextBox1_KeyDown(ByVal KeyCode As _ MSForms.ReturnString,
ByVal Shift As String)
With Me.ZipCode
Select Case True
Case (KeyCode = 97 And KeyCode < 122)
'exit quietly
Case (KeyCode = 48 And KeyCode < 57)
'exit quietly
Case (KeyCode = 65 And KeyCode < 90)
'exit quietly
Case KeyCode = 9 'tab
Case KeyCode = 8 'backspace
If Len(.Text) 0 Then
.Text = Left(.Text, Len(.Text) - 1)
End If
KeyCode = 0
Case Else
KeyCode = 0
Beep
End Select
End With
End Sub

I might have something wrong, modifying it to fit your needs. But it's
enough to get started.

*** Sent via Developersdex http://www.developersdex.com ***

Darrin Henshaw

Validate textbox entry
 
I actually started modifying it for your needs before I started the
post. So ignore my first piece of code, and only take a look at the
second part.

*** Sent via Developersdex http://www.developersdex.com ***


All times are GMT +1. The time now is 09:10 AM.

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